7.Python-字典

本文展示了如何在Python中创建、访问和操作字典,包括创建空字典、获取字典值、复制字典、获取键、值和项目、使用in和notin检查键、使用get方法安全获取值以及按键和值对字典进行排序。还强调了字典与列表的不同特性,如动态添加元素和非数字索引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.创建字典 

empty_dict={}
a_dict={'one':1,'two':2,'three':3}
print("{}".format(a_dict))
print("a_dict has {!s} elements".format(len(a_dict)))
another_dict={'x':'printer','y':5,'z':['star','circle',9]}
print("{}".format(another_dict))
print("another_dict also has {!s} elements".format(len(another_dict)))

#结果
{'one': 1, 'two': 2, 'three': 3}
a_dict has 3 elements
{'x': 'printer', 'y': 5, 'z': ['star', 'circle', 9]}
another_dict also has 3 elements

2.引用字典中的值 

a_dict={'one':1,'two':2,'three':3}
another_dict={'x':'printer','y':5,'z':['star','circle',9]}
print("{}".format(a_dict['two']))
print("{}".format(another_dict['z']))

#结果 
2
['star', 'circle', 9]

3.复制 

a_dict={'one':1,'two':2,'three':3}
a_new_dict=a_dict.copy() 
print("{}".format(a_new_dict))

#结果  
{'one': 1, 'two': 2, 'three': 3}

4.键-值-项目

a_dict={'one':1,'two':2,'three':3}
print("{}".format(a_dict.keys())) 
a_dict_keys=a_dict.keys() 
print("{}".format(a_dict_keys))
print("{}".format(a_dict.values))
print("{}".format(a_dict.items))


#结果 
dict_keys(['one', 'two', 'three'])
dict_keys(['one', 'two', 'three'])
<built-in method values of dict object at 0x000001DA91EE8980>
<built-in method items of dict object at 0x000001DA91EE8980>

5.使用in,not in,get 

a_dict={'one':1,'two':2,'three':3}
another_dict={'x':'printer','y':5,'z':['star','circle',9]}
if 'y' in another_dict:
	print("y is a key in another_dict:{}".format(another_dict.keys()))
if 'c' not in another_dict:
	print("c is not a key in another_dict:{}".format(another_dict.keys()))
print("{!s}".format(a_dict.get('three')))
print("{!s}".format(a_dict.get('four')))  #不存在的键返回None,
print("{!s}".format(a_dict.get('four','Not in dict'))) #键four不存在时返回:Not in dict 


#结果 
y is a key in another_dict:dict_keys(['x', 'y', 'z'])
c is not a key in another_dict:dict_keys(['x', 'y', 'z'])
3
None
Not in dict

6.排序

#使用sorted()对字典进行排序
a_dict={'one':1,'two':2,'three':3}
print("{}".format(a_dict))
dict_copy=a_dict.copy() 
ordered_dict1=sorted(dict_copy.items,key=lambda item: item[0]) #根据键升序
print("order by keys:{}".format(ordered_dict1))
ordered_dict2=sorted(dict_copy.items(),key=lambda item: item[1]) #根据values升序
print("order by values:{}".format(ordered_dict2))
ordered_dict3=sorted(dict_copy.items(),key=lambda x: x[1],reverse=True) #根据value降序
print("order by values,descending:{}".format(ordered_dict3))
ordered_dict4=sorted(dict_copy.items(),key=lambda x: x[1],reverse=False)#根据value升序
print("order by values,ascending:{}".format(ordered_dict4))

#结果 
{'one': 1, 'two': 2, 'three': 3}
order by keys:[('one', 1), ('three', 3), ('two', 2)]
order by values:[('one', 1), ('two', 2), ('three', 3)]
order by values,descending:[('three', 3), ('two', 2), ('one', 1)]
order by values,ascending:[('one', 1), ('two', 2), ('three', 3)]

7.总结 

(1).不能在列表中给没有的位置赋值。
(2).可以给字典随时创建新的位置。
(3).字典索引不一定是数值,列表的索引是数值。
(4).python使用缩进表示逻辑上一个块。
(5).lambda 函数中 item:item[0],x:x[0],可以替换为任意变量表示。如:y:y[0]在字典中都代表键 ,y:y[1] 有1代表值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值