简体   繁体   English

如何检查子字符串是否与Python中的字符串列表匹配

[英]How to Check if the substring is matching in a list of strings in Python

I have a list and I want to find if the string is present in the list of strings. 我有一个列表,我想查找字符串列表中是否存在该字符串。

li = ['Convenience','Telecom Pharmacy']
txt = '1 convenience store'

I want to match the txt with the Convenience from the list. 我想将txt与列表中的Convenience匹配。

I have tried 我努力了

if any(txt.lower() in s.lower() for s in li):
   print s

print [s for s in li if txt in s]

Both the methods didn't give the output. 两种方法都没有给出输出。

How to match the substring with the list? 如何将子字符串与列表匹配?

You could use set() and intersection : 您可以使用set()intersection

In [19]: set.intersection(set(txt.lower().split()), set(s.lower() for s in list1))
Out[19]: {'convenience'}

You can try this, 你可以试试看

>> list1 = ['Convenience','Telecom Pharmacy']

>> txt = '1 convenience store'

>> filter(lambda x: txt.lower().find(x.lower()) >= 0, list1)
['Convenience']

# Or you can use this as well

>> filter(lambda x: x.lower() in txt.lower(), list1)
['Convenience']

I see two things. 我看到两件事。

Do you want to find if the pattern string matches EXACTLY an item in the list? 您是否要查找模式字符串是否与列表中的某个项目完全匹配? In this case, nothing simpler: 在这种情况下,没有比这更简单的了:

if txt in list1:
    #do something

You can also do txt.upper() or .lower() if you want list case insensitive But If you want as I understand, to find if there is a string (in the list) which is part of txt, you have to use "for" loop: 如果您希望列表不区分大小写,也可以执行txt.upper()或.lower()。但是,如果按照我的理解,要查找是否有一个字符串(在列表中)是txt的一部分,则必须使用“ for”循环:

def find(list1, txt):
#return item if found, false otherwise
for i in list1:
    if i.upper() in txt.upper(): return i

return False

It should work. 它应该工作。

Console output: 控制台输出:

>>>print(find(['Convenience','Telecom Pharmacy'], '1 convenience store'))
Convenience
>>>

I think split is your answer. 我认为拆分是您的答案。 Here is the description from the python documentation: 这是python文档中的描述:

string.split(s[, sep[, maxsplit]]) string.split(s [,sep [,maxsplit]])

Return a list of the words of the string s. 返回字符串s的单词列表。 If the optional second argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). 如果不存在可选的第二个参数sep或“无”,则单词由任意的空白字符字符串(空格,制表符,换行符,返回值,换页符)分隔。 If the second argument sep is present and not None, it specifies a string to be used as the word separator. 如果存在第二个参数sep而不是None,则它指定一个字符串用作单词分隔符。 The returned list will then have one more item than the number of non-overlapping occurrences of the separator in the string. 然后,返回的列表将比字符串中不重复出现的分隔符数量多一个项目。 If maxsplit is given, at most maxsplit number of splits occur, and the remainder of the string is returned as the final element of the list (thus, the list will have at most maxsplit+1 elements). 如果指定了maxsplit,则最多会发生最大拆分次数,并将字符串的其余部分作为列表的最后一个元素返回(因此,列表最多包含maxsplit + 1个元素)。 If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made). 如果未指定maxsplit或-1,则分割数没有限制(进行所有可能的分割)。

The behavior of split on an empty string depends on the value of sep. 空字符串上split的行为取决于sep的值。 If sep is not specified, or specified as None, the result will be an empty list. 如果未指定sep或将其指定为None,则结果将为空列表。 If sep is specified as any string, the result will be a list containing one element which is an empty string. 如果将sep指定为任何字符串,则结果将是一个包含一个元素的列表,该元素为空字符串。

Use the split command on your txt variable. 在txt变量上使用split命令。 It will give you a list back. 它会给你一个清单。 You can then do a compare on the two lists to find any matches. 然后,您可以在两个列表上进行比较以找到任何匹配项。 I personally would write the nested for loops to check the lists manually, but python provides lots of tools for the job. 我个人会编写嵌套的for循环来手动检查列表,但是python为这项工作提供了很多工具。 The following link discusses different approaches to matching two lists. 以下链接讨论了匹配两个列表的不同方法。

How can I compare two lists in python and return matches 如何比较python中的两个列表并返回匹配项

Enjoy. 请享用。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM