-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamed_slice.py
68 lines (54 loc) · 1.47 KB
/
named_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
56
57
58
59
60
61
62
63
64
65
66
67
68
'''
File: named_slice.py
Project: 01-DataSturcture
===========
File Created: Monday, 20th July 2020 10:34:20 pm
Author: <<LanLing>> (<<[email protected]>>)
===========
Last Modified: Monday, 20th July 2020 10:34:25 pm
Modified By: <<LanLing>> (<<[email protected]>>>)
===========
Description: 切片命名
Copyright <<2020>> - 2020 Your Company, <<XDU>>
'''
from collections import namedtuple
string = "00110022"
# 左开右闭
# 直接切片会导致代码可读性降低
a = slice(2, 4)
b = slice(6, 8)
print(int(string[a]) + int(string[b]))
# 访问切片对象
a = slice(1, 100, 2)
print(a.start, a.step, a.stop)
# 提前命名,并按名层访问序列
# 第一个参数是类名
Subscriber = namedtuple('people', ['addr', 'joined'])
sub = Subscriber('[email protected]', '2012-10-19')
print(sub.addr, sub.joined)
# 实例
records = [
['a', 12, 12],
['b', 12, 12],
['c', 12, 12],
['d', 12, 12],
['e', 12, 12],
]
Stock = namedtuple('record', ['name', 'shares', 'price'])
def compute_cost(records):
total = 0.0
for rec in records:
s = Stock(*rec)
total += s.shares * s.price
return total
print(compute_cost(records))
# 数据增加一列, 原函数不用修改,修改命名即可
records1 = [
['a', 12, 'test', 12],
['b', 12, 'test', 12],
['c', 12, 'test', 12],
['d', 12, 'test', 12],
['e', 12, 'test', 12],
]
Stock = namedtuple('Stock', ['name', 'shares', 'test', 'price'])
print(compute_cost(records1))