1、数据类型的补充
1、1 元组
当元组里面只有一个元素且没有逗号时,则该数据的数据类型与括号里面的元素相同。
tu1 = (‘laonanhai‘) tu2 = (‘laonanhai‘) print(tu1, type(tu1)) print(tu2, type(tu2),)
tu1 = (1) tu2 = (1,) print(tu1, type(tu1)) print(tu2, type(tu2))
tu1 = ([1, 2, 3]) tu2 = ([1, 2, 3],) print(tu1, type(tu1)) print(tu2, type(tu2))
1、2 list 列表
在循环一个列表时,最好不要改变列表的大小,会影响你的最终结果。
li = [111, 222, 333, 444, 555,],索引为奇数的所有元素全部删除。
方法一:
l1 = [111, 222, 333, 444, 555, ] del l1[1::2] print(l1)
方法二(错误展示):
l1 = [111, 222, 333, 444, 555, ] for index in range(len(l1)): print(‘删除之前的index:%s‘ % index) print(‘删除之前的l1:%s‘ % l1) if index % 2 == 1: del l1[index] print(‘删除之后的index:%s‘ % index) print(‘删除之后的l1:%s‘ % l1) print(l1)
方法三(倒着删):
l1 = [111, 222, 333, 444, 555, ] for index in range(len(l1) - 1, -1, -1): if index % 2 == 1: del l1[index] print(l1)
方法四(自己做的):
l1 = [111, 222, 333, 444, 555, 666, 777, 888, 999, ] i = len(l1) count = 1 s = int(len(l1)/2) for j in range(s): del l1[count] count += 1 print(l1)
1、3 字典
1、3、1 dict.fromkeys(A, B) A为可迭代对象,B任意。创建一个字典,迭代A的最小元素为键,B为值。
dic = dict.fromkeys(‘abc‘, 666) print(dic)
dic = dict.fromkeys([11, 22, 33], 666) print(dic)
dic = dict.fromkeys([1, 2, 3], []) dic[1].append(666) print(dic)
1、3、2 在循环字典时,最好不要改变字典的大小,会影响结果或者报错。
错误示例:
dic = {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘, ‘k3‘: ‘v3‘, ‘name‘: ‘alex‘} for i in dic: if ‘k‘ in i: del dic[i] print(dic)
dic = {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘, ‘k3‘: ‘v3‘, ‘name‘: ‘alex‘} l1 = [] for key in dic: if ‘k‘ in key: l1.append(key) print(l1) for key in l1: del dic[key] print(dic)
1、4 数据类型的转换
str ------> list:
split
list ----->str:
join
tuple ------> list:
tu1 = (1, 2, 3) l1 = list(tu1) print(l1)
list ------> tuple:
tu1 = (1, 2, 3) l1 = list(tu1) tu2 = tuple(l1) print(tu2)
dic ------> list:
list(dic) 列表中只有key。
dic = {‘k1‘: ‘v1‘, ‘k2‘: ‘v2‘,‘k3‘: ‘v3‘,} l1 = list(dic) print(l1) print(list(dic.keys())) print(list(dic.values())) print(list(dic.items()))
0, ‘‘,[], {},() ------> bool都是False。
print(bool(0)) print(bool(‘‘)) print(bool(())) print(bool([])) print(bool({}))
print(bool([0, 0, 0]))
2、set 集合
set1 = {1, 2, 3, ‘abc‘, (1, 2, 3), True, } print(set1)
2、1 去重
集合去重。
set2 = {11, 11, 11, 22} print(set2)
列表的去重。
l1 = [11, 11, 22, 22, 33, 33, 33, 44] l2 = list(set(l1)) l2.sort() print(l2)
2、2 增
__.add(‘A‘) A为添加的内容,随机添加。
set1 = {‘alex‘, ‘WuSir‘, ‘RiTiAn‘, ‘egon‘, ‘barry‘} set1.add(‘太白‘) print(set1)
__.update(‘A‘) A为添加的可迭代内容,将A拆分为最小单元然后迭代添加。
set1 = {‘alex‘, ‘WuSir‘, ‘RiTiAn‘, ‘egon‘, ‘barry‘} set1.update(‘abc‘) print(set1)
set1 = {‘alex‘, ‘WuSir‘, ‘RiTiAn‘, ‘egon‘, ‘barry‘} set1.update([111, 2222, 333]) print(set1)
2、3 删
__.remove(‘A‘) 按元素删除。A为需要删除的内容。
set1 = {‘alex‘, ‘WuSir‘, ‘RiTiAn‘, ‘egon‘, ‘barry‘} set1.remove(‘RiTiAn‘) print(set1)
__.pop() 随机删除,有返回值。
set1 = {‘alex‘, ‘WuSir‘, ‘RiTiAn‘, ‘egon‘, ‘barry‘} print(set1.pop()) print(set1)
__.clear() 清空集合。 空集合为set()。
set1 = {‘alex‘, ‘WuSir‘, ‘RiTiAn‘, ‘egon‘, ‘barry‘} set1.clear() print(set1)
del __ 删除集合。
set1 = {‘alex‘, ‘WuSir‘, ‘RiTiAn‘, ‘egon‘, ‘barry‘} del set1 print(set1)
2、4 查
for循环。
set1 = {‘alex‘, ‘WuSir‘, ‘RiTiAn‘, ‘egon‘, ‘barry‘} for i in set1: print(i)
2、5 交集
交集:__ & __ 或者 __.intersection(__)
set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} set3 = set1 & set2 print(set3)
set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} set3 = set1.intersection(set2) print(set3)
2、6 并集
并集:__ | __ 或者 __.union(__)
set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} set3 = set1 | set2 print(set3)
set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} set3 = set1.union(set2) print(set3)
2、7 差集
差集:__ - __ 或者 __.difference(__)
set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} set3 = set1 - set2 print(set3) # set1独有的
set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} set3 = set1.difference(set2) # set1独有的 print(set3)
2、8 反交集
反交集:__ ^ __ 或者 __.symmetric_difference(__)
set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} set3 = set1 ^ set2 print(set3)
set1 = {1, 2, 3, 4, 5} set2 = {4, 5, 6, 7, 8} set3 = set1.symmetric_difference(set2) print(set3)
2、9 子集
子集:__ < __ 或者 __.issubset(__)
set1 = {1, 2, 3} set2 = {1, 2, 3, 4, 5, 6} print(set1 < set2) # True set1 是set2 的子集
set1 = {1, 2, 3} set2 = {1, 2, 3, 4, 5, 6} print(set1.issubset(set2)) # True set1是set2的子集
2、10 超集
set1 = {1, 2, 3} set2 = {1, 2, 3, 4, 5, 6} print(set2 > set1) # set2 是 set1 的超集
set1 = {1, 2, 3} set2 = {1, 2, 3, 4, 5, 6} print(set2.issuperset(set1)) # set2 是 set1 的超集
frozenset() frozenset是冻结的集合,它是不可变的,存在哈希值,好处是它可以作为字典的key,也可以作为其它集合的元素。缺点是一旦创建便不能更改,没有add,remove方法。
set1 = frozenset({1, 2, 3, ‘alex‘}) print(set1)
3、深浅copy
对于赋值运算来说,指向的都是同一个内存地址,一变都变。
l1 = [1, 2, 3] l2 = l1 l3 = l2 l3.append(666) print(l1, l2, l3)
3、1 浅copy
copy.() 浅copy
l1 = [11, 22, 33] l2 = l1.copy() l1.append(666) print(l1, id(l1)) print(l2, id(l2))
当列表内层列表增加元素时,浅copy跟随变化。内层的列表同样是同一个地址。
对于浅copy来说,第一层创建的是新的内存地址,从第二层开始,指向的都是同一个内存地址,所以,对于第二层以及更深的层数来说,保持一致性。
l1 = [11, 22, [‘barry‘, [55, 66]], [11, 22]] l2 = l1.copy() l1[2].append(‘alex‘) print(l1, id(l1)) print(l2, id(l2)) print(l1, id(l1[-1])) print(l2, id(l2[-1]))
3、2 深copy
import copy
import copy l1 = [11, 22, 33] l2 = copy.deepcopy(l1) l1.append(666) print(l1, id(l1)) print(l2, id(l2))
import copy l1 = [11, 22, [‘barry‘]] l2 = copy.deepcopy(l1) l1[2].append(‘alex‘) print(l1, id(l1[-1])) print(l2, id(l2[-1]))
深copy 完全独立。
l1 = [1, 2, 3] l2 = [1, 2, 3] l1.append(666) print(l1, id(l1)) print(l2, id(l2))
对于切片来说,这是浅copy。
l1 = [1, 2, 3, 4, 5, 6, [11, 22]] l2 = l1[:] l1.append(666) print(l1, l2)
l1 = [1, 2, 3, 4, 5, 6, [11, 22]] l2 = l1[:] l1[-1].append(666) print(l1, l2)
原文地址:https://www.cnblogs.com/ZN-225/p/9587953.html