Python:使用Counter进行计数统计及collections模块
来源:互联网 发布:淘宝哪个返利多 编辑:程序博客网 时间:2024/05/16 08:06
1
2
3
4
5
6
7
8
9
10
#coding=utf-8
data
=
[
'a'
,
'2'
,
2
,
4
,
5
,
'2'
,
'b'
,
4
,
7
,
'a'
,
5
,
'd'
,
'a'
,
'z'
]
count_frq
=
dict
()
for
one
in
data:
if
one
in
count_frq:
count_frq[one]
+
=
1
else
:
count_frq[one]
=
1
print
count_frq
1
2
3
4
5
6
7
8
#coding=utf-8
data
=
[
'a'
,
'2'
,
2
,
4
,
5
,
'2'
,
'b'
,
4
,
7
,
'a'
,
5
,
'd'
,
'a'
,
'z'
]
data_set
=
set
(data)
count_list
=
[]
for
one
in
data_set:
count_list.append((one,data.count(one)))
print
count_list
1
2
3
4
5
#coding=utf-8
from
collections
import
Counter
data
=
[
'a'
,
'2'
,
2
,
4
,
5
,
'2'
,
'b'
,
4
,
7
,
'a'
,
5
,
'd'
,
'a'
,
'z'
]
c
=
Counter(data)
print
c
1
2
print
c.elements()
print
list
(c.elements())
1
2
3
4
c[
'z'
]
-
=
1
print
c
print
c.elements()
print
list
(c.elements())
1
2
c.update(
"aaaa"
)
print
c
1
2
c.subtract(
"aaaaa"
)
print
c
1
print
c.most_common()
1
2
3
4
5
6
7
8
9
10
11
#coding=utf-8
from
collections
import
Counter
data
=
[
'a'
,
'2'
,
'2'
,
'b'
,
'a'
,
'd'
,
'a'
,]
c
=
Counter(data)
b
=
Counter(a
=
1
,b
=
2
)
print
c
print
b
print
b
+
c
# c[x] + d[x]
print
c
-
b
# subtract(只保留正数计数的元素)
print
c&b
# 交集: min(c[x], d[x])
print
c|b
# 并集: max(c[x], d[x])
1
2
3
4
5
6
7
8
9
10
11
#coding=utf-8
from
collections
import
Counter
data
=
[
'a'
,
'2'
,
2
,
4
,
5
,
'2'
,
'b'
,
4
,
7
,
'a'
,
5
,
'd'
,
'a'
,
'z'
]
c
=
Counter(data)
print
c.keys()
print
c.has_key(
'a'
)
print
c.get(
'a'
)
print
c.items()
print
c.values()
print
c.viewitems()
print
c.viewkeys()
1
2
from
collections
import
deque
q
=
deque([
'a'
,
'b'
,
'c'
])
1
2
3
4
5
6
#coding=utf-8
from
collections
import
deque
q
=
deque([
'a'
,
'b'
,
'c'
])
print
q
q.rotate(
2
)
print
q
1
2
3
4
5
6
7
8
9
10
#coding=utf-8
import
sys
import
time
from
collections
import
deque
fancy_loading
=
deque(
'>--------------------'
)
while
True
:
print
'\r%s'
%
''.join(fancy_loading),
fancy_loading.rotate(
1
)
sys.stdout.flush()
time.sleep(
0.1
)
1
2
3
4
5
6
7
#coding=utf-8
Bob
=
(
'bob'
,
30
,
'male'
)
print
'Representation:'
,Bob
Jane
=
(
'Jane'
,
29
,
'female'
)
print
'Field by index:'
,Jane[
0
]
for
people
in
[Bob,Jane]:
print
"%s is %d years old %s"
%
people
1
2
3
4
5
6
7
8
9
10
#coding=utf-8
import
collections
Person
=
collections.namedtuple(
'Person'
,
'name age gender'
)
print
'Type of Person:'
,
type
(Person)
Bob
=
Person(name
=
'Bob'
, age
=
30
, gender
=
'male'
)
print
'Representation:'
, Bob
Jane
=
Person(name
=
'Jane'
, age
=
29
, gender
=
'female'
)
print
'Field by Name:'
, Jane.name
for
people
in
[Bob,Jane]:
print
"%s is %d years old %s"
%
people
1
2
3
4
5
6
#coding=utf-8
import
collections
with_class
=
collections.namedtuple(
'Person'
,
'name age class gender'
,rename
=
True
)
print
with_class._fields
two_ages
=
collections.namedtuple(
'Person'
,
'name age gender age'
,rename
=
True
)
print
two_ages._fields
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#coding=utf-8
from
collections
import
OrderedDict
items
=
(
(
'A'
,
1
),
(
'B'
,
2
),
(
'C'
,
3
)
)
regular_dict
=
dict
(items)
ordered_dict
=
OrderedDict(items)
print
'Regular Dict:'
for
k, v
in
regular_dict.items():
print
k, v
print
'Ordered Dict:'
for
k, v
in
ordered_dict.items():
print
k, v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from
collections
import
OrderedDict
class
LastUpdatedOrderedDict(OrderedDict):
def
__init__(
self
, capacity):
super
(LastUpdatedOrderedDict,
self
).__init__()
self
._capacity
=
capacity
def
__setitem__(
self
, key, value):
containsKey
=
1
if
key
in
self
else
0
if
len
(
self
)
-
containsKey >
=
self
._capacity:
last
=
self
.popitem(last
=
False
)
print
'remove:'
, last
if
containsKey:
del
self
[key]
print
'set:'
, (key, value)
else
:
print
'add:'
, (key, value)
OrderedDict.__setitem__(
self
, key, value)
阅读全文
0 0
- Python:使用Counter进行计数统计及collections模块
- python使用Collections.Counter进行计数
- Python使用collections的Counter类对字符串、列表、字典的数据元素进行计数
- Python中Counter计数统计
- python之collections模块Counter类使用学习
- [Python]collections模块中的Counter类
- Python collections模块Counter类深入分析
- Python collections模块的Counter类
- Python Collections Counter
- python collections Counter
- Python标准库——collections模块的Counter类
- Python标准库——collections模块的Counter类
- Python标准库——collections模块的Counter类
- Python标准库——collections模块的Counter类
- Python标准库——collections模块的Counter类
- Python标准库——collections模块的Counter类
- python学习:counter计数
- Python Counter()计数工具
- 循环链表-圆桌出列问题
- MySql高级—视图、函数、存储过程、触发器
- 字符串数组元素排列与组合
- mysql 数据库
- Android异步消息处理机制
- Python:使用Counter进行计数统计及collections模块
- Noip2015 斗地主【搜索】【贪心】
- 自动更新packagejson
- 图像的矩
- Tomcat共用Session实现SSO单点登录
- 5步做一个 TensorFlow 聊天机器人:DeepQA
- Codeforces 828D High Load (构造)
- 企业级旅行App源码、高仿爱鲜蜂源码、iOS Arkit测距源码等
- 图片局部马斯克demo展示