-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseq_slice.py
55 lines (43 loc) · 1001 Bytes
/
seq_slice.py
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
'''
File: seq_slice.py
Project: DataSturcture
===========
File Created: Monday, 20th July 2020 10:48:44 am
Author: <<LanLing>> (<<[email protected]>>)
===========
Last Modified: Monday, 20th July 2020 11:42:46 am
Modified By: <<LanLing>> (<<[email protected]>>>)
===========
Description: 序列元素分解
Copyright <<2020>> - 2020 Your Company, <<XDU>>
'''
# 全部分解
data = (12, 34)
x, y = data
print(x, y)
# 忽略不想要的元素
data = [1, 2, 3, 4, [5, 6]]
_, a, _, _, b = data
print(a, b)
# 一个变量接收多个变量
# 计算学生的平均成绩
score = [1, 78, 87, 98, 100]
def avg_score(score):
first, *middle, last = score
print(sum(middle) / len(middle))
avg_score(score)
# 忽略多个变量
*_, last = score
print(last)
# 处理变长参数
records = [
('foo', 1, 2),
('bar', 'hello'),
('foo', 3, 4),
]
def do_foo(x, y):
print('foo', x, y)
def do_bar(s):
print('bar', s)
for tag, *args in records:
print(tag, '--->', *args)