-2

I have this:

[[['0'], ['0'], ['0'], ['0'], ['0'], ['0.0178885743420442']], [['0'], ['0'], ['0'], ['0'], ['0.00189664199330356'], ['0.0105739740488949']], [['0'], ['0'], ['0'], ['0'], ['0'], ['0.0131745569045574']]] 

Finally, i want to get:

[['0', '0', '0', '0', '0', '0.0178885743420442'], ['0', '0', '0', '0', '0.00189664199330356', '0.0105739740488949'], ['0', '0', '0', '0', '0', '0.0131745569045574']]

I tried to use join but nothing happend thanks

Mei Olya
  • 13
  • 4
  • Can you show what you have done? – DavidR Jan 05 '16 at 05:01
  • 1
    You are looking to flatten the list and there are many example on SO of flattening, e.g. http://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-python – AChampion Jan 05 '16 at 05:02
  • No, there is an error message: sequence item 0: expected string, list found – Mei Olya Jan 05 '16 at 05:03
  • `join` will only work on a iterable containing strings. It wont work on an iterable containing lists. It's basically a string concatenation function. – Paul Rooney Jan 05 '16 at 05:33
  • @MeiOlya, you posted a different input and output originally. After a solution was posted, you edited the entire input and output invalidating all the posted solution. – helloV Jan 05 '16 at 05:58

4 Answers4

4
>>> from itertools import chain
>>> list(chain(*mylist))
['0', '0', '0', '0', '0', '0.0216939116219239', '0.44748960163182', '1.47290267132899', '2.34321826311047', '2.99520324761532', '3.11538509056595', '3.46600327678834', '2.80910123913849']
helloV
  • 50,176
  • 7
  • 137
  • 145
  • Before anyone downvotes this answer: The person asked a different question initially and this answer was for that question. Now the question itself is changed and this answer will not answer the new question. – helloV Jan 05 '16 at 06:00
2

Simply do a list comprehension:

x = [['0'], ['0'], ['0'], ['0'], ['0'], ['0.0216939116219239'], ['0.44748960163182'], ['1.47290267132899'], ['2.34321826311047'], ['2.99520324761532'], ['3.11538509056595'], ['3.46600327678834'], ['2.80910123913849']]
print [j for i in x for j in i]

Output:

['0', '0', '0', '0', '0', '0.0216939116219239', '0.44748960163182', '1.47290267132899', '2.34321826311047', '2.99520324761532', '3.11538509056595', '3.46600327678834', '2.80910123913849']

Or

print [ast.literal_eval(j) for i in x for j in i]  # for list of floats and integers

Output:

[0, 0, 0, 0, 0, 0.0216939116219239, 0.44748960163182, 1.47290267132899, 2.34321826311047, 2.99520324761532, 3.11538509056595, 3.46600327678834, 2.80910123913849]
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
vks
  • 67,027
  • 10
  • 91
  • 124
  • Hi, I don't understand what is "ast.literal_eval(j) – Mei Olya Jan 05 '16 at 05:00
  • `ast.literal_eval` safely evaluates a python expression (literal), and is useful for turning a string into a native type, e.g. `ast.literal_eval('0')` will return an `int`, where as `ast.literal_eval('0.0216939116219239')` will return a `float`. – AChampion Jan 05 '16 at 05:07
  • @MeiOlya see above comment – vks Jan 05 '16 at 05:19
0

this would work (picks up first element of every sub-list):

[x[0] for x in list]
Ayush
  • 3,695
  • 1
  • 32
  • 42
  • I have in reality a list like this: [[['0'], ['0'], ['0'], ['0'], ['0'], ['0.0178885743420442']], [['0'], ['0'], ['0'], ['0'], ['0.00189664199330356'], ['0.0105739740488949']], [['0'], ['0'], ['0'], ['0'], ['0'], ['0.0131745569045574']]] Using a loop for, I can't made it with your proposition, isn't it ? – Mei Olya Jan 05 '16 at 05:11
  • `>>> [x[0] for y in list for x in y]` – Ayush Jan 05 '16 at 05:35
  • I want to have [['0', '0', '0', '0', '0', '0.0178885743420442'], ['0', '0', '0', '0', '0.00189664199330356', '0.0105739740488949'], ['0', '0', '0', '0', '0', '0.0131745569045574']] but with your code all lists are gone – Mei Olya Jan 05 '16 at 05:45
0

You could use a Python list comprehension combined with the chain.from_iterable function from itertools as follows:

from itertools import chain

data = [[['0'], ['0'], ['0'], ['0'], ['0'], ['0.0178885743420442']], [['0'], ['0'], ['0'], ['0'], ['0.00189664199330356'], ['0.0105739740488949']], [['0'], ['0'], ['0'], ['0'], ['0'], ['0.0131745569045574']]]

output = [list(chain.from_iterable(x)) for x in data]
print output

The combines each of the sub-sub-entries into sub-entries.

Giving you the desired output as follows:

[['0', '0', '0', '0', '0', '0.0178885743420442'], ['0', '0', '0', '0', '0.00189664199330356', '0.0105739740488949'], ['0', '0', '0', '0', '0', '0.0131745569045574']]
Martin Evans
  • 45,791
  • 17
  • 81
  • 97