-4

I am having trouble figuring out the following:

Suppose I have a list of strings

strings = ["and","the","woah"]

I want the output to be a list of strings where the ith position of every string becomes a new string item in the array like so

["atw","nho","dea","h"]

I am playing with the following list comprehension

u = [[]]*4
c = [u[i].append(stuff[i])  for i in range(0,4) for stuff in strings]

but its not working out. Can anyone help? I know you can use other tools to accomplish this, but i am particularly interested in making this happen with for loops and list comprehensions. This may be asking a lot, Let me know if I am.

theamateurdataanalyst
  • 2,794
  • 4
  • 38
  • 72
  • 4
    First, it is not ruby. Second, it is not factorial. Third - what exactly is not working? What do you expect, what is error? – dmitry Jul 13 '14 at 18:56
  • _"I know you can use other tools to accomplish this, but i am particularly interested in making this happen with for loops and list comprehensions."_ - Are you sure? Because [`itertools.izip_longest`](https://docs.python.org/2/library/itertools.html#itertools.izip_longest) could do this easily and efficiently: `map(''.join, izip_longest(*strings, fillvalue=''))` –  Jul 13 '14 at 19:00
  • @dmitry sorry I messed up the title – theamateurdataanalyst Jul 13 '14 at 19:05

3 Answers3

0

My idea would be to use itertools.izip_longest and a list comprehension.

>>> from itertools import izip_longest
>>> strings = ["and","the","woah"]
>>> [''.join(x) for x in izip_longest(*strings, fillvalue='')]
['atw', 'nho', 'dea', 'h']
timgeb
  • 76,762
  • 20
  • 123
  • 145
  • Yes perfect. I started to use izip_longest but I was having trouble figuring out how to input multiple arguments and looks like you are doing this via "*". Beautiful my friend. – theamateurdataanalyst Jul 13 '14 at 19:07
  • @user2801122 Check this question and the third answer in particular for further reading: http://stackoverflow.com/questions/287085/what-do-args-and-kwargs-mean – timgeb Jul 13 '14 at 19:11
0

Try

array = ["and","the","woah"]
array1 = []
longest_item = 0
for i in range(0,3): #length of array
    if len(array[i]) > longest_item:
        longest_item = len(array[i]) #find longest string
for i in range(0,longest_item):
    str = ""
    for i1 in range(0,3): #length of array
        if len(array[i1]) < longest_item:
            continue
        str += array[i1][i:i+1]
    array1.append(str)

I didn't actually try this code out, I just improvised it. Please leave a comment ASAP if you find a bug.

Arc676
  • 4,445
  • 3
  • 28
  • 44
0

Using just list comprehensions and for loops you can:

strings = ["and","the","woah"]
#Get a null set to be filled in
new = ["" for x in range(max([len(m) for m in strings]))]
#Cycle through new list
for index,item in enumerate(new):
    for w in strings:
        try: 
            item += w[index]
            new[index] = item
        except IndexError,err:
            pass
print new
user1636615
  • 102
  • 5