假设列表太多:
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,]
如何做到更聪明?