标签:转化 dict color item 结果 join 影响 pop set
遍历字典的几种方法:
dic = {‘a‘: ‘1‘, ‘b‘: ‘2‘, ‘c‘: ‘3‘} # 1 遍历key值 # for key in dic: # print(key, dic[key]) # # for key in dic.keys(): # print(key, dic[key]) # a 1 b 2 c 3 # 2 遍历value值 # for value in dic.values(): # print(value) # 1 2 3 # 3 遍历字典项 # for kv in dic.items(): # print(kv) # (‘a‘, ‘1‘) (‘b‘, ‘2‘) (‘c‘, ‘3‘) # 4 遍历key键值 # for key,value in dic.items(): # print(key + ‘:‘ + value) # # for (key, value) in dic.items(): # print(key + ‘:‘ + value) # a:1 b:2 c:3
utf - 8 和 gbk 的转化
# s1 = ‘中国‘ # b1 = s1.encode(‘utf-8‘) # # print(b1) # # utf-8 bytes --->gbk bytes # s2 = b1.decode(‘utf-8‘) # # print(s2) # b2 = s2.encode(‘gbk‘) # print(b2)
基础数据类型的补充
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))
2 列表
#索引为奇数对应的所有元素全部删除。
#方法一:
# l1 = [111, 222, 333, 444, 555, ]
# del l1[1::2] # print(l1)
#方法二错误展示:
# 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)
# 在循环一个列表时,最好不要改变列表的大小,会影响你的最终结果。
# 倒着删除。
# for index in range(len(l1)-1, -1, -1): # if index % 2 == 1: # del l1[index] # print(l1)
3 字典
fromkeys()
# 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)
# 在循环dict中,最好不要改变dict的大小,会影响结果或者报错。
删除字典键带有“k”的值 先把带有“k”的键放进新的列表里 然后从列表中删除
# 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)
# 数据类型的转换:
str ---> list split
list --> str join
tuple <---> list 直接转换
l1 = list(tu1)
print(l1)
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 都是Fasle
集合
集合是一个可变的数据类型,他是以{}形式存在的,空集合set(),
但是它要求它里面的元素是不可变的,集合是无序的,不重复的。
# set1 = {1, 2, 3, ‘abc‘, (1,2,3), True, }
1,去重。
# 列表的去重
# l1 = [11, 11, 22, 22, 33, 33, 33, 44] # l2 = list(set(l1)) # l2.sort() # 正序排列
# print(l2)
# set1 = {‘alex‘, ‘WuSir‘, ‘RiTiAn‘, ‘egon‘, ‘barry‘}
# 增
# set1.add(‘太白‘) # print(set1)
# set1.update(‘abc‘) # set1.update([111, 2222, 333]) # print(set1)
# 删
# remove按元素删除 # set1.remove(‘RiTiAn‘) # print(set1)
# 随机删除,有返回值 pop # print(set1.pop()) # print(set1)
# 清空clear # set1.clear() # print(set1)
# 删除整体 del # del set1 # print(set1)
# 查
只能用for 循环
# for i in set1: # print(i)
2,关系测试。
# set1 = {1, 2, 3, 4, 5}
# set2 = {4, 5, 6, 7, 8}
# 交集 & intersetion
# set3 = set1 & set2 # print(set3) # print(set1.intersection(set2))
# 并集 | union
# print(set1 | set2) # print(set1.union(set2))
# 差集 - difference
# print(set1 - set2) # set1 独有 # print(set2.difference(set1)) # set2 独有
# 反交集 ^ symmetric_difference
# print(set1 ^ set2) # print(set1.symmetric_difference(set2))
# set1 = {1, 2, 3}
# set2 = {1, 2, 3, 4, 5, 6}
# 子集
# print(set1 < set2) # True set1 是set2的子集 # print(set1.issubset(set2)) # True set1 是set2的子集
# 超集
# print(set2 > set1) # True set2 是set1的超集 # print(set2.issuperset(set1)) # True set2 是set1的超集
# frozenset 冻集合 把元组 变为可变的
set1 = frozenset({1, 2, 3, ‘alex‘}) print(set1,type(set1))
深浅 copy
# 对于赋值运算来说,指向的都是同一个内存地址,一变都变。
l1 = [1, 2, 3] l2 = l1 l3 = l2 l3.append(666) print(l1, l2, l3)
# 浅copy
#对于浅copy来说,对于浅copy来说,第一层创建的是新的内存地址,而从第二层开始
# ,指向的都是同一个内存地址,所以,对于第二层以及更深的层数来说,保持一致性。
# l1 = [11, 22, 33] # l2 = l1.copy() # l1.append(666) # print(l1, id(l1)) # print(l2, id(l2))
# 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]))
# 深copy deepcopy
深copy 完全独立。
import copy 引入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, 4, 5, 6, [11,22]] # l2 = l1[:] # l1.append(666) # print(l1, l2) # l1[-1].append(666) # print(l1, l2)
python 学习 D7 遍历字典的集中方法 集合的作用 以及增删查
标签:转化 dict color item 结果 join 影响 pop set
原文地址:https://www.cnblogs.com/HoneyTYX/p/9003646.html