1

For ex:

a = "pandaxngeqrymtso-ezmlaesowxaqbujl-noilktxreecytrql-gskaboofsfoxdtei-utsmakotufodhlrd-iroachimpanzeesa-nintrwflyrkhcdum-jcecahkktiklsvhr-mhvsbaykagodwgca-koalatcwlkfmrwbb-jsrrfdolphinuyt"

a = a.split("-") 

mylist = []

word = ""

while i < len(a[0]): #16

      for elem in a:
        word+= elem[i]
  mylist.append(kelime)

  i += 1

  word = "" 

I just want a list which contains "penguinjmhkj, azostrichos..." But I get an Index error. What can I do?

Isdj
  • 1,835
  • 1
  • 18
  • 36
  • 2
    Your code is unclear. What is kelime, why don't you use ```for i in index(len(a[0])```? Your error might be because you use ```len(a[0])``` without making sure all strings in the array are of the same length? – Isdj Dec 13 '19 at 07:55
  • I'm surprised you don't get an indentation error. – Some programmer dude Dec 13 '19 at 07:55

3 Answers3

2

You can try this:

>>> word = [''.join(letters) for letters in zip(*(list(word) for word in a.split('-')))]
>>> word
['penguinjmkj',
 'azostrichos',
 'nmiksonevar',
 'dllamatcslr',
 'aakbacrabaf',
 'xetokhwhatd',
 'nsxooifkyco',
 'gorftmlkkwl',
 'ewesupytalp',
 'qxeffarigkh',
 'racoonkkofi',
 'yqyxdzhldmn',
 'mbtdhecswru',
 'turtledvgwy',
 'sjqersuhcbt']

Explanation: You can understand what it is doing if you print the individual parts, for e.g.:

>>> print(*(list(word) for word in a.split('-'))
['p', 'a', 'n', 'd', 'a', 'x', 'n', 'g', 'e', 'q', 'r', 'y', 'm', 't', 's', 'o'] ['e', 'z', 'm', 'l', 'a', 'e', 's', 'o', 'w', 'x', 'a', 'q', 'b', 'u', 'j', 'l'] ['n', 'o', 'i', 'l', 'k', 't', 'x', 'r', 'e', 'e', 'c', 'y', 't', 'r', 'q', 'l'] ['g', 's', 'k', 'a', 'b', 'o', 'o', 'f', 's', 'f', 'o', 'x', 'd', 't', 'e', 'i'] ['u', 't', 's', 'm', 'a', 'k', 'o', 't', 'u', 'f', 'o', 'd', 'h', 'l', 'r', 'd'] ['i', 'r', 'o', 'a', 'c', 'h', 'i', 'm', 'p', 'a', 'n', 'z', 'e', 'e', 's', 'a'] ['n', 'i', 'n', 't', 'r', 'w', 'f', 'l', 'y', 'r', 'k', 'h', 'c', 'd', 'u', 'm'] ['j', 'c', 'e', 'c', 'a', 'h', 'k', 'k', 't', 'i', 'k', 'l', 's', 'v', 'h', 'r'] ['m', 'h', 'v', 's', 'b', 'a', 'y', 'k', 'a', 'g', 'o', 'd', 'w', 'g', 'c', 'a'] ['k', 'o', 'a', 'l', 'a', 't', 'c', 'w', 'l', 'k', 'f', 'm', 'r', 'w', 'b', 'b'] ['j', 's', 'r', 'r', 'f', 'd', 'o', 'l', 'p', 'h', 'i', 'n', 'u', 'y', 't']

So it breaks up all the individual words delimited by - into characters. Then zip does this:

>>> print(zip(*(list(word) for word in a.split('-'))))
('p', 'e', 'n', 'g', 'u', 'i', 'n', 'j', 'm', 'k', 'j') ('a', 'z', 'o', 's', 't', 'r', 'i', 'c', 'h', 'o', 's') ('n', 'm', 'i', 'k', 's', 'o', 'n', 'e', 'v', 'a', 'r') ('d', 'l', 'l', 'a', 'm', 'a', 't', 'c', 's', 'l', 'r') ('a', 'a', 'k', 'b', 'a', 'c', 'r', 'a', 'b', 'a', 'f') ('x', 'e', 't', 'o', 'k', 'h', 'w', 'h', 'a', 't', 'd') ('n', 's', 'x', 'o', 'o', 'i', 'f', 'k', 'y', 'c', 'o') ('g', 'o', 'r', 'f', 't', 'm', 'l', 'k', 'k', 'w', 'l') ('e', 'w', 'e', 's', 'u', 'p', 'y', 't', 'a', 'l', 'p') ('q', 'x', 'e', 'f', 'f', 'a', 'r', 'i', 'g', 'k', 'h') ('r', 'a', 'c', 'o', 'o', 'n', 'k', 'k', 'o', 'f', 'i') ('y', 'q', 'y', 'x', 'd', 'z', 'h', 'l', 'd', 'm', 'n') ('m', 'b', 't', 'd', 'h', 'e', 'c', 's', 'w', 'r', 'u') ('t', 'u', 'r', 't', 'l', 'e', 'd', 'v', 'g', 'w', 'y') ('s', 'j', 'q', 'e', 'r', 's', 'u', 'h', 'c', 'b', 't')

So it takes all the corresponding characters from each group, each of the tuples get passed as letters in each iteration in the main code. Then you join each tuple, for e.g. in first iteration:

>>> ''.join(('p', 'e', 'n', 'g', 'u', 'i', 'n', 'j', 'm', 'k', 'j'))
'penguinjmkj'

[<item after some operation> for <each item> in <item_list>] this structure is called list comprehension. * is used for iterable unpacking.

Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
1

it took a while but i cracked it;

what you just need to do is to handle the Index Error: String out of range.

if you count the words they are over 16 while the array items after splitting are just over 11. to cut long story short; Handle the exception with a try_except where you are appending the letters at:

 word+= elem[i]

here is my code and how i solved it using try_catch

a = "pandaxngeqrymtso-ezmlaesowxaqbujl-noilktxreecytrql-gskaboofsfoxdtei-utsmakotufodhlrd-iroachimpanzeesa-nintrwflyrkhcdum-jcecahkktiklsvhr-mhvsbaykagodwgca-koalatcwlkfmrwbb-jsrrfdolphinuyt"
newArr = a.split('-')
newWord = []
i = 0
mylist = []
while i < len(newArr[0]):
    word = ""
    for item in newArr:
       try:
            word  += item[i]
       except:
            break
    i += 1
    mylist.append(word)
print(mylist)


I used a try_except to handle the Index Error when appending the letter, then break when ever the 'i' used for the while loop is greater than the newArr length in the for loop.

try for your self!

0

Similar to the code from Sayandip, but it would have been more readable for me in the past:

mylist =[]

for element in zip(*a.split('-')):
    mylist.append(''.join(element))

print(mylist)

I get

['penguinjmkj', 'azostrichos', 'nmiksonevar', 'dllamatcslr', 'aakbacrabaf', 'xetokhwhatd', 'nsxooifkyco', 'gorftmlkkwl', 'ewesupytalp', 'qxeffarigkh', 'racoonkkofi', 'yqyxdzhldmn', 'mbtdhecswru', 'turtledvgwy', 'sjqersuhcbt']

I am moving the splitting in the for loop, and using the * construct to pass the whole list resulting from splitting, see usage here and here.

Alex
  • 194
  • 5