Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
63 lines (48 loc) · 1.46 KB

File metadata and controls

63 lines (48 loc) · 1.46 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
#coding:utf-8
"""
统计一个一维数组中的各个元素的个数,然后删除多出来的重复元素,并输出结果。
例如:[1,2,2,2,3,3,3,3,3]-->[1,2,3]
"""
def count_element(one_list):
element_number = {}
for e in one_list:
number = one_list.count(e) #数出某个元素的个数
element_number[e] = number #生成类似:{1:1,2:3,3:5}的结果,key-element,value-元素的个数
return element_number
#函数count_element(one_list)的功能,可以用collections模块中的Counter完成
from collections import Counter
def count_element2(one_list):
return Counter(one_list)
def no_repeat_element(element_number): #element_number是count_element(one_list)的返回值
no_repeat_list = [key for key in element_number]
return no_repeat_list
"""
另外一种删除重复元素方法
list_a = [1,1,2,2,2,3,3,3,3,3,]
list_b = list(set(list_a))
"""
if __name__=="__main__":
ls = ["a","a","b","b",'b','c','c']
el_num=count_element(ls)
print el_num
no_repeat = no_repeat_element(el_num)
print no_repeat
print "another way is:"
no_repeat2 = list(set(ls))
print no_repeat2
print "-------"
el_num2=count_element2(ls)
print el_num2
no_repeat = no_repeat_element(el_num)
print no_repeat
"""
the result is :
{'a': 2, 'c': 2, 'b': 3}
['a', 'c', 'b']
another way is:
['a', 'c', 'b']
-------
Counter({'b': 3, 'a': 2, 'c': 2})
['a', 'c', 'b']
"""
Morty Proxy This is a proxified and sanitized view of the page, visit original site.