如何在不使用任何for或while循环的情况下将列表列表打印到python中的单个列表中? [英] How to print list of list into one single list in python without using any for or while loop?
问题描述
今天我接受了一次采访,我被要求将列表列表打印到一个列表中而不使用任何for或while循环,但是你可以使用其他内置函数。
Today I had an interview and I was asked to print a list of list into one single list without using any for or while loop but you can use other built in function.
以下是列表:
>>> myList = [[[1,2,3],[4,5],[6,7,8,9]]]
>>> myList
[[[1, 2, 3], [4, 5], [6, 7, 8, 9]]]
>>>
输出将是 [1,2,3,4,5, 6,7,8,9]
。
任何想法如何解决这个问题?
Any idea how to go about this?
推荐答案
三个选项:
-
您可以对嵌套列表求和;
sum()
接受第二个参数,即起始值,将其设置为空列表:
You could sum the nested lists;
sum()
takes a second argument, a starting value, set that to an empty list:
>>> sum(myList[0], [])
[1, 2, 3, 4, 5, 6, 7, 8, 9]
这是有效的,因为 sum()
基本上是作为循环实现的:
This works because sum()
is essentially implemented as a loop:
def sum(values, start=0):
total = start
for value in values:
total += value
return total
与列表并置一起使用,前提是起始值是列表对象本身。 0 + [1,2,3]
不起作用,但 [] + [1,2,3]
工作得很好。
which works with list concatenation, provided the start value is a list object itself. 0 + [1, 2, 3]
would not work, but [] + [1, 2, 3]
works just fine.
你可以使用 reduce()
operator.addrel =nofollow> operator.add()
,与 sum()基本相同
,减去给出起始值的要求:
You could use reduce()
with operator.add()
, which is essentially the same as sum()
, minus the requirement to give a start value:
from operator import add
reduce(add, myList[0])
operator.add()
可替换为 lambda a,b:a + b
或 list .__ add __
如果要不惜一切代价避免进口。
operator.add()
could be replaced with lambda a, b: a + b
or with list.__add__
if imports are to be avoided at all cost.
随着嵌套输入列表的增长, operator.iadd()
(就地添加,对于列表相当于 list.extend()
)将迅速变得更快选项:
As the nested input list grows, operator.iadd()
(in-place add, for lists the equivalent of list.extend()
) will rapidly become a faster option:
from operator import iadd
reduce(add, myList[0], [])
但 需要一个空列表才能开始。
but this does need an empty list to start with.
您可以使用 itertools.chain.from_iterable()
:
You could chain the lists using itertools.chain.from_iterable()
:
>>> from itertools import chain
>>> list(chain.from_iterable(myList[0]))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
所有三个解决方案都要求您使用索引删除最外面的列表,尽管您也可以传递<1中的一个元素code> myList 作为 chain.from_iterable()
的单个参数,带有列表(chain.from_iterable(* myList))
以及。
All three solutions require that you use indexing to remove the outermost list, although you can also pass the one element in myList
as a single argument to chain.from_iterable()
with list(chain.from_iterable(*myList))
as well.
在这些选项中, reduce(add,...)
是最快的:
Of these options, reduce(add, ...)
is the fastest:
>>> timeit.timeit("sum(myList[0], [])", 'from __main__ import myList')
1.2761731147766113
>>> timeit.timeit("reduce(add, myList[0])", 'from __main__ import myList; from operator import add')
1.0545191764831543
>>> timeit.timeit("reduce(lambda a, b: a.extend(b) or a, myList[0], [])", 'from __main__ import myList')
2.225532054901123
>>> timeit.timeit("list(chain.from_iterable(myList[0]))", 'from __main__ import myList; from itertools import chain')
2.0208170413970947
并比较 iadd
与添加
:
>>> timeit.timeit("reduce(add, myList[0])", 'from __main__ import myList; from operator import add')
0.9298770427703857
>>> timeit.timeit("reduce(iadd, myList[0], [])", 'from __main__ import myList; from operator import iadd')
1.178157091140747
>>> timeit.timeit("reduce(add, myListDoubled)", 'from __main__ import myList; myListDoubled = myList[0] + myList[0]; from operator import add')
2.3597090244293213
>>> timeit.timeit("reduce(iadd, myListDoubled, [])", 'from __main__ import myList; myListDoubled = myList[0] + myList[0]; from operator import iadd')
1.730151891708374
您可以使用递归来避免使用循环,使其适用于任意嵌套列表:
You could use recursion to avoid using a loop, to make this work for arbitrarily nested lists:
def flatten(lst):
try:
return flatten(sum(lst, []))
except TypeError:
return lst
演示:
>>> flatten(myList)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> flatten(myList + myList)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9]
这篇关于如何在不使用任何for或while循环的情况下将列表列表打印到python中的单个列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!