-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ä
  • 45,748
  • 6
  • 100
  • 105

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
  • 20,042
  • 2
  • 16
  • 36
1

You can use itertools.chain

from itertools import chain
print map(float,list(chain(*a)))
Ahsanul Haque
  • 9,865
  • 4
  • 35
  • 51
0

Try this

t = []
for i in a:
    for j in i:
        t.append(float(j))
print t
cjahangir
  • 1,683
  • 15
  • 23