1 回答

TA貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊
我不太確定你的要求是什么,但這里有一個(gè)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']
添加回答
舉報(bào)