补充
列表:list
reverse,反转。
v1 = [1,2,3111,32,13]
#print(v1)
v1.reverse()
print(v1)
sort
v1 = [11,22,3111,32,13]
print(v1)
v1.sort(reverse=False) # 从小到大(默认)
v1.sort(reverse=True) # 从大到小
print(v1)
字典
-
- keys/values/items
-
-
get
info = {‘k1’:‘v1’,‘k2’:‘v2’}# v1 = info['k11111'] # v2 = info.get('k1111') # None就是Python中的空 # v3 = info.get('k1111',666) # print(v2) # None数据类型,改类型表示空(无任何功能,专门用于提供空值)
-
pop
info = {‘k1’:‘v1’,‘k2’:‘v2’}
result = info.pop(‘k2’)
print(info,result)del info['k1']
-
update
info = {‘k1’:‘v1’,‘k2’:‘v2’}# 不存在,则添加/存在,则更新 info.update({'k3':'v3','k4':'v4','k2':666}) print(info)
-
- 判断一个字符串中是否有敏感字符?
-
str
v = “Python全栈21期”if "全栈" in v: print('含敏感字符')
-
list/tuple
v = [‘alex’,‘oldboy’,‘藏老四’,‘利奇航’]if "利奇航" in v: print('含敏感')
-
- dict
v = {‘k1’:‘v1’,‘k2’:‘v2’,‘k3’:‘v3’}
默认按照键判断,即:判断x是否是字典的键。
if ‘x’ in v:
pass
请判断:k1 是否在其中?
if ‘k1’ in v:
pass
请判断:v2 是否在其中?
方式一:循环判断
flag = ‘不存在’
for v in v.values():
if v == ‘v2’:
flag = ‘存在’
print(flag)
方式二:
if ‘v2’ in list(v.values()): # 强制转换成列表 [‘v1’,‘v2’,‘v3’]
pass
请判断:k2:v2 是否在其中?
value = v.get(‘k2’)
if value == ‘v2’:
print(‘存在’)
else:
print(‘不存在’)
集合
set:集合:无序的不可重合的
‘’’’’’
None
int
v1 = 123
v1 = int() --> 0
bool
v2 = True/False
v2 = bool() -> False
str
v3 = “”
v3 = str()
list
v4 = []
v4 = list()
tuple
v5 = ()
v5 = tuple()
dict
v6 = {}
v6 = dict()
set
v7 = set()
‘’’’’’
s = {} :空字典
s = set() :结合 print(
例子:
s = [1,1,2,2,3,3]
print(set(s) #{1,2,3}
add:集合添加
例子:
s = {‘1,2,3’}
c = s.add(‘4’)
print©
set1 = {‘1’,‘2’,‘3’,‘4’,‘5’}
set1.add(‘6’)
print(set1) # {‘5’, ‘4’, ‘3’, ‘1’, ‘6’, ‘2’}
update:迭代着增加 批量添加
set1 = {‘1’,‘2’,‘3’,‘4’,‘5’}
set1.update(‘AA2’)
print(set1) # {‘3’, ‘4’, ‘A’, ‘1’, ‘2’, ‘5’}
discard:删除
例子:
set1 = {‘1’,‘2’,‘3’,‘4’,‘5’}
set1.remove(‘1’) # 删除一个元素
print(set1)
set1.pop() # 随机删除一个元素
print(set1)
set1.clear() # 清空集合
print(set1)
del set1 # 删除集合
print(set1)
集合的其他操作:
.1 交集。(& 或者 intersection)
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 & set2) # {4, 5}
print(set1.intersection(set2)) # {4, 5}
2 并集。(| 或者 union)
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 | set2) # {1, 2, 3, 4, 5, 6, 7,8}
print(set2.union(set1)) # {1, 2, 3, 4, 5, 6, 7,8}
3 差集。(- 或者 difference)
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 - set2) # {1, 2, 3}
print(set1.difference(set2)) # {1, 2, 3}
4反交集。 (^ 或者 symmetric_difference)
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
print(set1 ^ set2) # {1, 2, 3, 6, 7, 8}
print(set1.symmetric_difference(set2)) # {1, 2, 3, 6, 7, 8}
内存储存
v1 = [11,22,33]
v2 = [11,22,33]
v1 = 666
v2 = 666
v1 = “asdf”
v2 = “asdf”
按理 v1 和 v2 应该是不同的内存地址。特殊:
- 整型: -5 ~ 256
- 字符串:“alex”,'asfasd asdf asdf d_asdf ’ ----“f_*” * 3 - 重新开辟内存。