1. 程式人生 > >python3筆記-字典dict

python3筆記-字典dict

#字典dict
'''
#資料型別劃分:可變資料型別,不可變資料型別
不可變資料型別:元祖,bool int str 可雜湊
可變資料型別:list , dict set 不可雜湊
'''
'''
dic = {'name':['dexter','elives'],
       'age':['18','28']
       }
print(dic)
'''
dic1 = {'age':28,'name':'dexter','sex':'male'}
print(dic1)

# 增
#沒有鍵值對,新增;有則覆蓋

dic1['high'] = 178
print(dic1)
#{'high': 178, 'age': 28, 'name': 'dexter', 'sex': 'male'}
dic1['age'] = 18
print(dic1)
#{'high': 178, 'age': 18, 'name': 'dexter', 'sex': 'male'}

#有鍵值對,不做任何改變,沒有才新增
dic1.setdefault('name','tom')
print(dic1)
#{'high': 178, 'name': 'dexter', 'sex': 'male', 'age': 18}
dic1.setdefault('weight','150')
print(dic1)
#{'weight': '150', 'high': 178, 'sex': 'male', 'name': 'dexter', 'age': 18}

# 刪
print (dic1.pop('weight')) #有返回值,按鍵去刪除
#150
print (dic1.pop('tom',None)) #可設定返回值
#None
print(dic1)
#{'high': 178, 'age': 18, 'sex': 'male', 'name': 'dexter'}

print(dic1.popitem()) # 隨機刪除
print(dic1)

print(dic1.clear()) #清空字典
#None
print(dic1)
#{}

del dic1 #刪除字典

# 改
dic2 = {'high': 178,'weight': '150', 'name': 'dexter', 'sex': 'male'}
dic3 = {'age': 18,'sex': 'male', 'name': 'tom'}
dic3.update(dic2)

print(dic2)
#{'high': 178, 'name': 'dexter', 'sex': 'male', 'weight': '150'}
print(dic3)
#{'name': 'dexter', 'sex': 'male', 'weight': '150', 'high': 178, 'age': 18}

# 查
print (dic2.keys(),type(dic2.keys()))
#dict_keys(['sex', 'name', 'high', 'weight']) <class 'dict_keys'>
print (dic2.values())
#dict_values(['male', 'dexter', 178, '150'])
print (dic2.items())
#dict_items([('weight', '150'), ('sex', 'male'), ('name', 'dexter'), ('high', 178)])

for i in dic2:
    print(i)
'''
weight
high
sex
name
'''
for i in dic2.keys():
    print(i)
'''
weight
high
sex
name
'''
for i in dic2.values():
    print(i)
'''
150
178
male
dexter
'''

for i in dic2.items():
    print(i)
'''
('weight', '150')
('name', 'dexter')
('sex', 'male')
('high', 178)
'''

a , b = ['dexter','tom']
print (a,b)

for x,y in dic2.items():
    print(x,y)
'''
sex male
high 178
name dexter
weight 150
'''

v1 = dic2['name']
print(v1)
#dexter

#v2 = dic2['name1'] #報錯
#print(v2)

#避免報錯的方法
print(dic2.get('name1','fuck , no this key'))
#fuck , no this key
print(dic2.get('name','fuck , no this key'))
#dexter

# 字典巢狀

dic4 = {
    'name':['dexter','alex','taibai'],
    'python':{
        'time':'1213',
        'learn_money':19800,
        'addr':'CBD'
    },
    'age':21
}
dic4['age'] = 26
dic4['name'].append('yan')
print(dic4)
#{'name': ['dexter', 'alex', 'taibai', 'yan'], 'age': 26, 'python': {'addr': 'CBD', 'learn_money': 19800, 'time': '1213'}}

dic4['name'][1] = 'tom'
print(dic4)
#{'name': ['dexter', 'tom', 'taibai', 'yan'], 'python': {'addr': 'CBD', 'time': '1213', 'learn_money': 19800}, 'age': 26}

dic4['name'][1] = dic4['name'][1].upper()
print(dic4)
#{'name': ['dexter', 'TOM', 'taibai', 'yan'], 'age': 26, 'python': {'time': '1213', 'addr': 'CBD', 'learn_money': 19800}}

dic4['python']['female'] = 10
print(dic4)
#{'name': ['dexter', 'TOM', 'taibai', 'yan'], 'age': 26, 'python': {'addr': 'CBD', 'female': 10, 'time': '1213', 'learn_money': 19800}}