如何在以下列表中搜索包含字符串“abc”的项目?

xs = ['abc-123', 'def-456', 'ghi-789', 'abc-456']

以下检查“abc”是否在列表中,但未检测到“abc-123”和“abc-456”:

if 'abc' in xs:

当前回答

问题:给出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']

其他回答

将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)
any('abc' in item for item in mylist)
mylist=['abc','def','ghi','abc']

pattern=re.compile(r'abc') 

pattern.findall(mylist)

我是Python新手。我得到了下面的代码,使其易于理解:

my_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
for item in my_list:
    if 'abc' in item:
       print(item)