2013-07-17 32 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]] 
相關問題