Python 内联 for 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27411631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inline for loop
提问by Will
I'm trying to learn neat pythonic ways of doing things, and was wondering why my for loop cannot be refactored this way:
我正在尝试学习简洁的 Pythonic 做事方式,并且想知道为什么我的 for 循环不能以这种方式重构:
q = [1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5]
vm = [-1, -1, -1, -1]
for v in vm:
if v in q:
p.append(q.index(v))
else:
p.append(99999)
vm[p.index(max(p))] = i
I tried replacing the for loop with:
我尝试用以下方法替换 for 循环:
[p.append(q.index(v)) if v in q else p.append(99999) for v in vm]
But it doesn't work. The for v in vm:
loop evicts numbers from vm
based on when they come next in q
.
但它不起作用。该for v in vm:
循环由逐出数字vm
基于当他们来到下一对q
。
采纳答案by Will
What you are using is called a list comprehensionin Python, not an inline for-loop (even though it is similar to one). You would write your loop as a list comprehension like so:
您使用的在 Python 中称为列表推导式,而不是内联 for 循环(即使它类似于一个)。您可以将循环编写为列表推导式,如下所示:
p = [q.index(v) if v in q else 99999 for v in vm]
When using a list comprehension, you do not call list.append
because the list is being constructed from the comprehension itself. Each item in the list will be what is returned by the expression on the left of the for
keyword, which in this case is q.index(v) if v in q else 99999
. Incidentially, if you do use list.append
inside a comprehension, then you will get a list of None
values because that is what the append
method always returns.
使用列表推导式时,不要调用,list.append
因为列表是从推导式本身构造的。列表中的每一项都将是for
关键字左侧的表达式返回的内容,在本例中为q.index(v) if v in q else 99999
。顺便说一句,如果您确实list.append
在推导式中使用,那么您将获得一个None
值列表,因为这是该append
方法始终返回的内容。
回答by Padraic Cunningham
回答by Hackaholic
your list comphresnion will, work but will return list of None because append return None:
您的列表编译会起作用,但会返回 None 列表,因为 append 返回 None:
demo:
演示:
>>> a=[]
>>> [ a.append(x) for x in range(10) ]
[None, None, None, None, None, None, None, None, None, None]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
better way to use it like this:
像这样使用它的更好方法:
>>> a= [ x for x in range(10) ]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
回答by f.rodrigues
q = [1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5]
vm = [-1, -1, -1, -1,1,2,3,1]
p = []
for v in vm:
if v in q:
p.append(q.index(v))
else:
p.append(99999)
print p
p = [q.index(v) if v in q else 99999 for v in vm]
print p
Output:
输出:
[99999, 99999, 99999, 99999, 0, 1, 2, 0]
[99999, 99999, 99999, 99999, 0, 1, 2, 0]
Instead of using append()
in the list comprehension you can reference the p as direct output, and use q.index(v)
and 99999
in the LC.
append()
您可以将 p 作为直接输出引用,而不是在列表推导中使用,q.index(v)
并99999
在 LC 中使用和。
Not sure if this is intentional but note that q.index(v)
will find just the first occurrence of v
, even tho you have several in q
. If you want to get the index of all v
in q
, consider using a enumerator
and a list of already visited indexes
不知道这是故意的,但请注意,q.index(v)
会发现刚才的第一次出现v
,甚至尽管你有几个q
。如果要获取所有v
in的索引q
,请考虑使用 aenumerator
和已访问的列表indexes
Something in those lines(pseudo-code):
这些行中的内容(伪代码):
visited = []
for i, v in enumerator(vm):
if i not in visited:
p.append(q.index(v))
else:
p.append(q.index(v,max(visited))) # this line should only check for v in q after the index of max(visited)
visited.append(i)