如何在以下列表中搜索包含字符串'abc'
的项目?
xs = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
以下代码检查列表中是否存在'abc'
,但无法检测到'abc-123'
和'abc-456'
:if 'abc' in xs:
如何在以下列表中搜索包含字符串'abc'
的项目?
xs = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
以下代码检查列表中是否存在'abc'
,但无法检测到'abc-123'
和'abc-456'
:if 'abc' in xs:
我需要与以下匹配项对应的列表索引:
lst=['abc-123', 'def-456', 'ghi-789', 'abc-456']
[n for n, x in enumerate(lst) if 'abc' in x]
输出
[0, 3]
如果您想获取多个子字符串的数据列表,您可以按照以下方式进行更改:
将它更改为这样
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']
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])
mylist=['abc','def','ghi','abc']
pattern=re.compile(r'abc')
pattern.findall(mylist)
import re
mylist=['abc','def','ghi','abcff']
my_list_string=''.join(mylist)
string_to_find="abc"
res=re.findall(string_to_find,my_list_string)
print(res)
- arun_munagalamy_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
for item in my_list:
if (item.find('abc')) != -1:
print ('Found at ', item)
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?")
我进行了一次搜索,需要您输入一个特定的值,然后它将从包含您输入的值的列表中查找一个值:
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)
尝试搜索“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']