是否有任何内置方法来打印python列表中存在的重复元素。
我可以编写相同的程序。
我要搜索的只是是否有任何内置方法或相同的东西。
例如:
对于输入[4,3,2,4,5,6,4,7,6,8]
我需要操作4,6
答案 0 :(得分:1)
collections
中有一个Counter
类可以解决问题
from collections import Counter
lst = [4,3,2,4,5,6,4,7,6,8]
d = Counter(lst) # -> Counter({4: 3, 6: 2, 3: 1, 2: 1, 5: 1, 7: 1, 8: 1})
res = [k for k, v in d.items() if v > 1]
print(res)
# [4, 6]
答案 1 :(得分:0)
可能相关的重复问题:How do I find the duplicates in a list and create another list with them?
简单答案:
>>> l = [1,2,3,4,4,5,5,6,1]
>>> set([x for x in l if l.count(x) > 1])
set([1, 4, 5])
答案 2 :(得分:0)
通过简单的内置功能,您可以执行以下操作:
Protected Sub createTable()
Try
Dim listB As New ArrayList()
Dim dt As New DataTable
Dim info As SessioneUtente = Session(SessionVariables.InformazioniUtente)
Dim id As String = info.getIdDisciplinaDefault.ToString
dt = operations.G_Number(id)
If dt.Rows.Count = 0 Then
notifica.Text = "There is no number for this id"
Else
For Each row In dt.Rows
list.Add(row(0))
listB.Add(row(0))
Next
listB.Add(23)
listB.Add(24)
listB.Add(25)
Dim cell1 As New TableCell
Dim cell2 As New TableCell
Dim Nr As New Label
Dim Icon As New Label
Dim row1 As New TableRow
Dim row2 As New TableRow
For Each item In listB
If list.Contains(item) Then
Nr.Text = item
Icon.Text = "<img = src='../Images/Icon1.png' />"
cell1.Controls.Add(Nr)
cell2.Controls.Add(Icon)
row1.Cells.Add(cell1)
row2.Cells.Add(cell2)
Else
Nr.Text = item & vbLf
Icon.Text = "<img = src='../Images/Icon2.png' />"
cell1.Controls.Add(Nr)
cell2.Controls.Add(Icon)
row1.Cells.Add(cell1)
row2.Cells.Add(cell2)
End If
Next
tabel.Rows.Add(row1)
tabel.Rows.Add(row2)
notifica.Visible = False
End If
Catch ex As Exception
Dim log As New Log
log.writeLog("default", ex)
End Try
End Sub
答案 3 :(得分:0)
这是一个简单的演示
x = [1,2,3,5,3,2]
y = []
for a in x:
if not a in y:
y.append(a)
print(a)
这就是它的工作方式。对x
中的每个项目进行迭代。如果y
中当前的迭代项目不存在,请添加