python字典的操作

原文链接: http://www.cnblogs.com/qflyue/p/8072973.html

思维导图如下

python字典的操作_第1张图片

1、字典的增加

    dic1={'name':'wujie','age':18,'gender':'男'}
    dic1['profession']='python全栈'
    dic1.setdefault('hobby','游戏')
    print(dic1)

2、字典的删除

dic1={'name':'wujie','age':18,'gender':'男'}
dic1['profession']='python全栈'
dic1.setdefault('hobby','游戏')
# print(dic1)
# 删
dic1.pop('age')
dic1.popitem()
del dic1['name']
print(dic1)

3、字典的更改

    dic1 = {'name': 'wujie', 'age': 18, 'gender': '男'}
    dic2 = {'name': 'wushuai', 'age': 18, 'gender': '男','hobby':'游戏'}
    dic2.update(dic1)
    print(dic2)

4、字典的查询

    dic1 = {'name': 'wujie', 'age': 18, 'gender': '男'}
    print(dic1.keys())
    print(dic1.values())
    print(dic1.items())
    for k,v in dic1.items():
        print(k,v)

python字典的操作_第2张图片

 

转载于:https://www.cnblogs.com/qflyue/p/8072973.html

你可能感兴趣的:(python字典的操作)