Python-将字符串中的单词与字符串列表匹配 [英] Python - match a word in a string with a list of strings
问题描述
我是python的新手,我想知道如何进行字符串比较
I'm new to python and I was wondering how string comparison is done
假设我有一个包含状态名称的字符串列表,例如
Let's say I have a list of strings containing state names like
states = ["New York", "California", "Nebraska", "Idaho"]
我还有另一个包含地址的字符串,例如
I also have another string that contains an address like
postal_addr = "1234 1st E St San Jose California 95112"
如何解析此地址字符串并找到与状态列表中的项目匹配的项目?在上面的示例中,加利福尼亚将是一个匹配项.匹配后,如何提取"California"
并将其存储为单独的字符串?
How do I parse this address string and find a match with the items in the states list? In the above example, California will be a match. How do I then, after matching, extract "California"
and store it as a separate string?
推荐答案
我会做
matches = [ s for s in states if s in postal_addr ]
然后,如果要从邮政地址获取字符串:
Then, if you want to get the string from the postal address:
import re
if matches:
extracted = re.findall( matches[0], postal_addr)[0]
..但这不适用于城市名称包含不同州的城市/州组合,例如postal_adr = '1 Arrowhead Dr, Kansas City, Missouri 64129'
和states = ["New York", "California", "Nebraska", "Idaho", "Missouri", "Kansas"]
等.在这种情况下
..but this won't work for city/state combos where the city name contains a different state, for example if postal_adr = '1 Arrowhead Dr, Kansas City, Missouri 64129'
and states = ["New York", "California", "Nebraska", "Idaho", "Missouri", "Kansas"]
etc. In this case
import re
if matches:
extracted = [(re.search(m, postal_addr).start() , m) for m in matches ]
extracted = sorted( extracted )[-1][1]
这篇关于Python-将字符串中的单词与字符串列表匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!