Python 如何向不在列表中的人打印邮件

Python 如何向不在列表中的人打印邮件,python,Python,如果人们不在字典中,我想提醒他们进行调查,对于字典中的人,我会打印“谢谢”。您有两个列表,每个列表有一条消息。只需使用两个循环: favourite_languages = { "jen": "python", "sarah": "c", "edward": "ruby", "phil": "python"

如果人们不在字典中,我想提醒他们进行调查,对于字典中的人,我会打印“谢谢”。您有两个列表,每个列表有一条消息。只需使用两个循环:

favourite_languages = {
    "jen": "python",
    "sarah": "c",
    "edward": "ruby",
    "phil": "python",
}

people = ["adam", "bavi", "phil"]

for name in favourite_languages.keys():
    print(name.title() + " thanks for doing the poll")
else:
    print(people[:] + " please do the poll")
输出:

for name in favourite_languages.keys():
    print(name + " thanks for doing the poll")

for name in people:
    print(name + " please do the poll")
req_set = {x for x in (list(favourite_languages) + people)}

for name in req_set:
    if name in favourite_languages:
        print(f"{name.title()}, thanks for doing the poll")
    else:
        print(f"{name.title()}, please do the poll")

req_set = {x for x in (list(favourite_languages) + people)}

not_done = []
for name in req_set:
    if name in favourite_languages:
        print(f"{name.title()}, thanks for doing the poll")
    else:
        not_done.append(name)

print() # Prints an empty line to seperate the done and not done
for name in not_done:
    print(name.title(), end=", ")
print("please do the poll")
对于列表中的名称(set(list(favorite_languages.keys())+people)):#删除重复项
如果名称使用常用语言。键():
打印(f“{name.title()},感谢您进行投票”)
其他:
打印(f“{name.title()},请进行投票”)

您需要检查存在的所有名称,并对其进行迭代。然后,您可以检查
name
是否位于
people
中。如果是,那么你可以要求他们进行投票。否则,请感谢他们。

就我个人而言,我已经将这些名称添加到您的词典中,并为该语言指定一个空字符串。然后您可以执行以下操作:

jen thanks for doing the poll
sarah thanks for doing the poll
edward thanks for doing the poll
phil thanks for doing the poll
adam please do the poll
bavi please do the poll
phil please do the poll
编辑: 如果名称重复,则更新

favourite_languages = {
    "jen": "python",
    "sarah": "c",
    "edward": "ruby",
    "phil": "python",
}

people = ["adam", "bavi", "phil"]

for name in people: favourite_languages[name] = ''

for name, language in favourite_languages.items():
    print(name.title() + " thanks for doing the poll") if language else print(name + " please do the poll")
使用此代码,您的输出将产生

favourite_languages = {
    "jen": "python",
    "sarah": "c",
    "edward": "ruby",
    "phil": "python"
}

people = ["adam", "bavi", "phil"]

for name in people:
    if name in favourite_languages.keys():
        print(name + " thanks for doing the poll")
    else:
        print(name + " please do the poll")

我们可以使用集合来防止相同值的重复

adam please do the poll
bavi please do the poll       
phil thanks for doing the poll
输出:

for name in favourite_languages.keys():
    print(name + " thanks for doing the poll")

for name in people:
    print(name + " please do the poll")
req_set = {x for x in (list(favourite_languages) + people)}

for name in req_set:
    if name in favourite_languages:
        print(f"{name.title()}, thanks for doing the poll")
    else:
        print(f"{name.title()}, please do the poll")

req_set = {x for x in (list(favourite_languages) + people)}

not_done = []
for name in req_set:
    if name in favourite_languages:
        print(f"{name.title()}, thanks for doing the poll")
    else:
        not_done.append(name)

print() # Prints an empty line to seperate the done and not done
for name in not_done:
    print(name.title(), end=", ")
print("please do the poll")
如果你想知道最后没有做过民意调查的人的名字

Jen, thanks for doing the poll
Sarah, thanks for doing the poll
Edward, thanks for doing the poll
Adam, please do the poll
Bavi, please do the poll
Phil, thanks for doing the poll
输出:

for name in favourite_languages.keys():
    print(name + " thanks for doing the poll")

for name in people:
    print(name + " please do the poll")
req_set = {x for x in (list(favourite_languages) + people)}

for name in req_set:
    if name in favourite_languages:
        print(f"{name.title()}, thanks for doing the poll")
    else:
        print(f"{name.title()}, please do the poll")

req_set = {x for x in (list(favourite_languages) + people)}

not_done = []
for name in req_set:
    if name in favourite_languages:
        print(f"{name.title()}, thanks for doing the poll")
    else:
        not_done.append(name)

print() # Prints an empty line to seperate the done and not done
for name in not_done:
    print(name.title(), end=", ")
print("please do the poll")

只有在for循环中没有调用break时,才会到达else子句,但是people中的名字不是用最喜欢的语言表示的;我把答案烧成灰烬,做了一个合适的答案。嗨,我试过你的代码,它只对人中的人有效。如果我想感谢我字典里的那些。我该怎么做呢。谢谢,它确实在dict中进行了迭代。请解释它对您不起作用的地方。如果您注意到我的dict中的姓是phil,并且此代码的输出是phil,请进行轮询。谢谢您的帮助。我能知道添加一个空字符串有什么用吗?你也可以
None
,但是因为这个想法是存储包含语言的字符串,所以我决定选择一个空字符串。此外,python将空字符串计算为
False
,因此在
for循环中,如果
if language
可以执行
if language
并打印
,如果
language
包含某些内容,感谢您执行轮询,或者如果字符串为空,请执行
轮询。虽然为了公平起见,python对
None
的求值也为
False
,因此您也可以使用
None
。这不太有效:它用一个空条目覆盖了
phil
的语言首选项。我没有看到,我会更新。谢谢您的帮助。输出为phil将被要求进行民意测验。有什么解决办法吗?你说的是什么意思?菲尔会被要求做这个调查,而不是感谢菲尔。如果我只想感谢他而不是让他做民意调查,那么有什么解决办法吗?@Anscom不幸的是,可怜的phil在名单和字典中都有。这是故意的还是偶然的?我故意添加它是为了测试它是否已经在dict中,我如何才能排除phil。@12944qwerty是的,它确实是部分答案。这并不感谢字典里的其他人