簡體   English   中英

沒有列表的重復元素

[英]Repeated elements without list

所以這是我的代碼:

y=[]
for x in random_gen:
    y.append(x)   
print([x for x, count in collections.Counter(y).x() if count > 1]) 

有沒有在不附加列表的情況下執行此操作,即沒有y.append(x)

這個循環的作用:

y=[]
for x in random_gen:
  y.append(x)   

基本上是復制random_gen並將其命名為y

因此,只需使用random_gen

print([x for x, count in collections.Counter(random_gen).x() if count > 1]) 

然后你甚至不需要循環。 這取自 Barmar 的評論。

y=[]   
print([x for x, count in collections.Counter(random_gen).x() if count > 1]) 

因為y只是ramdom_gen的一個副本

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM