This question already has an answer here:
这个问题在这里已有答案:
- Making a flat list out of list of lists in Python 34 answers
- 在Python 34答案中列出列表中的平面列表
I am trying to write a function to take in any number of different lists or tuples as arguments and return one big list.
我正在尝试编写一个函数来接受任意数量的不同列表或元组作为参数并返回一个大列表。
def addify(*args):
big_list = list()
for iterable in args:
if isinstance(iterable, tuple):
big_list.extend(list(iterable))
else:
big_list.extend(iterable)
return big_list
>>> print addify((1,2,3), [2, 5, 3], (3,1,3), [3, 2344, 3])
[1, 2, 3, 2, 5, 3, 3, 1, 3, 3, 2344, 3]
I was learning about args and kwargs, and my code is working all right, but this seems like too much code for something so simple.
我正在学习args和kwargs,而且我的代码工作得很好,但这看起来像是太多代码了。
There must be a better way than writing a long function to check if an argument is a tuple and if it is add convert it to a list and then add it on. That just seems bloated.
必须有一个更好的方法,比编写一个long函数来检查参数是否为元组,如果是add,则将其转换为列表然后添加它。这看起来很臃肿。
2 个解决方案
#1
4
itertools.chain
is what you are looking for:
itertools.chain正是您要找的:
>>> from itertools import chain
>>> print list(chain((1,2,3), [2, 5, 3], (3,1,3), [3, 2344, 3]))
[1, 2, 3, 2, 5, 3, 3, 1, 3, 3, 2344, 3]
Note: Calling list
is necessary if you want a list instead of a itertools.chain
object.
注意:如果需要列表而不是itertools.chain对象,则需要调用列表。
#2
3
This functionality is already included in Python via itertools.chain
:
这个功能已经通过itertools.chain包含在Python中:
>>> from itertools import chain
>>> def addify(*args):
... return list(chain(*args))
...
>>> addify((1,2,3), [2, 5, 3], (3,1,3), [3, 2344, 3])
[1, 2, 3, 2, 5, 3, 3, 1, 3, 3, 2344, 3]
That, or as @Navith said, you could use itertools.chain.from_iterable
:
那,或者@Navith说,你可以使用itertools.chain.from_iterable:
>>> from itertools import chain
>>> def addify(*args):
... return list(chain.from_iterable(args))
...
>>> addify((1,2,3), [2, 5, 3], (3,1,3), [3, 2344, 3])
[1, 2, 3, 2, 5, 3, 3, 1, 3, 3, 2344, 3]
Note however that you need to call list()
on both because they return iterators by default:
但请注意,您需要在两者上调用list(),因为它们默认返回迭代器:
>>> chain((1,2,3), [2, 5, 3], (3,1,3), [3, 2344, 3])
<itertools.chain object at 0x04829390>
>>> chain.from_iterable([(1,2,3), [2, 5, 3], (3,1,3), [3, 2344, 3]])
<itertools.chain object at 0x048293D0>
Which might actually be a good thing if you do not need all of the items right up front.
如果您不需要所有项目,这可能实际上是一件好事。
#1
4
itertools.chain
is what you are looking for:
itertools.chain正是您要找的:
>>> from itertools import chain
>>> print list(chain((1,2,3), [2, 5, 3], (3,1,3), [3, 2344, 3]))
[1, 2, 3, 2, 5, 3, 3, 1, 3, 3, 2344, 3]
Note: Calling list
is necessary if you want a list instead of a itertools.chain
object.
注意:如果需要列表而不是itertools.chain对象,则需要调用列表。
#2
3
This functionality is already included in Python via itertools.chain
:
这个功能已经通过itertools.chain包含在Python中:
>>> from itertools import chain
>>> def addify(*args):
... return list(chain(*args))
...
>>> addify((1,2,3), [2, 5, 3], (3,1,3), [3, 2344, 3])
[1, 2, 3, 2, 5, 3, 3, 1, 3, 3, 2344, 3]
That, or as @Navith said, you could use itertools.chain.from_iterable
:
那,或者@Navith说,你可以使用itertools.chain.from_iterable:
>>> from itertools import chain
>>> def addify(*args):
... return list(chain.from_iterable(args))
...
>>> addify((1,2,3), [2, 5, 3], (3,1,3), [3, 2344, 3])
[1, 2, 3, 2, 5, 3, 3, 1, 3, 3, 2344, 3]
Note however that you need to call list()
on both because they return iterators by default:
但请注意,您需要在两者上调用list(),因为它们默认返回迭代器:
>>> chain((1,2,3), [2, 5, 3], (3,1,3), [3, 2344, 3])
<itertools.chain object at 0x04829390>
>>> chain.from_iterable([(1,2,3), [2, 5, 3], (3,1,3), [3, 2344, 3]])
<itertools.chain object at 0x048293D0>
Which might actually be a good thing if you do not need all of the items right up front.
如果您不需要所有项目,这可能实际上是一件好事。