我试图弄清楚如何删除列表列表中的重复项,如果元素已经存在于任何列表中,我希望不再添加它
这是全部
def listeAmis(n) :
liste = []
print("Rentrez un numéro d'un ami de ",n)
a = 0
while a!="na":
a=input("tapez 'na' s'il n'y a pas d'autres amis")
liste.append(a)
last = int(len(liste)-1)
del liste[last]
return liste
def initReseau(n) :
i = 0
listeR = []
while i<n :
listeR.append(listeAmis(i))
i = i+1
return listeR
def ami(r,i,j) :
if j in r[i] and i in r[j]:
return True
else :
return False
def groupeAmis(r,i) :
groupe = [i]
a = 0
while a < n-1 :
res = all(ami(r,elem,a) for elem in groupe)
if res :
groupe.append(a)
a=a+1
return groupe
def groupeAmisPartition(r) :
liste=[]
for i in range(0,n) :
T = groupeAmis(r,i)
liste.append(T)
return liste
n = 8
r = [[2, 3], [3, 5], [0, 3, 6, 7], [0, 1, 2, 4, 5, 6], [3, 7],[1, 3, 6, 7], [2, 3, 5], [2, 4, 5]]
groupeAmisPartition(r)
以下是我得到的:
[[0, 2, 3],[1, 3, 5],[2, 0, 3],[3, 0, 2],[4, 3],[5, 1, 3],[6, 2, 3],[7, 2]]
以下是我想要的:
[[0,2,3],[1,5],[4,7],[6]]
谢谢
我的答案是基于你对问题的文字描述
尝试以下操作(代码注释中的解释):
输出:
用塔尔的代码,我得到了这个,仍然不是预期的,但几乎:
输出:
相关问题 更多 >
编程相关推荐