PYTHON-COOKBOOK
文章目录
- PYTHON-COOKBOOK
- 一:数据结构和算法
- 1、Heapq
- 取出最大或最小的几个元素
- 优先级队列
- 2、字典
- 多值字典
- 字典排序
- 字典列表排序
- 字典共同点和不同点
- 3、collections
- 保留最后几个元素
- 频率统计
- 命名元组
- 字典(映射)联合查找
- 4、其他
- 解压赋值
- 去重且保持顺序
- 切片
- 分组
一:数据结构和算法
三个基本的写法:
- 列表生成式
- 字典生成式
- 迭代器生成式
1、Heapq
取出最大或最小的几个元素
# 最大或者最小的n个元素
import
random
from
heapq
import
nsmallest
,
nlargest
tt
=
[
random
.
randint
(
1
,
10000
)
for
i
in
range
(
1000
)
]
# 取出的元素较少
%
time nsmallest
(
7
,
tt
)
%
time nlargest
(
7
,
tt
)
# 取出的元素很多
%
time
sorted
(
tt
)
[
:
7
]
%
time
sorted
(
tt
)
[
-
7
:
]
# 只取出最大的或者最小的
%
time
min
(
tt
)
%
time
max
(
tt
)
优先级队列
import
heapq
class
PriorityQueue
:
def
__init__
(
self
)
:
self
.
_queue
=
[
]
def
push
(
self
,
item
,
priority
)
:
heapq
.
heappush
(
self
.
_queue
,
(
priority
,
item
)
)
def
pop
(
self
)
:
r
=
heapq
.
heappop
(
self
.
_queue
)
return
r
pp
=
PriorityQueue
(
)
pp
.
push
(
'a'
,
3
)
pp
.
push
(
'b'
,
9
)
pp
.
push
(
'c'
,
6
)
pp
.
push
(
'd'
,
3
)
for
i
in
range
(
4
)
:
print
(
pp
.
pop
(
)
)
2、字典
多值字典
from
collections
import
defaultdict
d
=
defaultdict
(
list
)
d
[
'a'
]
.
append
(
1
)
d
[
'a'
]
.
append
(
2
)
d
[
'b'
]
.
append
(
4
)
# dd = defaultdict(tuple)
字典排序
# 字典排序
shares
=
{
'ACME'
:
45.23
,
'AAPL'
:
612.78
,
'IBM'
:
205.55
,
'HPQ'
:
37.20
,
'FB'
:
10.75
}
rev_shares
=
list
(
zip
(
shares
.
values
(
)
,
shares
.
keys
(
)
)
)
print
(
min
(
rev_shares
)
)
print
(
max
(
rev_shares
)
)
print
(
sorted
(
rev_shares
)
)
字典列表排序
# 字典列表的排序
from
operator
import
itemgetter
aa
=
[
{
'name'
:
'bob'
,
'score'
:
10
}
,
{
'name'
:
'ming'
,
'score'
:
12
}
,
{
'name'
:
'mike'
,
'score'
:
1
}
]
sorted
(
aa
,
key
=
itemgetter
(
'score'
)
)
字典共同点和不同点
# 字典的共同点 和 不同点
a
=
{
'x'
:
1
,
'y'
:
2
,
'z'
:
3
}
b
=
{
'w'
:
10
,
'x'
:
11
,
'y'
:
2
}
print
(
a
.
keys
(
)
&
b
.
keys
(
)
)
print
(
a
.
keys
(
)
-
b
.
keys
(
)
)
3、collections
保留最后几个元素
# 保留最后几个元素
from
collections
import
deque
def
lastN
(
ll
)
:
dq
=
deque
(
maxlen
=
3
)
for
i
in
ll
:
# yield i
dq
.
append
(
i
)
return
dq
aa
=
[
1
,
2
,
67
,
4
,
3
,
6
,
87
,
4
]
lastN
(
aa
)
频率统计
# 统计出现频率
import
random
from
collections
import
Counter
aa
=
[
random
.
randint
(
1
,
10
)
for
x
in
range
(
30
)
]
res
=
Counter
(
aa
)
print
(
res
)
print
(
res
.
most_common
(
3
)
)
命名元组
# 命名元组
from
collections
import
namedtuple
sub
=
namedtuple
(
'sub'
,
[
'name'
,
'score'
]
)
s
=
sub
(
'bob'
,
'87'
)
print
(
s
.
name
)
print
(
s
.
score
)
字典(映射)联合查找
from
collections
import
ChainMap
a
=
{
'x'
:
1
,
'z'
:
3
}
b
=
{
'y'
:
2
,
'z'
:
4
}
c
=
{
'd'
:
5
,
'z'
:
9
}
md
=
ChainMap
(
a
,
b
,
c
)
print
(
md
.
get
(
'x'
)
)
print
(
md
.
get
(
'z'
)
)
print
(
md
.
get
(
'yzz'
)
)
4、其他
解压赋值
# 解压赋值
record
=
(
'Dave'
,
'dave@example.com'
,
'773-555-1212'
,
'847-555-1212'
)
name
,
_
,
*
phone
=
record
print
(
name
)
print
(
phone
)
去重且保持顺序
# 去重并且保持顺序
def
dedupe
(
ll
)
:
seen
=
set
(
)
for
item
in
ll
:
if
item
not
in
seen
:
yield
item
seen
.
add
(
item
)
return
seen
a
=
[
1
,
2
,
56
,
7
,
4
,
8
,
32
,
8
,
4
,
76
,
8
,
43
,
3
]
print
(
list
(
dedupe
(
a
)
)
)
切片
# 切片命名
a
=
'----80-34--------8000---'
b
=
'----60-22--------7867---'
iid
=
slice
(
4
,
6
)
age
=
slice
(
7
,
9
)
salary
=
slice
(
17
,
21
)
print
(
a
[
iid
]
)
print
(
b
[
salary
]
)
分组
# 分组
from
operator
import
itemgetter
from
itertools
import
groupby
rows
=
[
{
'address'
:
'5412 N CLARK'
,
'date'
:
'07/01/2012'
}
,
{
'address'
:
'5148 N CLARK'
,
'date'
:
'07/04/2012'
}
,
{
'address'
:
'5800 E 58TH'
,
'date'
:
'07/02/2012'
}
,
{
'address'
:
'2122 N CLARK'
,
'date'
:
'07/03/2012'
}
,
{
'address'
:
'5645 N RAVENSWOOD'
,
'date'
:
'07/02/2012'
}
,
{
'address'
:
'1060 W ADDISON'
,
'date'
:
'07/02/2012'
}
,
{
'address'
:
'4801 N BROADWAY'
,
'date'
:
'07/01/2012'
}
,
{
'address'
:
'1039 W GRANVILLE'
,
'date'
:
'07/04/2012'
}
,
]
rows
.
sort
(
key
=
itemgetter
(
'date'
)
)
for
date
,
items
in
groupby
(
rows
,
itemgetter
(
'date'
)
)
:
print
(
date
)
for
i
in
items
:
print
(
i
)