Data type (IV)
Today Executive Summary
- set
- Memory-related
- Copy depth
Recap & supplements
Recap
supplement
List
Reverse: List .reverse ()
v1 = [11,22,33,44,55,66] print(v1) v1.reverse() print(v1)
In sequence: a listing .sort (reverse = False / Ture)
v1 = [111,2,33,454,5,89] v1.sort(reverse=False) #按从小到大排列 print(v1) #输出:[2, 5, 33, 89, 111, 454] v1.sort(reverse=True) #按从大到小排列 print(v1) #输出:[454, 111, 89, 33, 5, 2]
dictionary
get value
- List .get ( "key", the return value)
- If no get to the key, the second argument is returned, no return None (None save more memory)
v1 = {"k1":11,"k2":22,"k3":33} reset = v1.get("k1") #输出:11 reset = v1.get("k1","不存在") #输出:11 reset = v1.get("k111","不存在") #输出:不存在 reset = v1.get("k111") #输出:None #判断get取得值是否存在于字典中 info = {"k1":11,"k2":22,"k3":33} result = info.get("k111") if result: print("存在") else: print("不存在")
- List .get ( "key", the return value)
pop take Deleted values
# pop删的为键值对,赋值给新变量的是值 v1 = {"k1":11,"k2":22,"k3":33} result = v1.pop("k1") print(v1,result) #输出为:{'k2': 22, 'k3': 33} 11
del delete key-value pairs in the dictionary
v1 = {"k1":11,"k2":22,"k3":33} del v1["k1"] print(v1) #删除字典中的键值对
update will merge two dictionaries
- If there are duplicate key, the second covering the first dictionary in the dictionary duplicate values
info = {"k1":11,"k2":22,"k3":33} info.update({"k4":44,"k5":55,"k6":66}) print(info) #输出为:{'k1': 11, 'k2': 22, 'k3': 33, 'k4': 44, 'k5': 55, 'k6': 66}
After :( summary method often used)
- Dictionary .keys
- Dictionary .values
- Dictionary .items
- Dictionary .get
3. To determine whether a character sensitive character
str
v = "去你大爷家玩去吧" if "你大爷" in v: print('含敏感字符')
list / tuple
v = ['yanan','海燕','成龙','alex'] if 'yanan' in v: print('含敏感字符')
dict
info = {'k1': 11, 'k2': 22, 'k3': 33, 'k4': 44, 'k5': 55, 'k6': 66} #默认按照键判断,即:判断"k"是否是字典的键 if "k" in info: pass #判断字典中值是否存在“22” #方法一 flag = "不存在" for v in info.values(): if v == 22: flag = "存在" break print(flag) #方法二 if 22 in list(info.values()): print("存在") #list(info.values())表示将info的值强制转换为列表,整个循环在列表中判断 #判断字典中键值对是否存在"k1":11 value = info.get("k1") if value == 11: print("存在")
Sensitive character exercises
#info为敏感字符列表,用户输入后判断是否包含敏感字符,如包含,打印“包含”,如不包含,打印输入内容 info = ['大爷','炸弹','无所谓'] prop = input("请输入:") flag = True for v in info: if v in prop: flag = False break if flag: print(prop) else: print("包含敏感字符")
Content Today
1. collection: set
- Disorderly
- No duplicate values
v = set() #表示空集合
v = {1,2,3,4,5,6,7} #跟字典比没有键值对,只有单一元素
Unique features:
add: add an element to the collection
v = {'明天',"白天","雨天"}
v.add("晴天")
print(v) #输出:{'晴天', '白天', '雨天', '明天'}
discard: delete elements in the collection
v = {'明天',"白天","雨天"}
v.discard("白天")
print(v) #输出:{'明天', '雨天'}
update: Batch Add
v = {'明天',"白天","雨天"}
v.update({11,22,33,44})
print(v) #输出:{33, '明天', '白天', 22, 11, '雨天', 44}
intersection: the intersection (the new value is received)
- Take the intersection of two sets, if not return the empty set
- The second may be a list / tuple / set
v = {'明天',"白天","雨天"}
result = v.intersection({"白天","雨天","晴天"})
print(result) #输出为{'雨天', '白天'}
union: union (new value received)
- And set to take two sets, two sets combined
- The second may be a list / tuple / set
v = {'明天',"白天","雨天"}
result = v.union({"白天","雨天","晴天"})
print(result) #输出为{'明天', '白天', '雨天', '晴天'}
difference: difference set (new value received)
- Take the first set with a second set of elements is not
- The second may be a list / tuple / set
v = {'明天',"白天","雨天"}
result = v.difference({"白天","雨天","晴天"})
print(result) #输出为{'明天'}
symmetric_difference: symmetric difference (the new value of the reception)
- Take two sets of elements are not
- The second may be a list / tuple / set
v = {'明天',"白天","雨天"}
result = v.symmetric_difference({"白天","雨天","晴天"})
print(result)
Public functions:
Only ()
v = {'明天',"白天","雨天"}
print(len(v))
for loop
v = {'明天',"白天","雨天"}
for item in v:
print(item)
- Index (no)
- Step (no)
- Slice (no)
- Delete / modify (no)
Nesting
- List / Dictionary / collection -> can not be placed in the collection
- List / Dictionary / collection -> can not be used as a dictionary key (unhashable)
hash
- Because the hash algorithm will internally and get a numerical value (corresponding to a memory address), the user to quickly find a target element
- Orderly before being hashed (str / int / list / tuple / dict key)
- And a collection of dictionaries is to find the fastest, faster than the list
v = ['明天',"白天","雨天"]
val = hash(v[0])
print(val)
2. memory-related
note:
Re-assignment and modify memory addresses are not the same
Commonly used int and str will do cache
Like int (-5 ~ 256) memory addresses, cache phenomenon
str (+ alphabetic characters, multiple occurrences of memory address will be different)
v1 = "as&d"*2 v2 = "as&d"*2 print(id(v1),id(v2)) #内存地址不同
Example 1:
v1 = [11,22,33] v2 = [11,22,33] #所指的不是同一块内存 v1 = 666 v2 = 666 #所指的是同一块内存(-5至256范围内存地址一样,范围外不一样) v1 = "asdf" v2 = "asdf" #所指的是同一块内存()
Example Two:
v1 = [11,22,33,44] v1 = [11,22,33]
Example Three:
v1 = [11,22,33] v2 = v1 #练习1:(内部修改) v1 = [11,22,33] v2 = v1 v1.append(999) print(v2) #含 999 #练习2:(赋值) v1 = [11,22,33] v2 = v1 v1 = [1,2,3,4] print(v2) #练习3:(重新赋值) v1 = "yanan" v2 = v1 v1 = "zhoujielun" print(v2)
Example Four:
v = [1,2,3] values = [11,22,v] #练习1: v.append(9) print(values) #[11,22,[1,2,3,9]] #练习2: values[2].append(999) print(v) #[1,2,3,999] #练习3: v = 999 print(values) # [11,22,[1,2,3]] #练习4: values[2] = 666 print(v) #[1,2,3]
Example five:
v1 = [1,2] v2 = [2,3] v3 = [11,22,v1,v2]
View memory address:
- id (variable)
v={"k1":1,"k2":2} print(id(v))
to sum up
- List list
- reverse
- sort
- Dictionary dict
- get (Key)
- update
- Set set (random, non-repeating)
- add
- discard
- update
- intersection (Key)
- union
- difference
- special
- Nesting: collection / dictionary of key
- Empty set: set ()
- id
- type
- Nested Application (focus)
- Assignment
- Modify the internal elements: a list / dictionary / collection