Flattened Out A Mixed Type Of List
I have a mixed list that looked like this: [('1CFV',), 'NONRBP', [0.00325071141379, 0.278046326931, 0.291350892759]] It was created with this command: In [12]: l1 = [0.0032507114
Solution 1:
>>>final = []>>>a = [('1CFV',), 'NONRBP', [0.00325071141379, 0.278046326931, 0.291350892759]]>>>for i in a:...ifhasattr(i, '__iter__'):...for j in i:... final.append(j)...else:... final.append(i)...>>>print final
['1CFV', 'NONRBP', 0.00325071141379, 0.27804632693100001, 0.291350892759]
Solution 2:
how about
[item for item inlistiftype(item) isnotlist] +
[item for sublist in mixl iftype(item) islistfor item in sublist]
Solves the specific case where a list contains lists and other iterables (strings)
Post a Comment for "Flattened Out A Mixed Type Of List"