python英汉互译词典下载_Python教程-字典(1)

本文介绍了Python字典的基本操作,包括创建字典、获取值、遍历元素、删除键值对、检查键存在、更新字典等内容,并通过实例展示了如何使用这些方法。
摘要由CSDN通过智能技术生成

d = {key1 : value1, key2 : value2 }

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}

dict1 = { 'abc': 456 };

dict2 = { 'abc': 123, 98.6: 37 };

x.get(key):获得字典中key对应的值

x.items():获得字典中所有的元素

x.iteritems():用迭代方式获得字典中所有的元素

x.pop(key):删除字典中对应的key-value

x.keys():返回字典中所有的键

x.values():返回字典中所有的值

x.has_key(key):查找字典中是否含有此key

x.update(y):使用y字典更新x字典

x={'one':[1,'first'],'two':'second','three':{'name':'third','age':3}}

print x.items()

print list(x.iteritems())

print x.keys()

print list(x.iterkeys())

x.pop('two')

print x.items()

y={'one':'first','four':'forth'}

x.update(y)

print x.items()

print x.values()

[('three', {'age': 3, 'name': 'third'}), ('two', 'second'), ('one', [1, 'first'])]

[('three', {'age': 3, 'name': 'third'}), ('two', 'second'), ('one', [1, 'first'])]

['three', 'two', 'one']

['three', 'two', 'one']

[('three', {'age': 3, 'name': 'third'}), ('one', [1, 'first'])]

[('four', 'forth'), ('three', {'age': 3, 'name': 'third'}), ('one', 'first')]

['forth', {'age': 3, 'name': 'third'}, 'first']

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值