2013-07-17 34 views
0

我有一个列表列表。每个子列表都包含三个字符串。将列表中的字符串列表转换为整数和浮点列表的列表理解

bins = [['1', '2', '3.5'], ['4', '5', '6.0']] 

我需要将它转换成列表的列表,其中每个子列表包含两个整数和一个浮点数。我沿线的思维列表理解的:

[ [int(start), int(stop), float(value)] for bn in bins for [start, stop, value] in bn] 
+0

当然......你的想法看起来像会起作用......这里的问题是什么? –

回答

4

你接近:

[[int(start), int(stop), float(value)] for start, stop, value in bins] 

你不需要bn变量来保存每个bin或一个循环遍历其内容;每个bin可以直接解压到三个变量中。

+0

Doh!回想起来很明显:-S – BioGeek

0

另一种选择是使用map

>>> bins = [['1', '2', '3.5'], ['4', '5', '6.0']] 
>>> map(lambda x: [int(x[0]), int(x[1]), float(x[2])], bins) 
[[1, 2, 3.5], [4, 5, 6.0]] 
相关问题