的接受在. PEP 448
_Python 3.5
例如:
>>> l1 = [1, 2, 3]
>>> l2 = [4, 5, 6]
# unpack both iterables in a list literal
>>> joinedList = [*l1, *l2]
>>> print(joinedList)
[1, 2, 3, 4, 5, 6]
问题:有没有办法用列表做类似的事情?
此代码不起作用:
SyntaxError:可迭代解包不能用于理解
# List of variable size
list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
joined_list = [*l for l in list_of_lists]
当然,您可以执行以下操作,但这看起来不那么优雅并且看起来效率不高:
# List of variable size
list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
joined_list = list()
for l in list_of_lists:
joined_list += l