Python 如何在数组中搜索单词,如果找到,如何将相应的字符串返回到全局变量?
我对Python非常陌生。 我想查看字符串数组,如果某些单词匹配,则将这些单词作为变量返回。我希望能够在全局范围内打印if语句之外的变量Python 如何在数组中搜索单词,如果找到,如何将相应的字符串返回到全局变量?,python,loops,iteration,Python,Loops,Iteration,我对Python非常陌生。 我想查看字符串数组,如果某些单词匹配,则将这些单词作为变量返回。我希望能够在全局范围内打印if语句之外的变量 fullsubject = cells[2].text.encode("utf-8").replace(",","") subs = fullsubject.split(' ') # the above two lines work fine. 下面是subs数组的一些示例(我想在其中查找单词): 如果数组包含“MANH”或“Manhattan”,我希望变量
fullsubject = cells[2].text.encode("utf-8").replace(",","")
subs = fullsubject.split(' ')
# the above two lines work fine.
下面是subs数组的一些示例(我想在其中查找单词):
如果数组包含“MANH”或“Manhattan”,我希望变量borough等于“MANH”。如果数组包含“QNS”或“queen”,我希望borough等于“QNS”。
如果数组包含“BX”或“Bronx”,我希望borough等于“BX” 首先我试着这样做:
global borough
for item in subs:
if item == "BX":
borough = "BX"
elif item == "QNS":
borough = "QNS"
else:
borough = "something else"
print borough
borough = []
if [item for item in subs if item=="BX" or "Bronx"]:
borough.append("BX")
elif [item for item in subs if item=="QNS" or "Queens"]:
borough.append("QNS")
elif [item for item in subs if item=="MANH"]:
borough.append("MANH")
elif [item for item in subs if item=="BKLYN" or "Brooklyn"]:
borough.append("BKLYN")
elif [item for item in subs if item=="STATEN" or "SI"]:
borough.append("SI")
print borough
string_list = ['list', 'of', 'words', 'MANH']
borough = word_check(string_list)
但它总是打印最后一个选项“其他内容”。然后我想也许我可以这样做:
global borough
for item in subs:
if item == "BX":
borough = "BX"
elif item == "QNS":
borough = "QNS"
else:
borough = "something else"
print borough
borough = []
if [item for item in subs if item=="BX" or "Bronx"]:
borough.append("BX")
elif [item for item in subs if item=="QNS" or "Queens"]:
borough.append("QNS")
elif [item for item in subs if item=="MANH"]:
borough.append("MANH")
elif [item for item in subs if item=="BKLYN" or "Brooklyn"]:
borough.append("BKLYN")
elif [item for item in subs if item=="STATEN" or "SI"]:
borough.append("SI")
print borough
string_list = ['list', 'of', 'words', 'MANH']
borough = word_check(string_list)
但我还是得到了最后一个选择
我已搜索堆栈溢出,但找不到正确答案。帮助?您只需检查您的单词是否在列表中。比较中不需要列表来检查某个内容是否正确。然后返回变量
def word_check(string_list):
for word in string_list:
if word == "BX" or word == "Bronx":
borough = "BX"
elif word == "QNS" or word == "Queens":
borough = "QNS"
elif word == "MANH":
borough = "MANH"
else:
borough = "something else"
return borough
然后你这样称呼它:
global borough
for item in subs:
if item == "BX":
borough = "BX"
elif item == "QNS":
borough = "QNS"
else:
borough = "something else"
print borough
borough = []
if [item for item in subs if item=="BX" or "Bronx"]:
borough.append("BX")
elif [item for item in subs if item=="QNS" or "Queens"]:
borough.append("QNS")
elif [item for item in subs if item=="MANH"]:
borough.append("MANH")
elif [item for item in subs if item=="BKLYN" or "Brooklyn"]:
borough.append("BKLYN")
elif [item for item in subs if item=="STATEN" or "SI"]:
borough.append("SI")
print borough
string_list = ['list', 'of', 'words', 'MANH']
borough = word_check(string_list)
你好你能给我们举一个你正在使用的字符串数组的例子,也许还有你试图匹配的单词?谢谢你好@NBlaine-我添加了更多细节。谢谢对不起,我不清楚。我遇到的问题是,我希望能够访问该函数之外的borough变量。我更新了我的问题,使之更具体。你可以让它全球化,但你不需要。只需在函数末尾返回变量。这仅在单词是数组中的最后一项时有效。