1 回答
TA贡献1770条经验 获得超3个赞
我不太确定你的要求是什么,但这里有一个2搜索方法的样本...
#Sub Strings
List1 = ["Apple", "Mulberry"]
# List of lists
List2 = [
["Apple", "Grapefruit", "Guava"],
["Banana", "Blueberry", "Grape"],
["Lemon", "Lime"],
["Loquat", "Lychee", "Mango"],
["Mulberry", "Nectarine", "Strawberry"],
["Pomegranate", "Raspberry"]
]
#a substring to search for.
List3 = ["berry", "ime"]
print("Search for whole matching strings", List1)
for item in List2:
if any(x in item for x in List1):
print(item)
print('\n Substring search for substrings', List3)
for group in List2:
for item in group:
if any(sbs in item for sbs in List3):
print(group)
break # no need to keep searching the group
和输出
Search for whole matching strings ['Apple', 'Mulberry']
['Apple', 'Grapefruit', 'Guava']
['Mulberry', 'Nectarine', 'Strawberry']
Substring search for substrings ['berry', 'ime']
['Banana', 'Blueberry', 'Grape']
['Lemon', 'Lime']
['Mulberry', 'Nectarine', 'Strawberry']
['Pomegranate', 'Raspberry']
添加回答
举报