-3
a = [['0.15592', '0.28075'],
     ['0.36807889', '0.35', '0.57681501876'],
     ['0.21342619', '0.0519085', '0.042', '0.27', '0.50620017', '0.528'],
     ['0.2094294', '0.1117', '0.53012', '0.3729850', '0.39325246'],
     ['0.21385894', '0.3464815', '0.57982969', '0.10262264'],
     ['0.29584013', '0.17383923']]

I want to change it to:

t = [0.15592, 0.28075, 0.36807889, 0.35, 0.57681501876, 0.21342619,
     0.0519085, 0.042, 0.27, 0.50620017, 0.528, 0.2094294, 0.1117,
     0.53012, 0.3729850, 0.39325246, 0.21385894, 0.3464815, 0.57982969,
     0.10262264, 0.29584013, 0.17383923]

New beginner, Thanks a lot!

Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127

3 Answers3

3

Try this:

t = [float(u) for s in a for u in s]

This assumes that the list is two deep. It flattens the list and converts the numbers to floats. This uses a list comprehension, which is more efficient than explicitly iterating and appending.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
1

You can use itertools.chain

from itertools import chain
print map(float,list(chain(*a)))
Ahsanul Haque
  • 10,676
  • 4
  • 41
  • 57
0

Try this

t = []
for i in a:
    for j in i:
        t.append(float(j))
print t
cjahangir
  • 1,773
  • 18
  • 27