[英]List comprehension for turning a lists of lists of strings into a list of lists of ints and float
I have a list of lists. 我有一个清单清单。 The sublists each contain three strings.
子列表每个包含三个字符串。
bins = [['1', '2', '3.5'], ['4', '5', '6.0']]
I need to convert this into a lists of lists where each sublist consists of two integers and a float. 我需要将其转换为列表列表,其中每个子列表由两个整数和一个浮点数组成。 I was thinking of a list comprehension along the lines of:
我在考虑以下方面的列表理解:
[ [int(start), int(stop), float(value)] for bn in bins for [start, stop, value] in bn]
You're close: 您接近:
[[int(start), int(stop), float(value)] for start, stop, value in bins]
You don't need the bn
variable to hold each bin or a loop to iterate through its contents; 您不需要
bn
变量来保存每个bin或循环来遍历其内容; each bin can be unpacked directly into the three variables. 每个垃圾箱都可以直接解压缩为三个变量。
Another option is using map
另一种选择是使用
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]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.