Python教程-字典(1)

原创
2016/12/06 16:03
阅读数 61
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']

 

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部