-4
希望在新手問題上獲得一些幫助。我想對一系列子列表進行排序,如果子列表中的任何項目出現兩次,請將其刪除,然後將其各自列表中的值合併到一個新的子列表中。組合嵌套列表並刪除重複項
舉個例子:
my_currentlist = [[A,B],[C,D],[B,E]]
my_desiredlist = [[A,E],[C,D]]
任何想法?
我失敗的嘗試(抱歉,新的Python):
for i in range(len(items)):
j=i+1
for j in range(len(items)):
if items[i][0]==items[j][0]:
items.remove(items[i])
items.remove(items[j])
items.append([items[i][1], items[j][1]])
elif items[i][0]==items[j][1]:
items.remove(items[i])
items.remove(items[j])
items.append([items[i][1], items[j][0]])
elif items[i][1]==items[j][0]:
items.remove(items[i])
items.remove(items[j])
items.append([items[i][0], items[j][1]])
elif items[i][1]==items[j][1]:
items.remove(items[i])
items.remove(items[j])
items.append([items[i][0], items[j][0]])
你嘗試過什麼到目前爲止其餘的元素呢? –
目前還不清楚你想要做什麼的一般情況。子列表總是隻有兩個項目嗎? '[[A,B],[B,C],[C,A]]'的結果是什麼?那麼'[[A,A],[B,C]]'?或'[[A,B],[C,D],[A,B]]? – Reti43
歡迎來到StackOverflow。請閱讀並遵守幫助文檔中的發佈準則。 [最小,完整,可驗證的示例](http://stackoverflow.com/help/mcve)適用於此處。在您發佈代碼並準確描述問題之前,我們無法有效幫助您。 – Prune