2 回答
TA贡献1786条经验 获得超11个赞
您可能正在寻找formatstring 方法或(自 Python 3.6 起)f 字符串。
"Member present: {person}".format(person)
使用前者,
f"Member present: {person}"
就是后者怎么写。所以总的来说你可以写:
for person in visitors:
if person in members:
print(f"Member present: {person}")
break
else:
print("No members visited")
TA贡献1829条经验 获得超9个赞
您可以迭代两个列表以获取两个列表中都存在的成员,然后将其包装在函数中
members = ['Alex', 'Danny', 'Kieran', 'Zoe', 'Caroline']
visitors = ['Scott', 'Helen', 'Raj', 'Danny']
def member_name(members, visitors):
return [x for x in members if x in visitors]
result = member_name(members, visitors)
for member in result:
print("Member present: {}".format({member}))
输出:
Member present: {'Danny'}
添加回答
举报