Python常用数据结构:元组,列表,字典,集合
目录
1.字典(哈希表或关联数组)
字典是拥有灵活尺寸的键值对,键和值都是Python的对象;
{}是创建字典的方式,使用逗号来分隔。
1.1基础操作
empty_dict={}
d1={'Andrew', 'wiggins', 'Luka': 'Donic','Leborn':'James'}
d1
#输出:{'Andrew': 'wiggins', 'Luka': 'Donic', 'Leborn': 'James'}
#字典插入元素
d1[3]='Curry'
d1
#输出:{'Andrew': 'wiggins', 'Luka': 'Donic', 'Leborn': 'James', 3: 'Curry'}
#查找键对应的值
d1['Andrew']
#输出:'wiggins'
#判断某个键是否在字典中
3 in d1
#输出:True
#删除键值对
del d1[3] #只删除不返回值
d1
#输出:{'Andrew': 'wiggins', 'Luka': 'Donic', 'Leborn': 'James'}
ret=d1.pop('Leborn') #删除后返回值
ret
#输出:'James'
d1
#输出:{'Andrew': 'wiggins', 'Luka': 'Donic'}
#键的列表
list(d1.keys())
#输出:['Andrew', 'Luka']
#值的列表
list(d1.values())
#输出:['wiggins', 'Donic']
#更新字典(已经存在键,则更新值;若键不存在,则添加键值对)
d1.update({'Luka':'Magic','3':'Curry'})
d1
#输出:{'Andrew': 'wiggins', 'Luka': 'Magic', '3': 'Curry'}
1.2从序列产生字典
key_list=['Andrew','Luka','Leborn']
value_list=['wiggins','Donic','James']
mapping={}
for key,value in zip(key_list,value_list):
mapping[key]=value
mapping
#输出:{'Andrew': 'wiggins', 'Luka': 'Donic', 'Leborn': 'James'}
map0=dict(zip(key_list,value_list))
map0
#输出:{'Andrew': 'wiggins', 'Luka': 'Donic', 'Leborn': 'James'}
1.3默认值
key_list=['Andrew','Luka','Leborn']
value_list=['wiggins','Donic','James']
mapping={}
for key,value in zip(key_list,value_list):
mapping[key]=value
mapping
#输出:{'Andrew': 'wiggins', 'Luka': 'Donic', 'Leborn': 'James'}
map0=dict(zip(key_list,value_list))
map0
#输出:{'Andrew': 'wiggins', 'Luka': 'Donic', 'Leborn': 'James'}
字典的值可以是任何对象,但键必须是不可变的对象,即数字,字符串或元组(元组内的对象也是不可变的)。通常用hash函数判断一个对象是否可以哈希化(若可以,则不可变,可以用作键)
hash([1,2])---------------------------------------------------------------------------
#TypeError Traceback (most recent call last)
#Cell In[31], line 1
#----> 1 hash([1,2])
#
#TypeError: unhashable type: 'list'
hash(((1,2),3))
#输出:-333907151259015829
2.集合
具备集合的性质:无序且元素唯一
#集合的创建
#使用set()函数
a=set([1,2,3,4,5,6])
#使用大括号
b={4,5,6,7,8,9}
a.union(b) #或者a|b
#输出:{1, 2, 3, 4, 5, 6, 7, 8, 9}
a.intersection(b) #或者a&b
#输出:{4, 5, 6}
a.pop() #弹出首元素
#输出:1
a.remove(2) #删除某一元素
#判断a是否包含于b
a.issubset(b)
#输出:False
#判断a是否包含b
a.issuperset(a&b)
#输出:True
集合的元素必须是不可变的。
3.列表、集合和字典的推导式
推导式的作用是通过简洁的语法生成满足某一条件的列表、集合或字典,从而简化代码并提高可读性。
3.1列表推导式
推导式:[expr for val in collection if condition]
列表推导式中的expr是用来生成列表元素的表达式;
推导式即在val满足条件的情况下把expr添加到列表中
string=['a','ab','abc','abcd','abcde']
[x.upper() for x in string if len(x)<=3 ]
#输出:['A', 'AB', 'ABC']
#expr:x.upper()
#val:x
#condition:len(x)<=3
3.2集合推导式
推导式:{expr for val in collection if condition}
#获取每个字符串的长度
length={len(x) for x in string}
length
#输出:{1, 2, 3, 4, 5}
3.3字典推导式
推导式:{key-expr:value-expr for value in collection if condition}
#将字符串和其位置相互匹配
{val:index for index,val in enumerate(string)}}
#输出:{'a': 0, 'ab': 1, 'abc': 2, 'abcd': 3, 'abcde': 4}
3.4嵌套列表推导式
tuples=[(1,2),(3,4),(5,6)]
flat=[x for tup in tuples for x in tup]
flat
#输出:[1, 2, 3, 4, 5, 6]
#上面代码中的推导式相当于:
#for tup in tuples:
# for x in tup:
# flat.append(x)
#即两层循环,写推导式时注意顺序,若循环太多,推导式反而不易理解
参考书籍:《利用Python进行数据分析》Wes McKinney
喜欢的小伙伴还请点赞收藏,(❁´◡`❁)( o=^•ェ•)o ┏━┓