我们如何从导入的模块中选择列表?

问题描述 投票:0回答:1

假设我做了以下事情:

from X import A, B, C, D

我想要做的是将 A、B、C、D 中的所有列表合并为一个大列表:

all_list=[]     
for each_list in (X.A, X.B, X.C, X.D combined):
    all_list.extend(each_list)

有没有一种简单的方法可以在Python中做到这一点?谢谢。

python loops directory iterator
1个回答
0
投票

使用 itertools.chain (https://docs.python.org/3/library/itertools.html#itertools.chain) 迭代多个可迭代对象。

© www.soinside.com 2019 - 2024. All rights reserved.