python辞書の添削、辞書のjson

3807 ワード

一:python辞書の追加削除
1、追加:キーと値を加えると、追加できます.以下は「f」:「12 ab」を追加します.
dict = {
    "a": "bbb",
    "b": None,
    "c": True,
    "d": 13,
    "e": ["13", "14"]
}

dict["f"] = "12ab"       #      
print(dict)

実行コードの結果:
D:\Python36\python.exe D:/test/mingtest/case/bolg/test_19.py
{'a': 'bbb', 'b': None, 'c': True, 'd': 13, 'e': ['13', '14'], 'f': '12ab'}

Process finished with exit code 0

2、削除:delメソッドを直接使用して、キーを削除して、対応する値も削除します
dict = {
    "a": "bbb",
    "b": None,
    "c": True,
    "d": 13,
    "e": ["13", "14"]
}

del(dict["a"])    #     "a": "bbb"
print(dict)

実行コードの結果:
D:\Python36\python.exe D:/test/mingtest/case/bolg/test_19.py
{'b': None, 'c': True, 'd': 13, 'e': ['13', '14']}

Process finished with exit code 0

3、修正:キーが変わらない、値が変わる
dict = {
    "a": "bbb",
    "b": None,
    "c": True,
    "d": 13,
    "e": ["13", "14"]
}

dict["a"] = "fffff"      #   "a"
print(dict)

実行コードの結果:
D:\Python36\python.exe D:/test/mingtest/case/bolg/test_19.py
{'a': 'fffff', 'b': None, 'c': True, 'd': 13, 'e': ['13', '14']}

Process finished with exit code 0

4、検索:
dict = {
    "a": "bbb",
    "b": None,
    "c": True,
    "d": 13,
    "e": ["13", "14"]
}

print(dict["b"])  #    print      

実行コードの結果:
D:\Python36\python.exe D:/test/mingtest/case/bolg/test_19.py
None

Process finished with exit code 0

二:辞書回転json,json回転辞書
1、辞書の回転json:注釈を見る
import json         #   json   
dict = {
    "a": "bbb",
    "b": None,
    "c": True,
    "d": 13,
    "e": ["13", "14"]
}


print(type(dict))       #   dict        ?     python  
js = json.dumps(dict)   #   dumps          json  
print(js)
print(type(js))         #   js     ,     ,      ,None   null,True     

実行コードの結果:
D:\Python36\python.exe D:/test/mingtest/case/bolg/test_19.py

{"a": "bbb", "b": null, "c": true, "d": 13, "e": ["13", "14"]}


Process finished with exit code 0

2、json辞書を回す:注釈を読む
import json         #   json   
dict = {
    "a": "bbb",
    "b": None,
    "c": True,
    "d": 13,
    "e": ["13", "14"]
}


print(type(dict))       #   dict        ?     python  
js = json.dumps(dict)   #   dumps          json  
print(js)
print(type(js))         #   js     ,     ,      ,None   null,True     

dict1 = json.loads(js)  #   loads     json     
print(dict1)            #   dict1,    ,null   None,true     
print(type(dict1))      #       dict1         

実行コードの結果:
D:\Python36\python.exe D:/test/mingtest/case/bolg/test_19.py

{"a": "bbb", "b": null, "c": true, "d": 13, "e": ["13", "14"]}

{'a': 'bbb', 'b': None, 'c': True, 'd': 13, 'e': ['13', '14']}


Process finished with exit code 0