Python - 检查单词是否与IF语句的单词列表匹配

时间:2013-11-24 13:52:41

标签: python if-statement

我正在尝试使用IF检查特定字符串是否同时匹配几个单词。放入代码,就像这样:

if shippingDest == "Spain":
    shippingRegion = "Spain"
if shippingDest == "Portugal":
    shippingRegion = "Portugal"
if shippingDest == ["France", "Germany", "Luxembourg", "Belgium", "United Kingdom"]:
    shippingRegion = "Euro1"
if shippingDest == ["Albania", "Andorra", "Austria", "Belarus", "Bosnia and Herzegovina", "Bulgaria", "Croatia", "Czech Republic", "Denmark", "Estonia", "Faroe Islands", "Finland", "Georgia", "Gibraltar", "Greece", "Hongary", "Iceland", "Ireland", "Italy", "Latvia", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Malta", "Moldova", "Monaco", "Montenegro", "Netherlands", "Norway", "Poland", "Romania", "Russia", "San Marino", "Serbia", "Slovenia", "Svalbard and Jan Mayen", "Sweden", "Switzerland", "Ukraine", "Vatican City State"]:
    shippingRegion = "Euro2"
else:
    shippingRegion = "World"

例如,当使用德国作为国家/地区进行检查时,shippingRegion将为Euro1。但是,它需要它作为世界。

有人能指出我正确的方向,看一个单词是否与IF中的任何一个单词匹配?

谢谢,

7 个答案:

答案 0 :(得分:4)

==替换为in

if shippingDest in ["France", "Germany", "Luxembourg", "Belgium", "United Kingdom"]:

如果您在if之后用elif替换else if语句,那么您的代码也会更有效率。{/ 1}}。

另外,你可以像这样缩短它:

if shippingDest in ("France", "Germany", "Luxembourg", "Belgium", "United Kingdom"):
    shippingRegion = "Euro1"
elif shippingDest in ("Albania", "Andorra", "Austria", "Belarus", "Bosnia and Herzegovina", "Bulgaria", "Croatia", "Czech Republic", "Denmark", "Estonia", "Faroe Islands", "Finland", "Georgia", "Gibraltar", "Greece", "Hongary", "Iceland", "Ireland", "Italy", "Latvia", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Malta", "Moldova", "Monaco", "Montenegro", "Netherlands", "Norway", "Poland", "Romania", "Russia", "San Marino", "Serbia", "Slovenia", "Svalbard and Jan Mayen", "Sweden", "Switzerland", "Ukraine", "Vatican City State"):
    shippingRegion = "Euro2"
elif shippingDest in ("Portugal", "Spain"):
    shippingRegion = shippingDest:
else:
    shippingRegion = "World"

我还将您的列表转换为元组(将[]替换为())因为您以后不需要更改它们,因此使它们不可变意味着使用的内存更少。

答案 1 :(得分:2)

您需要使用elif和in来检查成员身份而不是==

if shippingDest == "Spain":
    shippingRegion = "Spain"
elif shippingDest == "Portugal":
    shippingRegion = "Portugal"
elif shippingDest in ["France", "Germany", "Luxembourg", "Belgium", "United Kingdom"]:
    shippingRegion = "Euro1"
elif shippingDest in ["Albania", "Andorra", "Austria", "Belarus", "Bosnia and Herzegovina", "Bulgaria", "Croatia", "Czech Republic", "Denmark", "Estonia", "Faroe Islands", "Finland", "Georgia", "Gibraltar", "Greece", "Hongary", "Iceland", "Ireland", "Italy", "Latvia", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Malta", "Moldova", "Monaco", "Montenegro", "Netherlands", "Norway", "Poland", "Romania", "Russia", "San Marino", "Serbia", "Slovenia", "Svalbard and Jan Mayen", "Sweden", "Switzerland", "Ukraine", "Vatican City State"]:
    shippingRegion = "Euro2"
else:
    shippingRegion = "World"

正在发生的事情是shippingDest设置正确,然后失败了最后一个if,所以执行了else子句,导致World

如果你有一个查找表,使用dict,它会更有意义,更易于维护:

shipping_destinations = {
    'Portugal': 'Portugal',
    'France': 'Euro1',
    'Monaco': 'Euro2',
    'spain': 'Spain'
}

然后,一旦列出,你可以这样做:

ship_to = shipping_destinations.get(country_name, 'World')

检索目的地或将其默认为世界。这样可以节省if语句,如果你有一个区域的电子表格,那么很容易填充...

答案 2 :(得分:2)

if shippingDest == ["France", "Germany", "Luxembourg", "Belgium", "United Kingdom"]:
    shippingRegion = "Euro1"

这看不到shippingDest是否是其中一个字符串,而是它会检查shippingDest是否等于该列表

相反,您可以使用:

if shippingDest in ["France", "Germany", "Luxembourg", "Belgium", "United Kingdom"]:
    shippingRegion = "Euro1"

由于前面的条件评估为false,因此else已执行。此外,对于互斥条件,您希望使用elif而不是一系列if语句,后跟其他语句。在您的代码中,else仅对应于前面的if

答案 3 :(得分:2)

您想使用关键字elif - 这与其他语言else if类似。

此外,您想测试字符串(“德国”)是否在列表中,请使用in关键字而不是==

答案 4 :(得分:2)

尝试in

if shippingDest in ["France", "Germany", "Luxembourg", "Belgium", "United Kingdom"]:
    shippingRegion = "Euro1"

答案 5 :(得分:1)

使用the in operator

if shippingDest == "Spain":
    shippingRegion = "Spain"
elif shippingDest == "Portugal":
    shippingRegion = "Portugal"
elif shippingDest in ["France", "Germany", "Luxembourg", "Belgium", "United Kingdom"]:
    shippingRegion = "Euro1"
elif shippingDest in ["Albania", "Andorra", "Austria", "Belarus", "Bosnia and Herzegovina", "Bulgaria", "Croatia", "Czech Republic", "Denmark", "Estonia", "Faroe Islands", "Finland", "Georgia", "Gibraltar", "Greece", "Hongary", "Iceland", "Ireland", "Italy", "Latvia", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Malta", "Moldova", "Monaco", "Montenegro", "Netherlands", "Norway", "Poland", "Romania", "Russia", "San Marino", "Serbia", "Slovenia", "Svalbard and Jan Mayen", "Sweden", "Switzerland", "Ukraine", "Vatican City State"]:
    shippingRegion = "Euro2"
else:
    shippingRegion = "World"

当应用于列表时,in运算符会检查列表中是否包含值。

最好使用a set,因为您的值未订购。设置语法如下:

{'something', 'something else', 'another thing', '...'}

它类似于列表,但此数据结构未排序。与列表一样,您也可以使用in来检查某个集合中是否包含特定值。

注意:您似乎想要elif,而不是if - 使用if会使不在Euro2区域内的任何国家/地区变为World而是包括西班牙和葡萄牙在内的地区。

答案 6 :(得分:1)

您可以使用IN

if shippingDest in ["France", "Germany", "Luxembourg", "Belgium", "United Kingdom"]: