I have the following matrix:
matrix= [[' 0.9111 0.9082 0.9151 0.9023 0.9019 0.9106'],
[' 0.7488 114*0 0.7646 0.7594 0.7533 117*0/'],
[]]
I am trying to convert it to the following form:
[['0.9111', '0.9082', '0.9151', '0.9023', '0.9019', '0.9106'],
['0.7488', '114*0', '0.7646', '0.7594', '0.7533', '117*0']]
I tried different functions like:
list(zip((row.split() for row in matrix)))
updated_matrix = [x.strip() for x in matrix]
You need to filter out empty lists and split the 1st item in sublists:
matrix= [[' 0.9111 0.9082 0.9151 0.9023 0.9019 0.9106'], [' 0.7488 1140 0.7646 0.7594 0.7533 1170/'], []]
matrix = [m[0].rstrip('/').split() for m in matrix if m]
print(matrix)
[['0.9111', '0.9082', '0.9151', '0.9023', '0.9019', '0.9106'], ['0.7488', '1140', '0.7646', '0.7594', '0.7533', '1170']]
Related
How do I split strings in a list without resulting in a 2-D list?
list1:['Ben&Jerry', 'Julia', 'Sally&Don', 'Tom', 'Tracy&Jim']
to output:
['Ben','Jerry','Julia','Sally', 'Don','Tom','Tracy','Jim']
using
flat_list=[s.split('&') if "&" in s else s for s in list1 ]
will give me a 2-D list.
Lazy way:
flat_list = '&'.join(list1).split('&')
This should work
[i for j in [i.split('&') for i in list1] for i in j]
Important context: Dig on the esolang wiki
I am making an compiler for a esoteric programming language, using a 2d list to account for the language’s 2d nature. The problem comes when I need all the lists in the one mega list to be of same length.
This:
[[“#”,”#”],[“#”,”#”,”#”]]
Needs be this:
[[“#”,”#”,” “],[“#”,”#”,”#”]]
Thanks!
>>> mega_list = [["#","#"],["#","#","#"]]
>>> for a in mega_list:
... a.extend([" "] * (max(map(len, mega_list)) - len(a)))
...
>>> mega_list
[['#', '#', ' '], ['#', '#', '#']]
To apply fillvalues to uneven lists, use the itertools.zip_longest function.
import itertools as it
lists = [[1,2,3],[4,5]]
lists = list(zip(*it.zip_longest(*lists,fillvalue=' ')))
print(lists)
You can do it like this, finding the max length of the lists first:
max_length = max( len(i) for i in l)
[ i + [" "]*(max_length-len(i)) for i in l ]
I don't know if this is possible and so far I haven't found a way to do it. I have a list of lists that contain names. Some lists have 2 elements and others have 3 [['Doe', 'John'], ['Doe', 'Jr.,', 'John']]. Is there a way to combine the indexes inside a list of lists. I can do it with a single list, but I get an error saying TypeError: sequence item 0: expected str instance, list found when I try it on a list of lists. I was trying to do something like this:
while(len(name_list) > 2:
indices = [0, 1]
name_list = ['Doe', 'Jr.,', 'John']
join_index = ' '.join([e for i, e in enumerate(name_list) if i in indices])
print(join_index)
So it appears you are trying to combine the names to produce (last, first) from something more complex.
This code will combine all but the last element of the sub-list:
Code:
names = [['Doe', 'John'], ['Doe', 'Jr.,', 'John']]
print(names)
new_list = [[' '.join(n[:-1]), n[-1]] for n in names]
print(new_list)
Results:
[['Doe', 'John'], ['Doe', 'Jr.,', 'John']]
[['Doe', 'John'], ['Doe Jr.,', 'John']]
Here is the final answer
length = (len(names))
j=0
while j < length:
if len(names[j]) > 3:
names[j][0] = ''.join((names[j][0], ' ', names[j][1]))
del(names[j][1])
#print(names[j])
j += 1
else:
#print('3 or less')
j += 1
My sample data looks like:
list1 = ['AAAABBBBCCCC','DDDDEEEEFFFF','GGGGHHHHIIII','JJJJKKKKLLLL']
Make a list1b such that each element is split into groups of four
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?
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']]
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']]
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)
How do you turn a list of strings into a list of sublist of strings?
For example:
List_of_Strings = ['abc','def','ghi']
Desired Output:
[['abc'],['def'],['ghi']]
My hack to get it is:
List_of_Sublist_of_Strings = [(str(x)+",").split(",") for x in List_of_Strings]
Produces:
[['abc', ''], ['def', ''], ['ghi', '']]
This produces an unwanted empty item in the sublists, but perhaps it's not possible to create a list of sublists in which the sublists only have one item.
You need to put those strings in [] there, and it's done.
>>> lis = ['abc','def','ghi']
>>> [[x] for x in lis]
[['abc'], ['def'], ['ghi']]
Use a list comprehension like so:
>>> lst = ['abc','def','ghi']
>>> [[x] for x in lst]
[['abc'], ['def'], ['ghi']]
>>>
Putting x in [] places it in a list of its own.