3 回答

TA貢獻1794條經(jīng)驗 獲得超8個贊
假設(shè)列表大小相同。您需要做的就是遍歷“標題”并比較每個索引處的(嵌套循環(huán))元素。下面的代碼
id = [1, 2, 3, 4, 5, 6]
title = ['title1', 'title1', 'title2', 'title3', 'title3', 'title4']
text = ['sampleText1', 'sampleText1', 'sampleText2', 'sampleText3', 'sampleText3', 'sampleText4']
res = []
for i in range(len(id)):
for j in range(i+1,len(id)): #i+1 'cause we need to start comparing from the next index
if title[i] == title[j]:
res.append(id[j])
break
print(res)

TA貢獻1803條經(jīng)驗 獲得超3個贊
title另一種解決方案是下面的解決方案,其中的順序text無關(guān)緊要:
id = [1, 2, 3, 4, 5, 6]
title = ['title1', 'title1', 'title2', 'title3', 'title3', 'title4']
text = ['sampleText1', 'sampleText1', 'sampleText2', 'sampleText3', 'sampleText3', 'sampleText4']
dupTitles = [] # Titles that are already checked.
dupTexts = [] # Texts that are already checked.
result = [] # Final list of IDs.
for elemId, elemTitle, elemText in zip(id,title,text):
if elemTitle in dupTitles and elemText in dupTexts:
result.append(elemId)
else:
dupTitles.append(elemTitle)
dupTexts.append(elemText)
print(result)
結(jié)果將是:
[2, 5]

TA貢獻1795條經(jīng)驗 獲得超7個贊
您可以像這樣使用 for 循環(huán):
id = [1, 2, 3, 4, 5, 6]
title = ['title1', 'title1', 'title2', 'title3', 'title3', 'title4']
text = ['sampleText1', 'sampleText1', 'sampleText2', 'sampleText3', 'sampleText3', 'sampleText4']
result = []
for x in id:
tmptitle = title[x-1]
tmptext = text[x-1]
if x > 0:
if tmptitle == title[x-2] and tmptext == text[x-2]:
result.append(x)
print(result)
對于另一個問題,請?zhí)峁┠鸀榻鉀Q問題而編寫的代碼
添加回答
舉報