目录
字典是可以存储一组数据的对象,字典中的每个元素都有两个部分,key和value。
类似java的Hashmap。
-
创建和检索字典
empty_dic = {}
empty_dic2 = dict()
prices = {'apple':3, 'banana':4, 'orange':5}
print(prices['banana'])
print(prices['peach']) #这里会抛异常
if 'peach' in prices :
print(prices['peach'])
else :
print('we do not find price of peach')
用大括号或者dict()创建字典。用方括号检索字典。
key不存在则抛出KeyError异常,因此可以用in或not in来判断字典中是否存在某个key。
-
添加或更新字典元素
prices = {'apple':3, 'banana':4, 'orange':5}
prices['apple'] = 6
prices['grape'] = 10
print(prices)
字典是可变对象。
字典的key必须是不可变对象,如字符串、整数、浮点数或者元组,不能是列表或者其他可变对象。字典的value可以是任何类型的对象。
如果key已经存在,则更新其value。否则将key和value加入到字典。
-
删除字典元素
prices = {'apple':3, 'banana':4, 'orange':5}
del prices['banana']
print(prices)
del prices['peach'] #这里会抛异常
用del语句删除字典中现有的键值对。
-
获取字典元素数量
prices = {'apple':3, 'banana':4, 'orange':5}
print(len(prices))
用len()函数
-
for循环遍历字典
prices = {'apple':3, 'banana':4, 'orange':5}
for key in prices :
print(f'name={key}, price={prices[key]}')
for循环按key进行迭代
-
字典常用方法
prices = {'apple':3, 'banana':4, 'orange':5}
prices.clear()
print(prices)
prices = {'apple':3, 'banana':4, 'orange':5}
prices.get('apple')
prices.get('peach')
prices.get('peach', -1)
for key, value in prices.items() :
print(key, value)
for key in prices.keys() :
print(key)
for value in prices.values() :
print(value)
prices = {'apple':3, 'banana':4, 'orange':5}
value = prices.pop('apple')
print(prices)
prices = {'apple':3, 'banana':4, 'orange':5}
k,v = prices.popitem()
print(k, v, prices)
clear:清空字典
get:获取key对应的value,key不存在时不会抛异常,还能设置key不存在时的默认返回
items,keys、values: 分别返回 所有键值对,或所有key,或所有value。
pop:与get类似,区别有两个,一是如果不设置默认返回则会抛异常,二是还会把对应的键值对删掉。
popitem:删除一个最后添加的键值对(3.7版本之前是随机的),并将其作为元组返回。空集合上调用会抛异常。
-
字典解析
numbers = [1,2,3,4,5]
squares = {item:item**2 for item in numbers}
#结果表达式
#迭代表达式
#上面一句等价于下面三行
#squares = {}
#for item in numbers :
# squares[item] = item **2
numbers = [1,2,3,4,5]
squares = {item:item**2 for item in numbers}
squares_copy = {k:v for k,v in squares.items()}
print(squares_copy)
和前面的列表解析类似,也支持if子句。
-
集合
集合中的元素是唯一的,无序的。甚至可以是不同类型的。
-
创建集合
empty_set = set()
my_set = set(['a','b','c']) #把列表变为集合
print(my_set)
myset = set('abc') #把字符串变为集合
print(my_set)
myset = set('aabbcc') #集合元素不会重复
print(my_set)
print(len(my_set)) #获取集合元素的数量
#下面是错误的
myset = set('a', 'b','c')
必须使用内置的set()函数创建集合
-
添加和删除集合元素
myset = set()
myset.add(1)
myset.add(2)
myset.update([3,4,5])
myset.remove(5)
myset.discard(6)
print(myset)
myset.clear()
print(myset)
add添加元素
update可更新一组元素
remove和discard类似,但后者在找不到元素时不会抛出KeyError异常
-
for循环遍历集合
myset = set([1,2,3])
for val in myset :
print(val)
-
判断是否在集合中
myset = set(range(5))
if 5 not in myset :
print('param stop not included in range')
-
并集
print(set([1,2,3]).union(set([4,5,6])))
print(set([1,2,3]) | set([4,5,6]))
用集合的union方法,或者 | 运算符,求两个集合的并集
-
交集
print(set([1,2,3]).intersection(set([2,3,4,5,6])))
print(set([1,2,3]) & set([2,3,4,5,6]))
用集合的intersection方法,或者 & 运算符,求两个集合的并集
-
差集
set1 = set([1,2,3])
set2 = set([2,3,4,5,6])
print(set1.difference(set2))
print(set1 - set2)
print(set2.difference(set1))
print(set2 - set1)
用集合的difference方法,或者 - 运算符,求两个集合的差集(在前一个集合中但不在后一个集合中)
-
对称差集
set1 = set([1,2,3])
set2 = set([2,3,4,5,6])
print(set1.symmetric_difference(set2))
print(set1 ^ set2)
print(set2.symmetric_difference(set1))
print(set2 ^ set1)
用集合的symmetric_difference方法,或者 ^ 运算符,求两个集合的对称差集(只出现在其中一个集合中的元素)
-
子集和超集
set1 = set([1,2,3])
set2 = set([1,2,3,4,5,6])
print(set1.issubset(set2))
print(set1 <= set2)
print(set2.issuperset(set1))
print(set2 >= set1)
-
集合解析
set1 = set([0,1,2,3])
set2 = set(item**2 for item in set1 if item > 0)
print(set2)
类似列表解析和字典解析
-
序列化对象
在python中,对象序列化可称为pickling
import pickle
prices = {'apple':3, 'banana':4, 'orange':5}
file = open('prices.dat', 'wb')
pickle.dump(prices, file)
file.close()
file = open('prices.dat', 'rb')
prices2 = pickle.load(file)
print(prices2)
file.close()
pickle模块的dump和load函数用来序列号和反序列化
上一篇:python基础05——字符串