示例**:
lsta = [4, 2執行結果:, 5, 5
, 6, 8
, 6, 5
, 2, 3
, 2, 6
, 2, 5
, 4, 2
, 3, 6
, 9, 5
, 2, 2
, 6, 8
, 2, 1
, 4, 5
, 6, 8
, 5, 2]
setb = set(lsta)
forelbinsetb:
count = 0
forelainlsta:
ifelb == ela:
count += 1
print(elb,
"的個數為
:", count)
1 的個數為: 1
2 的個數為: 9
3 的個數為: 2
4 的個數為: 3
5 的個數為: 7
6 的個數為: 6
8 的個數為: 3
9 的個數為: 1
process finished with exit code 0
示例**2:
lsta = [4, 2執行結果:, 5, 5
, 6, 8
, 6, 5
, 2, 3
, 2, 6
, 2, 5
, 4, 2
, 3, 6
, 9, 5
, 2, 2
, 6, 8
, 2, 1
, 4, 5
, 6, 8
, 5, 2]
el = lsta
fromcollectionsimportdefaultdict
deflistdu(seq):
allnum = defaultdict(list)
fori, iteminenumerate(seq):
return((key, l)forkey, linallnum.items()
iflen(l) > 1)
foreloneinsorted(listdu(el)):
print(elone)
(2, [1, 8, 10, 12, 15, 20, 21, 24, 31])
(3, [9, 16])
(4, [0, 14, 26])
(5, [2, 3, 7, 13, 19, 27, 30])
(6, [4, 6, 11, 17, 22, 28])
(8, [5, 23, 29])
process finished with exit code 0
python中列表去重
li 2,5,4,7,2,1,3,2,6,7 print 方法一列表追加 center 40,list1 for i in li if i not in list1 print list1 print 推導式實現 center 40,list2 print list2 print 方法二利用字典鍵唯...
python中列表去重
列表去重 定義乙個函式 def remove element m list 將列表 10,1,2,20,10,3,2,1,15,20,44,56,3,2,1 去除重複元素 def remove element m list 定義乙個空列表 new list 迴圈判斷某個元素是不是在新的列表中,如果在...
Python 列表查重
from collections import counter 引入counter a 29,36,57,12,79,43,23,56,28,11,14,15,16,37,24,35,17,24,33,15,39,46,52,13 b dict counter a print key for key...