[英]Populate list or tuple from callable or lambda in python
This is a problem I've come across a lot lately. 这是我最近遇到的一个问题。 Google doesn't seem to have an answer so I bring it to the good people of stack overflow. 谷歌似乎没有答案所以我把它带给堆栈溢出的好人。
I am looking for a simple way to populate a list with the output of a function. 我正在寻找一种使用函数输出填充列表的简单方法。 Something like this: 像这样的东西:
fill(random.random(), 3) #=> [0.04095623, 0.39761869, 0.46227642]
Here are other ways I've found to do this. 以下是我发现的其他方法。 But I'm not really happy with them, as they seem inefficient. 但我对它们并不满意,因为它们看起来效率低下。
results = []
for x in xrange(3): results.append(random.random())
#results => [0.04095623, 0.39761869, 0.46227642]
and 和
map(lambda x: random.random(), [None] * 3)
#=> [0.04095623, 0.39761869, 0.46227642]
Suggestions? 建议?
Thanks for all the answers. 感谢所有的答案。 I knew there was a more python-esque way. 我知道有一种更蟒蛇式的方式。
And to the efficiency questions... 对效率问题......
$ python --version
Python 2.7.1+
$ python -m timeit "import random" "map(lambda x: random.random(), [None] * 3)"
1000000 loops, best of 3: 1.65 usec per loop
$ python -m timeit "import random" "results = []" "for x in xrange(3): results.append(random.random())"
1000000 loops, best of 3: 1.41 usec per loop
$ python -m timeit "import random" "[random.random() for x in xrange(3)]"
1000000 loops, best of 3: 1.09 usec per loop
How about a list comprehension ? 列表理解怎么样?
[random.random() for x in xrange(3)]
Also, in many cases, you need the values just once. 此外,在许多情况下,您只需要一次值。 In these cases, a generator expression which computes the values just-in-time and does not require a memory allocation is preferable: 在这些情况下,最好是生成器表达式 ,它只是及时计算值并且不需要内存分配:
results = (random.random() for x in xrange(3))
for r in results:
...
# results is "used up" now.
# We could have used results_list = list(results) to convert the generator
By the way, in Python 3.x, xrange
has been replaced by range
. 顺便说一句,在Python 3.x中, xrange
已被range
所取代。 In Python 2.x, range
allocates the memory and calculates all values beforehand (like a list comprehension), whereas xrange
calculates the values just-in-time and does not allocate memory (it's a generator). 在Python 2.x中, range
分配内存并预先计算所有值(如列表推导),而xrange
只是及时计算值并且不分配内存(它是生成器)。
why do you think they are inefficient? 为什么你认为他们效率低下?
There is another way to do it,a list-comprehension 还有另一种方法,列表理解
listt= [random.random() for i in range(3)]
list = [random.random() for i in xrange(3)]
list = [random.random() for i in [0]*3]
list = [i() for i in [random.random]*3]
Or : 要么 :
fill =lambda f,n: [f() for i in xrange(n)]
fill(random.random , 3 ) #=> [0.04095623, 0.39761869, 0.46227642]
something more generic... 更通用的东西......
from random import random
fill = lambda func, num: [func() for x in xrange(num)]
# for generating tuples:
fill = lambda func, num: (func() for x in xrange(num))
# then just call:
fill(random, 4)
# or...
fill(lambda : 1+2*random(), 4)
List comprehension is probably clearest, but for the itertools
afficionado: 列表理解可能是最清晰的,但对于itertools
afficionado:
>>> list(itertools.islice(iter(random.random, None), 3))
[0.42565379345946064, 0.41754360645917354, 0.797286438646947]
A quick check with timeit
shows that the itertools version is ever so slightly faster for more than 10 items, but still go with whatever seems clearest to you: 使用timeit
快速检查表明,对于超过10个项目,itertools版本的速度要快得多,但仍然可以使用对您来说最清晰的内容:
C:\Python32>python lib\timeit.py -s "import random, itertools" "list(itertools.islice(iter(random.random, None), 10))"
100000 loops, best of 3: 2.93 usec per loop
C:\Python32>python lib\timeit.py -s "import random, itertools" "[random.random() for _ in range(10)]"
100000 loops, best of 3: 3.19 usec per loop
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.