[英]How to check if a string is a substring of items in a list of strings?
How do I search for items that contain the string 'abc'
in the following list?如何在以下列表中搜索包含字符串'abc'
的项目?
xs = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
The following checks if 'abc'
is in the list, but does not detect 'abc-123'
and 'abc-456'
:以下检查'abc'
是否在列表中,但未检测到'abc-123'
和'abc-456'
:
if 'abc' in xs:
To check for the presence of 'abc'
in any string in the list:要检查列表中任何字符串中是否存在'abc'
:
xs = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
if any("abc" in s for s in xs):
...
To get all the items containing 'abc'
:要获取所有包含'abc'
的项目:
matching = [s for s in xs if "abc" in s]
Just throwing this out there: if you happen to need to match against more than one string, for example abc
and def
, you can combine two comprehensions as follows:只是把它扔在那里:如果你碰巧需要匹配多个字符串,例如abc
和def
,你可以组合两种理解,如下所示:
matchers = ['abc','def']
matching = [s for s in my_list if any(xs in s for xs in matchers)]
Output:输出:
['abc-123', 'def-456', 'abc-456']
Use filter
to get all the elements that have 'abc'
:使用filter
获取所有具有'abc'
的元素:
>>> xs = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
>>> list(filter(lambda x: 'abc' in x, xs))
['abc-123', 'abc-456']
One can also use a list comprehension:也可以使用列表推导:
>>> [x for x in xs if 'abc' in x]
If you just need to know if 'abc' is in one of the items, this is the shortest way:如果您只需要知道“abc”是否在其中一项中,这是最短的方法:
if 'abc' in str(my_list):
Note: this assumes 'abc' is an alphanumeric text.注意:这里假设“abc”是一个字母数字文本。 Do not use it if 'abc' could be just a special character (ie []', ).如果 'abc' 可能只是一个特殊字符(即 []', ),请不要使用它。
This is quite an old question, but I offer this answer because the previous answers do not cope with items in the list that are not strings (or some kind of iterable object).这是一个相当古老的问题,但我提供了这个答案,因为之前的答案不能处理列表中不是字符串(或某种可迭代对象)的项目。 Such items would cause the entire list comprehension to fail with an exception.这样的项目会导致整个列表理解失败并出现异常。
To gracefully deal with such items in the list by skipping the non-iterable items, use the following:要通过跳过不可迭代的项目来优雅地处理列表中的此类项目,请使用以下命令:
[el for el in lst if isinstance(el, collections.Iterable) and (st in el)]
then, with such a list:然后,使用这样的列表:
lst = [None, 'abc-123', 'def-456', 'ghi-789', 'abc-456', 123]
st = 'abc'
you will still get the matching items ( ['abc-123', 'abc-456']
)你仍然会得到匹配的项目( ['abc-123', 'abc-456']
)
The test for iterable may not be the best.可迭代的测试可能不是最好的。 Got it from here: In Python, how do I determine if an object is iterable?从这里得到它: 在 Python 中,我如何确定一个对象是否是可迭代的?
x = 'aaa'
L = ['aaa-12', 'bbbaaa', 'cccaa']
res = [y for y in L if x in y]
for item in my_list:
if item.find("abc") != -1:
print item
any('abc' in item for item in mylist)
I am new to Python.我是 Python 新手。 I got the code below working and made it easy to understand:我得到了下面的代码并使其易于理解:
my_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
for item in my_list:
if 'abc' in item:
print(item)
Use the __contains__()
method of Pythons string class.:使用 Python 字符串类的__contains__()
方法:
a = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
for i in a:
if i.__contains__("abc") :
print(i, " is containing")
If you want to get list of data for multiple substrings如果要获取多个子字符串的数据列表
you can change it this way你可以这样改变
some_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
# select element where "abc" or "ghi" is included
find_1 = "abc"
find_2 = "ghi"
result = [element for element in some_list if find_1 in element or find_2 in element]
# Output ['abc-123', 'ghi-789', 'abc-456']
I needed the list indices that correspond to a match as follows:我需要与匹配项对应的列表索引,如下所示:
lst=['abc-123', 'def-456', 'ghi-789', 'abc-456']
[n for n, x in enumerate(lst) if 'abc' in x]
output输出
[0, 3]
mylist=['abc','def','ghi','abc']
pattern=re.compile(r'abc')
pattern.findall(mylist)
Adding nan to list, and the below works for me:将 nan 添加到列表中,以下内容对我有用:
some_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456',np.nan]
any([i for i in [x for x in some_list if str(x) != 'nan'] if "abc" in i])
my_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
for item in my_list:
if (item.find('abc')) != -1:
print ('Found at ', item)
I did a search, which requires you to input a certain value, then it will look for a value from the list which contains your input:我进行了搜索,要求您输入某个值,然后它将从包含您输入的列表中查找一个值:
my_list = ['abc-123',
'def-456',
'ghi-789',
'abc-456'
]
imp = raw_input('Search item: ')
for items in my_list:
val = items
if any(imp in val for items in my_list):
print(items)
Try searching for 'abc'.尝试搜索“abc”。
def find_dog(new_ls):
splt = new_ls.split()
if 'dog' in splt:
print("True")
else:
print('False')
find_dog("Is there a dog here?")
From my knowledge, a 'for' statement will always consume time. 据我所知,“ for”陈述总是会浪费时间。
When the list length is growing up, the execution time will also grow. 当列表长度增加时,执行时间也会增加。
I think that, searching a substring in a string with 'is' statement is a bit faster. 我认为,使用“ is”语句在字符串中搜索子字符串会更快一些。
In [1]: t = ["abc_%s" % number for number in range(10000)]
In [2]: %timeit any("9999" in string for string in t)
1000 loops, best of 3: 420 µs per loop
In [3]: %timeit "9999" in ",".join(t)
10000 loops, best of 3: 103 µs per loop
But, I agree that the any
statement is more readable. 但是,我同意any
语句更具可读性。
Question : Give the informations of abc问题:给出 abc 的信息
a = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
aa = [ string for string in a if "abc" in string]
print(aa)
Output => ['abc-123', 'abc-456']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.