将多个列表中的元素集成到一个列表中

时间:2017-08-14 06:05:16

标签: python list

假设列表太多:

fred = ['one', 'two', 'three']
jim = [1, 2, 3]
# and many others

我想要['one', 'two', 'three', 1, 2, 3, ......]并尝试以下代码

dir()
output: ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'fred', 'jim']
multiple_list_names = [ e for e in dir() if "__" not in e]
output:['fred', 'jim']

然后,

new_list = []
for name in multiple_list_names:
    for elem in eval(name):
        new_list.append(elem)
print(new_list)
output:['one', 'two', 'three', 1, 2, 3,]

如何做到更聪明?

0 个答案:

没有答案