提问人:Boy Pasmo 提问时间:8/6/2013 最后编辑:Karl KnechtelBoy Pasmo 更新时间:1/27/2023 访问量:241365
如何使用列表推导式来处理嵌套列表?
How can I use list comprehensions to process a nested list?
问:
我有这个嵌套列表:
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']]
我想将每个元素转换为 .我有这个代码:l
float
newList = []
for x in l:
for y in x:
newList.append(float(y))
如何用嵌套列表推导来解决这个问题?
Смотритетакже: 如何从列表推导式而不是嵌套列表中获得平面结果?
答:
>>> 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 y in x] for x in l]
这将为您提供一个列表列表,类似于您开始时的列表,只是使用浮点数而不是字符串。
如果你想要一个平面列表,那么你可以使用
[float(y) for x in l for y in x]
请注意循环顺序 - 在此顺序中排在第一位。for x in l
是的,您可以使用这样的代码来完成:
l = [[float(y) for y in x] for x in l]
评论
[float(y) for y in x for x in l]
这将导致一堆 100 的总和为 2400。
如果你不喜欢嵌套列表推导式,你也可以使用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]]
评论
>>> float_l = [map(float, nested_list) for nested_list in l]
[[<map at 0x47be9b0>], [<map at 0x47be2e8>], [<map at 0x47be4a8>], [<map at 0x47beeb8>], [<map at 0x484b048>], [<map at 0x484b0b8>]]
>>> float_l = [list(map(float, nested_list)) for nested_list in l]
python3
在我看来,最好的方法是使用 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]
不确定您想要的输出是什么,但如果您使用的是列表推导式,则顺序遵循嵌套循环的顺序,这是向后的。所以我得到了我认为你想要的:
[float(y) for x in l for y in x]
原则是:使用与嵌套 for 循环相同的顺序。
评论
[float(y)]
这个问题可以在不使用 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, 100.0]]
评论
map
list(list( .. ) )
fluentpy
以下是将嵌套 for 循环转换为嵌套列表推导式的方法:
以下是嵌套列表推导的工作原理:
l a b c d e f
↓ ↓ ↓ ↓ ↓ ↓ ↓
In [1]: l = [ [ [ [ [ [ 1 ] ] ] ] ] ]
In [2]: 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(float(f))
...:
1.0
In [3]: [float(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[3]: [1.0]
对于您的情况,如果您想要一个平面列表,它将是这样的。
In [4]: new_list = [float(y) for x in l for y in x]
评论
(f(x) for x in l)
我想分享一下列表推导的实际工作原理,特别是对于嵌套列表推导:
new_list= [float(x) for x in l]
实际上等同于:
new_list=[]
for x in l:
new_list.append(float(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]]
我有一个类似的问题要解决,所以我遇到了这个问题。我对安德鲁·克拉克(Andrew Clark)和纳拉扬(narayan)的回答进行了性能比较,我想分享一下。
两个答案之间的主要区别在于它们如何遍历内部列表。其中一个使用内置地图,而另一个使用列表推导。如果 Map 函数不需要使用 lambdas,则与其等效列表推导相比,它具有轻微的性能优势。因此,在这个问题的上下文中,应该比列表理解稍微好一些。map
让我们做一个性能基准测试,看看它是否真的是真的。我使用 python 版本 3.5.0 来执行所有这些测试。在第一组测试中,我希望每个列表的元素保持为 10,列表数量从 10-100,000 不等
>>> python -m timeit "[list(map(float,k)) for k in [list(range(0,10))]*10]"
>>> 100000 loops, best of 3: 15.2 usec per loop
>>> python -m timeit "[[float(y) for y in x] for x in [list(range(0,10))]*10]"
>>> 10000 loops, best of 3: 19.6 usec per loop
>>> python -m timeit "[list(map(float,k)) for k in [list(range(0,10))]*100]"
>>> 100000 loops, best of 3: 15.2 usec per loop
>>> python -m timeit "[[float(y) for y in x] for x in [list(range(0,10))]*100]"
>>> 10000 loops, best of 3: 19.6 usec per loop
>>> python -m timeit "[list(map(float,k)) for k in [list(range(0,10))]*1000]"
>>> 1000 loops, best of 3: 1.43 msec per loop
>>> python -m timeit "[[float(y) for y in x] for x in [list(range(0,10))]*1000]"
>>> 100 loops, best of 3: 1.91 msec per loop
>>> python -m timeit "[list(map(float,k)) for k in [list(range(0,10))]*10000]"
>>> 100 loops, best of 3: 13.6 msec per loop
>>> python -m timeit "[[float(y) for y in x] for x in [list(range(0,10))]*10000]"
>>> 10 loops, best of 3: 19.1 msec per loop
>>> python -m timeit "[list(map(float,k)) for k in [list(range(0,10))]*100000]"
>>> 10 loops, best of 3: 164 msec per loop
>>> python -m timeit "[[float(y) for y in x] for x in [list(range(0,10))]*100000]"
>>> 10 loops, best of 3: 216 msec per loop
在下一组测试中,我想将每个列表的元素数增加到 100。
>>> python -m timeit "[list(map(float,k)) for k in [list(range(0,100))]*10]"
>>> 10000 loops, best of 3: 110 usec per loop
>>> python -m timeit "[[float(y) for y in x] for x in [list(range(0,100))]*10]"
>>> 10000 loops, best of 3: 151 usec per loop
>>> python -m timeit "[list(map(float,k)) for k in [list(range(0,100))]*100]"
>>> 1000 loops, best of 3: 1.11 msec per loop
>>> python -m timeit "[[float(y) for y in x] for x in [list(range(0,100))]*100]"
>>> 1000 loops, best of 3: 1.5 msec per loop
>>> python -m timeit "[list(map(float,k)) for k in [list(range(0,100))]*1000]"
>>> 100 loops, best of 3: 11.2 msec per loop
>>> python -m timeit "[[float(y) for y in x] for x in [list(range(0,100))]*1000]"
>>> 100 loops, best of 3: 16.7 msec per loop
>>> python -m timeit "[list(map(float,k)) for k in [list(range(0,100))]*10000]"
>>> 10 loops, best of 3: 134 msec per loop
>>> python -m timeit "[[float(y) for y in x] for x in [list(range(0,100))]*10000]"
>>> 10 loops, best of 3: 171 msec per loop
>>> python -m timeit "[list(map(float,k)) for k in [list(range(0,100))]*100000]"
>>> 10 loops, best of 3: 1.32 sec per loop
>>> python -m timeit "[[float(y) for y in x] for x in [list(range(0,100))]*100000]"
>>> 10 loops, best of 3: 1.7 sec per loop
让我们勇敢地迈出一步,将列表中的元素数修改为 1000
>>> python -m timeit "[list(map(float,k)) for k in [list(range(0,1000))]*10]"
>>> 1000 loops, best of 3: 800 usec per loop
>>> python -m timeit "[[float(y) for y in x] for x in [list(range(0,1000))]*10]"
>>> 1000 loops, best of 3: 1.16 msec per loop
>>> python -m timeit "[list(map(float,k)) for k in [list(range(0,1000))]*100]"
>>> 100 loops, best of 3: 8.26 msec per loop
>>> python -m timeit "[[float(y) for y in x] for x in [list(range(0,1000))]*100]"
>>> 100 loops, best of 3: 11.7 msec per loop
>>> python -m timeit "[list(map(float,k)) for k in [list(range(0,1000))]*1000]"
>>> 10 loops, best of 3: 83.8 msec per loop
>>> python -m timeit "[[float(y) for y in x] for x in [list(range(0,1000))]*1000]"
>>> 10 loops, best of 3: 118 msec per loop
>>> python -m timeit "[list(map(float,k)) for k in [list(range(0,1000))]*10000]"
>>> 10 loops, best of 3: 868 msec per loop
>>> python -m timeit "[[float(y) for y in x] for x in [list(range(0,1000))]*10000]"
>>> 10 loops, best of 3: 1.23 sec per loop
>>> python -m timeit "[list(map(float,k)) for k in [list(range(0,1000))]*100000]"
>>> 10 loops, best of 3: 9.2 sec per loop
>>> python -m timeit "[[float(y) for y in x] for x in [list(range(0,1000))]*100000]"
>>> 10 loops, best of 3: 12.7 sec per loop
从这些测试中,我们可以得出结论,在这种情况下,与列表理解相比,它具有性能优势。如果您尝试强制转换为 或 ,这也适用。对于每个列表元素较少的少量列表,差异可以忽略不计。对于每个列表包含更多元素的较大列表,可能希望使用列表推导而不是列表推导,但这完全取决于应用程序需求。map
int
str
map
但是,我个人认为列表理解比 更具可读性和惯用性。它是 python 中的事实标准。通常人们在使用列表理解方面比 .map
map
deck = []
for rank in ranks:
for suit in suits:
deck.append(('%s%s')%(rank, suit))
这可以使用列表推导来实现:
[deck.append((rank,suit)) for suit in suits for rank in ranks ]
评论
是的,您可以执行以下操作。
[[float(y) for y in x] for x in l]
如果需要扁平化列表:
[y for x in l for y in x]
如果需要嵌套列表(列表中的列表):
[[float(y) for y in x] for x in l]
评论
[float(y) for y in x for x in l]
本身不起作用。它之所以有效,只是因为以前代码中遗留的现有定义。x