私はこの問題を2日間苦労しており、助けが必要です。リストのリストで繰り返し要素を探す必要があります。 list_of_list = [(a1, b1, c1), (a2, b2, c2), ..., (an, bn, cn)]
"a"と "b"要素は整数で、 "c"要素は浮動小数点です。リストのリストに繰り返し要素がありますpython
たとえば、a1 == a2
またはa1 == bn
の場合、リスト要素全体で新しいリストを作成する必要があります。リストのリスト内のすべてのリスト(a、b、c)に対してこれを繰り返す必要があります。別の言い方をすると、複数のリストに存在する要素を持つすべてのリストが必要です。私は "a"と "b"要素だけを比較するが、最終的なリストでは関連する値 "c"を得る必要がある。例えば
:
list_of_list = [(1, 2, 4.99), (3, 6, 5.99), (1, 4, 3.00), (5, 1, 1.12), (7, 8, 1.99) ]
desired_result=[(1, 2, 4.99), (1, 4, 3.00), (5, 1, 1.12)]
私は多くのアイデアを試してみてください...しかし、素敵な何も来ない:
MI_network = [] #repeated elements list
genesis = list(complete_net) #clon to work on
genesis_next = list(genesis) #clon to remove elements in iterations
genesis_next.remove(genesis_next[0])
while genesis_next != []:
for x in genesis:
if x[0] in genesis_next and x[1] not in genesis_next:
MI_network.append(x)
if x[0] not in genesis_next and x[1] in genesis_next:
MI_network.append(x)
genesis_next.remove(genesis_next[0])
ん '、またはそれのほんの一部を? – ASGM
'a1'の値と比較する必要がありますか? –
あなたは(1、n)のi、jに対して 'ai = aj'と' ai = bj'を意味しますか? –