在一个循环中有一个循环,如果满足条件,则会运行一些代码。但是,如果不满足条件,则需要打印某些内容。尽管如果我将代码添加到任何位置,则会多次打印。如果不满足条件,如何只打印一次?
some_list = {'a_list': [{'name': 'Tom', 'age': 25}, {'name': 'John', 'age': 25}, {'name': 'Val', 'age': 25}], 'b_list': [{'name': 'Don', 'age': 25}, {'name': 'Tim', 'age': 25}, {'name': 'San', 'age': 25}]}
findperson = 'San'
for i in some_list:
for y in some_list[i]:
if y['name'].lower() == findperson.lower():
print('Friend found')
break
else:
print('Friend not found')
答案 0 :(得分:5)
您可以将any
用于内部循环(而将break
用于外部循环)...
for i in some_list:
if any(y['name'].lower() == findperson.lower() for y in some_list[i]):
print('Friend found')
break
else:
print('Friend not found')
...甚至整个事情:
if any(y['name'].lower() == findperson.lower()
for i in some_list for y in some_list[i]):
print('Friend found')
else:
print('Friend not found')
如果您还需要真正的朋友,则可以使用next
:
for i in some_list:
friend = next((y for y in some_list[i] if y['name'].lower() == findperson.lower()), None)
if friend is not None:
print('Friend found:', friend)
break
else:
print('Friend not found')
还可以与嵌套生成器一起使用,例如上面的any
:
friend = next((y for i in some_list for y in some_list[i]
if y['name'].lower() == findperson.lower()),
None)
if friend is not None:
print('Friend found:', friend)
else:
print('Friend not found')
答案 1 :(得分:2)
除了使用标志和break
(只会破坏内部循环)之外,另一个可能的解决方案是使用函数。这样,您可以简单地使用return
。一旦找到匹配项,这还具有停止搜索的优势。
some_list = {'a_list': [{'name': 'Tom', 'age': 25}, {'name': 'John', 'age': 25}, {'name': 'Val', 'age': 25}],
'b_list': [{'name': 'Don', 'age': 25}, {'name': 'Tim', 'age': 25}, {'name': 'San', 'age': 25}]}
search_name = 'San'
def find_person(data, name_to_find):
for i in data:
for y in data[i]:
if y['name'].lower() == name_to_find.lower():
print('Friend found')
return
print('Friend not found')
find_person(some_list, search_name)
答案 2 :(得分:1)
尝试一下:
some_list = {'a_list': [{'name': 'Tom', 'age': 25}, {'name': 'John', 'age': 25}, {'name': 'Val', 'age': 25}], 'b_list': [{'name': 'Don', 'age': 25}, {'name': 'Tim', 'age': 25}, {'name': 'San', 'age': 25}]}
findperson = 'San'
found = False
for i in some_list:
for y in some_list[i]:
if y['name'].lower() == findperson.lower():
print('Friend found')
found = True
if found:
break
if not found:
print('Friend not found')
答案 3 :(得分:1)
for else
是具有break
条件的一个很好的试用版,但是它仅在您获得1-for循环时有效,但是在这里您获得2-for循环时,可以使用标志:
some_list = {'a_list': [{'name': 'Tom', 'age': 25}, {'name': 'John', 'age': 25}, {'name': 'Val', 'age': 25}], 'b_list': [{'name': 'Don', 'age': 25}, {'name': 'Tim', 'age': 25}, {'name': 'San', 'age': 25}]}
findperson = 'San'
found = False
for i in some_list:
for y in some_list[i]:
if y['name'].lower() == findperson.lower():
print('Friend found')
found = True
break
if found:
break
if not found:
print('Friend not found')
答案 4 :(得分:1)
最好的imo选项是将嵌套的for循环放入函数中,并在找到朋友时返回,但是如果由于某种原因这不可能,则可以在发现嵌套时过早地结束嵌套的for循环
some_list = {'a_list': [{'name': 'Tom', 'age': 25}, {'name': 'John', 'age': 25}, {'name': 'Val', 'age': 25}],
'b_list': [{'name': 'Don', 'age': 25}, {'name': 'Tim', 'age': 25}, {'name': 'San', 'age': 25}]}
findperson = 'San'
for i in some_list:
for y in some_list[i]:
if y['name'].lower() == findperson.lower():
print('Friend found')
break
else:
continue
break
else:
print('Friend not found')