如何在列表中只找到双打?我的算法版本
import collections
a = [1,2,3,4,5,2,4,5]
b = []
for x,y in collections.Counter(a).items():
if y>1:
b.append(x)
print(b) # [2, 4, 5]
c = []
for item in a:
if item in b:
c.append(item)
print(c) # [2, 4, 5, 2, 4, 5]
Run Code Online (Sandbox Code Playgroud)
需要找到c等结果
代码缺陷:
我需要留下列表双打值,例如.x = [1,2,2,2,3,4,5,6,6,7],需要[2,2,2,6,6]而不是[2,6]
python ×1