biogeek
biogeek

Reputation: 197

How to split elements of a list into a list of lists in Python?

My sample data looks like:

list1 = ['AAAABBBBCCCC','DDDDEEEEFFFF','GGGGHHHHIIII','JJJJKKKKLLLL']

list1b = [['AAAA','BBBB','CCCC'],['DDDD','EEEE','FFFF'],['GGGG','HHHH','IIII'],['JJJJ','KKKK','LLLL']]

I tried to to write a generalisable code for any length of elements:

list1a =[]
list1b =[]

for sublist in list1:
    n = 4
    quad = [input[i:i+n] for i in range(0, len(sublist[0]), n)] 
    list1a.append(quadruplets)
    quad =[] #Setting it back to empty list
    list1b.append(list1a)

print list1b

#Error Message: 
 quad = [input[i:i+n] for i in range(0, len(sublist[0]), n)] 
 TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'

Can anyone please recognize where I may be going wrong and how I can correct it? Is there a simpler way of doing the same?

Upvotes: 2

Views: 394

Answers (3)

trincot
trincot

Reputation: 350127

You just have a wrong variable name in your code: input should be sublist, quaruplets misses a d, and sublist[0] should be just sublist:

quadruplets = [sublist[i:i+n] for i in range(0, len(sublist), n)] 

Then, you don't need an intermediate list, which only multiplies the same lists in the output. So list1a.append can be list1b.append and forget about the rest. n = 4 has no reason of being in a loop, so you can move that out:

list1 = ['AAAABBBBCCCC','DDDDEEEEFFFF','GGGGHHHHIIII','JJJJKKKKLLL']
list1b =[]
n = 4

for sublist in list1:
    quadruplets = [sublist[i:i+n] for i in range(0, len(sublist), n)] 
    list1b.append(quadruplets)

print (list1b)

And once you have that, you can make it one list comprehension:

list1 = ['AAAABBBBCCCC','DDDDEEEEFFFF','GGGGHHHHIIII','JJJJKKKKLLL']
n = 4
list1b = [[sublist[i:i+n] for i in range(0, len(sublist), n)] for sublist in list1]
print (list1b)

Upvotes: 0

dawg
dawg

Reputation: 103744

If you want to group by the same character, you can use groupby to do this:

>>> from itertools import groupby
>>> list1 = ['AAAABBBBCCCC','DDDDEEEEFFFF','GGGGHHHHIIII','JJJJKKKKLLLL']
>>> [[''.join(g) for k,g in groupby(sl)] for sl in list1]
[['AAAA', 'BBBB', 'CCCC'], ['DDDD', 'EEEE', 'FFFF'], ['GGGG', 'HHHH', 'IIII'], ['JJJJ', 'KKKK', 'LLLL']]

If you partitioning is by length vs by character, you can do:

>>> n=4
>>> [[s[i:i+n] for i in range(0, len(s), n)] for s in list1]
[['AAAA', 'BBBB', 'CCCC'], ['DDDD', 'EEEE', 'FFFF'], ['GGGG', 'HHHH', 'IIII'], ['JJJJ', 'KKKK', 'LLLL']]

Upvotes: 1

Joel Johnson
Joel Johnson

Reputation: 176

It might be easier if you generate list2 first before list1b.

list1 = ['AAAABBBBCCCC','DDDDEEEEFFFF','GGGGHHHHIIII','JJJJKKKKLLLL']

list1b = []
list2 = []

n = 4

for i in range(0, len(list1[0]), n):
    list2.append([x[i:i+n] for x in list1])

for i in range(len(list2[0])):
    list1b.append([x[i] for x in list2])

results

list1b = [['AAAA', 'BBBB', 'CCCC'],
          ['DDDD', 'EEEE', 'FFFF'],
          ['GGGG', 'HHHH', 'IIII'],
          ['JJJJ', 'KKKK', 'LLLL']]

list2 = [['AAAA', 'DDDD', 'GGGG', 'JJJJ'], 
         ['BBBB', 'EEEE', 'HHHH', 'KKKK'], 
         ['CCCC', 'FFFF', 'IIII', 'LLLL']]

Upvotes: 0

Related Questions