1"""
2python的基本数据结构
3
4- 序列
5 1. 字符串 str
6 2. 列表 list (列表推导式) data_list.py
7 3. 元组 tuple
8- 集合 set
9- 字典 dict
10- 其他的编程技巧
11"""
12
13
14def __str():
15 """
16 字符串不可变(immutable)
17 """
18 print('who are you')
19 # using \ to eacape.
20 print('I\'m robot')
21 # if u don't want to escape a str, like file path, use 'r' before quote.
22 print(r'C:\file\path')
23 # dispite the + calculation,
24 # python also support * calculation, cool
25 print('holy sh' + 7 * 'i' + 't!')
26
27 # TypeError
28 # str = 'python'
29 # str[0] = 'P'
30
31
32__str()
33
34
35def __str_index_and_slice():
36 """
37 在python中, 字符串是一种序列, 除了基本的操作之外, 还有序列的一些操作.
38 比如索引范访问, 切片等等.
39
40 索引会越界, 但是切片不会. 不过不要去在切片里故意越界, 那样不好玩.
41
42 The index could be understand like:
43
44 +---+---+---+---+---+---+
45 | p | y | t | h | o | n |
46 +---+---+---+---+---+---+
47 0 1 2 3 4 5 6
48 -6 -5 -4 -3 -2 -1
49 """
50 # index access, index could be negtive
51 s = 'py' 'thon'
52 print(s[0]) # p
53 print(s[5]) # n
54 print(s[-4]) # t
55 print(s[0] == s[-0]) # True
56
57 # Slice 切片
58 print(s[2:4]) # th
59 print(s[2:]) # thon
60 print(s[:5]) # pytho
61 print(s[-3:]) # hon
62 print(s[:-3]) # pyt
63 print(s[-2:] == s[4:]) # True
64 print(s[:-2] == s[:4]) # True
65 print((s[:1] + s[1:]) == s) # True
66
67
68__str_index_and_slice()
69'''
70元组是不可变序列, 不支持修改元素值
71支持序列的一般操作(索引取值, 切片, in/not in等等).
72元组的元素可重复, 可为空
73一般使用'(x, y, z)'来定义元组, 但'()'并不是必须的. 定义元组必须的其实是','
74'''
75t = 1, 'amg', None, 'amg'
76print(t)
77# 可嵌套
78u = 'mpower', t
79print(u)
80# 定义空元组时, () 是必须的
81emptyt = ()
82# 定义单元素的元组时, ','是必须的
83singleton = 'hello',
84v = 1, '2', [3, 4]
85print(v) # (1, '2', [3, 4])
86# 不可修改
87# v[0] = 10 # TypeError: 'tuple' object does not support item assignment
88# "可修改" 和Java的final关键字语义类似, 对象的引用不可变.
89v[2][1] = 9
90print(v) # (1, '2', [3, 9])
91'''
92集合 set 是由不可重复的元素组成的无序[容器].
93集合不能包含None.
94集合不是序列, 不支持序列的操作!
95集合支持合(并)集, 交集, 差集 对称差分等数学运算
96https://zh.wikipedia.org/wiki/%E9%9B%86%E5%90%88%E4%BB%A3%E6%95%B0
97
98创建空集合只能用set()方法, 不能用{}, 因为{}用来创建空字典.
99'''
100basket = {"apple", 'orange', 'apple', 'pear', 'banaba', None}
101print(basket) # {'pear', 'orange', 'apple', 'banana'}
102print('apple' in basket) # True
103w = set('abracadabra')
104x = set('alacazam')
105print(w)
106print(x)
107print(w & x) # 交集
108print(w | x) # 并集
109print(w - x) # 差集
110print(x - w) # 差集
111print(w ^ x) # 对称差集
112# 集合也支持推导式
113y = {x for x in 'abrhjschioqk' if x not in 'abc'}
114print(y)
115'''
116字典 dict . 即k-v键值对.
117字典的键可以是任何不可变类型: 数字, 字符串, 只包含字符串, 数字, 元组的元组和 None
118和集合一样, 字典可以通过 dict() 方法和 {}来创建
119关于字典的其他方法可以参见[API]
120https://docs.python.org/zh-cn/3/library/stdtypes.html#mapping-types-dict
121'''
122tel = {"jack": 1100, "rose": 1234}
123print(tel["jack"]) # 1100
124# 删除键值对
125del tel['rose']
126print(tel)
127# 添加键值对
128tel['rose'] = 5201
129# 使用list获取所有的键
130print(list(tel))
131# 使用构造器
132# 可迭代对象作为实参,可迭代对象中的每一项本身必须
133# 为一个刚好包含两个元素的可迭代对象.
134# 每一项中的第一个对象将成为新字典的一个键,
135# 第二个对象将成为其对应的值.
136# None 可以作为键
137z = dict(((1, 'tommy'), (2, 'high'), (3, 'lander'), (None, 'nb')))
138print(z)
139# 还有其他的构造器(不使用位置参数)
140k = dict(b='apple', t='pc', v='macOS')
141print(k) # {'b': 'apple', 't': 'pc', 'v': 'macOS'}
142# 还可以使用字典推导式
143j = {x: x ** 2 for x in range(5)}
144print(j)
145# 遍历字典的方式 类似的方法有 keys() values()
146# 类似于java的entry
147for key, value in z.items():
148 print(key, value)
149# 还有一个牛逼的函数 zip
150# 后文将单独介绍
151# 实际上是将多个元组的元素依次映射,形成一个字典
152zipped = dict(zip(['generic', 'track', 'year'],
153 ['classical', 'Requiem in D minor', '1673']))
154print(zipped)