[英]My loop is stopping after first iteration
I am trying to loop through a dictionary of lists and return the keys of values which contain a specific integer.我正在尝试遍历列表字典并返回包含特定整数的值的键。 For example if I loop through {0: [3], 1: [3], 2: [4, 0], 3: [1], 4: [1, 0, 2, 3]} with v = 4, it should return [2].
例如,如果我循环 {0: [3], 1: [3], 2: [4, 0], 3: [1], 4: [1, 0, 2, 3]} 且 v = 4,它应该返回 [2]。 However, my code seems to only consider the very first key value pair and I do not understand why.
但是,我的代码似乎只考虑第一个键值对,我不明白为什么。 It works if the integer is in the first key value pair and not in any others.
如果整数在第一个键值对中而不在任何其他键值对中,则它有效。 Here is the function I made:
这是我制作的功能:
def whence(g, v):
# Your code here
lov = []
count = 0
for key, value in g.items():
if v in value:
lov.append(count)
count += 1
print(lov)
return lov
You can do it in one line:您可以在一行中完成:
def whence(g, v):
return [key for key, values in g.items() if v in values]
The return statement indented too much. return 语句缩进太多。
def whence(g, v):
# Your code here
lov = []
count = 0
for key, value in g.items():
if v in value:
lov.append(count)
count += 1
print(lov)
return lov
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.