如何将此for for循环写为map函数

时间:2018-04-19 18:44:24

标签: python loops

我读到for loops可以比map更有效地用于迭代。如何在此for loop上使用import time start = time.time() L = [] a = 5 b = 0 for i in range(1000): L.append(i) b +=a print(L) print(b) end = time.time() print(end-start)

LET $1 = SELECT expand(bothE('Begets')) from Creature where uniquename='asdfasdf';\
LET $2 = SELECT expand(bothE('Begets')) FROM Creature WHERE uniquename='adalgrimTook';\
LET $3 = SELECT INTERSECT($1, $2);\
LET $4 = CREATE EDGE Begets FROM (SELECT FROM Creature WHERE uniquename='asdfasdf') TO (SELECT FROM Creature WHERE uniquename='adalgrimTook');\
SELECT IF($3.INTERSECT.size() > 0, 'Already exists', $4)

1 个答案:

答案 0 :(得分:0)

如果确实想要使用map

L = []
a, b = 5, 0

list(map(L.append, range(1000)))
# or sum(map(bool, map(L.append, range(1000)))) - doesn't waste memory

b = a * len(L)

请注意,要调用L.append,您需要迭代覆盖map的输出,这是list和{{ 1}}在此代码中执行。

但是,在这种情况下,您不需要使用它。这样看起来效果更好:

sum

如果您使用的是Python 2,则可以执行L = list(range(1000))