Python 中有什么方法可以检查字符串是否包含真实位置?
Is there any way in Python to check if a string contains a real-world location?
最有可能的是,如果有任何库分析字符串,然后 returns 那里的任何可能位置?
例如:
"foo foobar foot New York foo" 显然会 return "New York".
您可以创建一个列表,其中包含您要搜索的位置,然后使用 in
.
遍历该列表,将位置与字符串进行比较
mystring = 'hello new york foo'
locations = ['new york', 'dallas']
for location in locations:
if location in mystring:
print(location)
>new york
不过,您必须事先找出要查找的位置。您可能还想将 mystring 格式化为小写。
最有可能的是,如果有任何库分析字符串,然后 returns 那里的任何可能位置?
例如: "foo foobar foot New York foo" 显然会 return "New York".
您可以创建一个列表,其中包含您要搜索的位置,然后使用 in
.
mystring = 'hello new york foo'
locations = ['new york', 'dallas']
for location in locations:
if location in mystring:
print(location)
>new york
不过,您必须事先找出要查找的位置。您可能还想将 mystring 格式化为小写。