python嵌套列表理解

我有一个这个列表:

l = [['40', '20', '10', '30'], ['20', '20', '20', '20', '20', '30', '20'], ['30', '20', '30', '50', '10', '30', '20', '20', '20'], ['100', '100'], ['100', '100', '100', '100', '100'], ['100', '100', '100', '100']] 

现在,我想要做的是将列表中的每个元素转换为浮动。 我的解决办法是:

 newList = [] for x in l: for y in x: newList.append(float(y)) 

但是,这可以使用嵌套的列表理解来完成,对吗?

我所做的是:

 [float(y) for y in x for x in l] 

但是结果却是2400和100。

任何解决scheme,一个解释将不胜感激。 谢谢!

下面是你如何做到这一点与嵌套列表理解:

 [[float(y) for y in x] for x in l] 

这会给你一个列表的列表,类似于你开始,除了浮动而不是string。 如果你想要一个平坦的列表,那么你将使用[float(y) for x in l for y in x]

 >>> l = [['40', '20', '10', '30'], ['20', '20', '20', '20', '20', '30', '20'], ['30', '20', '30', '50', '10', '30', '20', '20', '20'], ['100', '100'], ['100', '100', '100', '100', '100'], ['100', '100', '100', '100']] >>> new_list = [float(x) for xs in l for x in xs] >>> new_list [40.0, 20.0, 10.0, 30.0, 20.0, 20.0, 20.0, 20.0, 20.0, 30.0, 20.0, 30.0, 20.0, 30.0, 50.0, 10.0, 30.0, 20.0, 20.0, 20.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0] 

不知道你想要的输出是什么,但如果你使用列表理解,顺序遵循嵌套循环的顺序,你有倒退。 所以我得到了我想要的东西:

 [float(y) for x in l for y in x] 

原则是:使用相同的顺序将其作为嵌套循环写出。

 In [105]: l = [[[[[[1]]]]]] In [106]: for a in l: ...: for b in a: ...: for c in b: ...: for d in c: ...: for e in d: ...: for f in e: ...: print(f) 1 In [107]: [f for a in l for b in a for c in b for d in c for e in d for f in e] #Which can be written in single line as In [107]: [f for a in l for b in a for c in b for d in c for e in d for f in e] Out[107]: [1] 

是的,你可以用这样的代码来做到这一点:

 l = [[float(y) for y in x] for x in l] 

如果你不喜欢嵌套的列表parsing,你也可以使用map函数,

 >>> from pprint import pprint >>> l = l = [['40', '20', '10', '30'], ['20', '20', '20', '20', '20', '30', '20'], ['30', '20', '30', '50', '10', '30', '20', '20', '20'], ['100', '100'], ['100', '100', '100', '100', '100'], ['100', '100', '100', '100']] >>> pprint(l) [['40', '20', '10', '30'], ['20', '20', '20', '20', '20', '30', '20'], ['30', '20', '30', '50', '10', '30', '20', '20', '20'], ['100', '100'], ['100', '100', '100', '100', '100'], ['100', '100', '100', '100']] >>> float_l = [map(float, nested_list) for nested_list in l] >>> pprint(float_l) [[40.0, 20.0, 10.0, 30.0], [20.0, 20.0, 20.0, 20.0, 20.0, 30.0, 20.0], [30.0, 20.0, 30.0, 50.0, 10.0, 30.0, 20.0, 20.0, 20.0], [100.0, 100.0], [100.0, 100.0, 100.0, 100.0, 100.0], [100.0, 100.0, 100.0, 100.0]] 

由于我在这里稍晚,但我想分享如何实际列表理解特别是嵌套列表理解:

  New_list= [[float(y) for x in l] 

实际上是一样的:

 New_list=[] for x in l: New_list.append(x) 

现在嵌套的列表理解:

 [[float(y) for y in x] for x in l] 

与…相同;

 new_list=[] for x in l: sub_list=[] for y in x: sub_list.append(float(y)) new_list.append(sub_list) print(new_list) 

输出:

 [[40.0, 20.0, 10.0, 30.0], [20.0, 20.0, 20.0, 20.0, 20.0, 30.0, 20.0], [30.0, 20.0, 30.0, 50.0, 10.0, 30.0, 20.0, 20.0, 20.0], [100.0, 100.0], [100.0, 100.0, 100.0, 100.0, 100.0], [100.0, 100.0, 100.0, 100.0]] 

在我看来,最好的方法是使用python的itertools包。

 >>>import itertools >>>l1 = [1,2,3] >>>l2 = [10,20,30] >>>[l*2 for l in itertools.chain(*[l1,l2])] [2, 4, 6, 20, 40, 60] 

这个问题可以在不使用for循环的情况下解决。单行代码就足够了。 使用带lambda函数的嵌套地图也可以在这里使用。

l = [['40','20','10','30'],['20','20','20','20','20','30','20'] ['30','20','30','50','10','30','20','20','20'],['100','100'],[' '100','100','100','100','100'],['100','100','100','100']]

 map(lambda x:map(lambda y:float(y),x),l) 

和输出清单如下:

[[40.0,20.0,10.0,30.0],[20.0,20.0,20.0,20.0,20.0,30.0,20.0],[30.0,20.0,30.0,50.0,10.0,30.0,20.0,20.0,20.0],[ 100.0],[100.0,100.0,100.0,100.0,100.0],[100.0,100.0,100.0,100.0]]