![](/img/trans.png)
[英]How to check if a list contains items from another lists sublist? [on hold]
[英]Check element in list of lists from items in another list of lists
我有一個列表list1
和一個要檢查list2
的列表,我想運行一個 for 循環來檢查list2
中的元素是否在list1
中,然后將存在的元素附加到“模型”,這是一個拉第一個的列表list1
中每個列表的索引。
沒有錯誤,但我相信這可能是因為我正在使用的類型。
我的代碼:
a = "sample.txt" # Contains multiple rows like "model1//h//4385////ffm1//gg5////lmm"
file1 = open(a, "r")
with open(a, 'r') as file:
lines = [line.rstrip('\n') for line in file]
cleaned_data = []
def split_lines(lines, delimiter, remove='^[0-9.]+$'):
for line in lines:
tokens = line.split(delimiter)
tokens = [re.sub(remove, "", token) for token in tokens]
clean_list = list(filter(lambda e: e.strip(), tokens))
cleaned_data.append(clean_list)
split_lines(lines, "/")
print(cleaned_data)
# outputs: ["model1", "h", "ffm1", "gg5", "1mm"] for each line in txt
##cleaned_data = [["model1", "h", "ffm1", "gg5", "1mm"], then more of this
## in a list of lists
list2 = ['gg5', 'pp6', 'gg7'] # to check element in the cleaned_data
models = []
for item in cleaned_data:
if item[0] not in models:
models.append(item[0])
## models = ['model1', 'model2', 'model3']
for item in cleaned_data:
if item == insulation:
models.append(item)
print(models)
#This is where I want to check if elements in list2 are in the list of lists #in cleaned_data, then append that elements from list2 to index1 of models.
輸出:
['model1', 'model2', 'model3']
預期輸出:
[['model1', 'gg5'], ['model2', 'pp6'], ['model3', 'gg5']
您的代碼存在一些問題。
你的循環沒有做任何事情,因為當你這樣做時:
if item == list2:
比較總是錯誤的,因為item
和list2
的結構不同(例如,您將["model1", "yes", "TT42"]
與['TT42', 'GG50', 'BB12']
進行比較) .
你的輸出是['model1', model2', 'model3']
但這是因為你這樣定義它,循環沒有改變它。
另外,當你這樣做時:
models.append(item)
您將在models
變量中添加一個完整列表。 例如,如果循環的第一次比較是正確的,您將擁有:
>>> print(models)
['model1', model2', 'model3', ['model1', 'yes', 'TT42']]
這顯然不是你想要的。
這是一種做你想做的事情的方法:
list1 = [['model1', 'yes', 'TT42'],
['model2', 'yes', 'GG50'],
['model3', 'no', 'BB12']]
list2 = ['TT42', 'GG50', 'BB12']
models = [[item[0], item[2]] for item in list1 if item[2] in list2]
輸出:
>>> print(models)
[['model1', 'TT42'], ['model2', 'GG50'], ['model3', 'BB12']]
IIUC 這將獲取您的列表 2 並將其與您的列表 1 進行比較,並找到 list2 中的所有內容都等於 list1 中的任何內容並將其添加到模型列表中
list1 = [['model1', 'yes', 'TT42'],
['model2', 'yes', 'GG50'],
['model3', 'no', 'BB12']]
list2 = ['TT42', 'GG50', 'BB12']
single_list = [list1[x][y] for x in range(0, len(list1)) for y in range(0, len(list1[x]))]
models = [x for x in list2 for y in single_list if x ==y]
models
如果這不是預期結果,請提供更多詳細信息和預期結果。
我不確定我是否理解正確,但這會產生你想要的嗎?:
list1 = [["model1", "h", "ffm1", "gg5", "1mm"],
["model2", "h", "ffm1", "pp6", "1mm"],
["model2", "h", "ffm1", "gg7", "1mm"]]
list2 = ['gg5', 'pp6', 'gg7']
models=[]
for x in list2:
for y in list1:
if x in y[1:]:
models.append([y[0],x])
另外,這是我能想到的另一種方式:
cleaned_data = [["model1", "h", "ffm1", "gg5", "1mm"],
["model2", "h", "ffm1", "pp6", "1mm"],
["model2", "h", "ffm1", "gg7", "1mm"]]
list2 = ['gg5', 'pp6', 'gg7']
models = {}
for item in cleaned_data:
if item[0] not in models:
models[item[0]]=[]
for x in list2:
for y in list1:
if x in y[1:]:
models[y[0]].append(x)
result=[[item[0],*item[1]] for item in models.items()]
# result: [['model1', 'gg5'], ['model2', 'pp6', 'gg7']]
對於每一行,您可以提取第一列作為模型名稱,其余列作為屬性,並通過與list2
執行集合交集來獲取與list2
中任何項目匹配的屬性,為了提高效率,應將其轉換為集合進步:
set2 = set(list2)
models = [[model, prop] for model, *props in list1 for prop in set2.intersection(props)]
演示: https ://replit.com/@blhsing/FakeFaintVendors
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.