Pythonデータ型のdict関連常用操作
6013 ワード
下一篇:Pythonデータ型のlist関連常用操作クリックジャンプカタログ編:python関連カタログ編クリックジャンプ次編:Pythonデータ型のset関連常用操作クリックジャンプ
目次
#dict辞書添削#dict辞書のマージ使用#fromkeysは新しい辞書を作成します(辞書のvalueが変更されると、すべてが変更されます)#dict辞書andlistリスト取値#dict辞書のsorted昇順#dict辞書の追加削除
目次
#dict辞書添削#dict辞書のマージ使用#fromkeysは新しい辞書を作成します(辞書のvalueが変更されると、すべてが変更されます)#dict辞書andlistリスト取値#dict辞書のsorted昇順#dict辞書の追加削除
info = {
'stu1101': "TengLan Wu",
'stu1102': "LongZe Luola",
'stu1103': "XiaoZe Maliya",
}
#
print(info["stu1101"]) #
#
info["stu1101"] = " " # key
info["stu1104"] = "changjinkong" # key key key
#
#del info["stu1101"] # key( )
info.pop("stu1101") # key
print(info)
#
#info['stu1104'] # , key ,
print(info.get('stu1103')) # .get('xxx')
print(info.get('stu1105')) # key none
# key
print('stu1103'in info) # key #python2:info.has_key("1103") , 3
# key , ,
info. setdefault('stu1103', "XiaoZe Maliya")
#dict
info = {
'stu1101': "TengLan Wu",
'stu1102': "LongZe Luola",
'stu1103': "XiaoZe Maliya",
}
b ={
'stu1101':"Burgess",
1:3,
2:5
}
info.update(b) # b info ( ) key
# .fromkeys ( value , )
c = dict.fromkeys([6,7,8],"test")
# c=={8: 'test', 6: 'test', 7: 'test'}
d = dict.fromkeys([6,7,8],[1,{"name":"burgess"},444])
# d=={8: [1, {'name': 'burgess'}, 444], 6: [1, {'name': 'burgess'}, 444], 7: [1, {'name': 'burgess'}, 444]}
d[8][1]['name'] = "Jack "
# d=={8: [1, {'name': 'Jack '}, 444], 6: [1, {'name': 'Jack'}, 444], 7: [1, {'name': 'Jack'}, 444]}
#dict andlist
__author__ = "Burgess Zheng"
USER_DICT = {
'1':{'name':'root1','email':'[email protected]'},
'2':{'name':'root2','email':'[email protected]'},
'3':{'name':'root3','email':'[email protected]'},
'4':{'name':'root4','email':'[email protected]'},
'5':{'name':'root5','email':'[email protected]'},
}
for i in USER_DICT.keys(): # key
print(i) # 1 2.....
for i in USER_DICT.values(): # value
print(i) # ([{'name':'root1','email':'[email protected]'},{‘name’:root2....}]
for k,i in USER_DICT.items():# key value
print(i) # value {'email': '[email protected]', 'name': 'root2'}
print(k) # key 2
print(USER_DICT["1"])# key=1 value
# {'name': 'root1', 'email': '[email protected]'}
print(USER_DICT["1"]["email"])# key=1 value key=email value
# [email protected]
print(list(USER_DICT.values()))
# [{'name': 'root1', 'email': '[email protected]'},{‘name’:root2....},....]
print(list(USER_DICT.key()))
# [‘1’,’2’....’5’]
print(list(a.items()))
# [(‘1’,{‘name’:root1....),(‘2’,{‘name’:root2...}),()...]
USER_LIST = [
{'name':'root1','email':'[email protected]'},
{'name':'root2','email':'[email protected]'},
{'name':'root3','email':'[email protected]'},
{'name':'root4','email':'[email protected]'},
{'name':'root5','email':'[email protected]'},
]
for i in USER_LIST:#
print(i)# {'name': 'root1', 'email': '[email protected]'}
print(USER_LIST[0]) # 0 value
# :{'email': '[email protected]', 'name': 'root1'}
print(USER_LIST[0]["name"])# 0 value key=name
# :root1
#dict sorted
a = {6:2,8:0,1:4,-5:6,99:11,4:22}
print(a) #
print(sorted(a)) # key , value
print(sorted(a.items())) # key , value
print(sorted(a.items(),key=lambda x:x[1])) # value
#key=lambda x=key:x[1] key value key
# items
:Python list
:python
:Python set