python -- 解決字典【KEY ERROR】
【問題描述】
通過KEY來獲取對應的值時,如果KEY不在字典中,就會報錯KeyError,引起程式執行終止。
例子:
test_0 = {"id":[1,1,2,3,3,4,5,5],"price":[5,6,8,3,4,6,9,5],"amount":[1,1,2,1,1,1,2,1],"status":['sale','sale','no','no','sale','no','sale','no']}
test_0['time']
【解決辦法】
使用get()方法來呼叫
語法:dict.get(key,default = None)
當字典中包含此key時,則返回此值,否則會返回預設的值(None),這裡的預設值可以自行更改
test_0.get('time',"沒有這個Key!")
解決問題!
(僅供個人學習,不負責任~~~~~~~~~~~~~~)
相關文章
- python 字典修改鍵(key)的方法Python
- Python 查詢字典中最大最小值的keyPython
- RuntimeError: Error(s) in loading state_dict for SSD: Missing key(s) in解決辦法Error
- Python中字典使用詳解Python
- Ubuntu下 fatal error: Python.h: No such file or directory 解決方法UbuntuErrorPython
- python迴圈遍歷字典: title_content_list.append([key, value])print(tiPythonAPP
- Python字典的遍歷,包括key遍歷/value遍歷/item遍歷/Python
- ERROR 1062 (23000): Duplicate entry for key 'PRIMARY'Error
- 【已解決】Error filter ListenerStartErrorFilter
- Error creating bean with name 解決ErrorBean
- 偶遇ERROR 1071 (42000): Specified key was too long; max key length is 767 bytesError
- 完美解決stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.ErrorPython
- Agent admitted failure to sign using the key解決MITAI
- fabirc error authorizing update: error validating ReadSet: readset expected key [Group] /Channel/AppErrorAPP
- 解決IDEA Error:Output directory is not specifiedIdeaError
- Python:說說字典和雜湊表,雜湊衝突的解決原理Python
- SSH auth method: private key 的解決方案
- redis中大key問題的解決思路Redis
- Error: error:0308010C:digital envelope routines::unsupported 解決方案ErrorGit
- 帝國CMS提示parse error syntax error的解決方法Error
- HBuilder解決:Error: error:0308010C:digital envelope routines::unsupportedUIErrorGit
- python sorted keyPython
- python-字典Python
- Python dict(字典)Python
- 【Jenkins】data stream error|Error cloning remote repo ‘origin‘ 錯誤解決JenkinsErrorREM
- 解決 "Script Error" 的另類思路Error
- Manjaro解決 Node.JS Error: ENOSPCJARNode.jsError
- error: use of deleted function ‘YYSTYPE::YYSTYPE()’[解決]ErrordeleteFunction
- DedeCMS Error: Tag disabled: "php" 解決方法ErrorPHP
- PHP curl error 60 錯誤解決PHPError
- 解決docker: Error response from daemon故障DockerError
- Python 列表、元組、字典及集合操作詳解Python
- 解決ERROR 1030 (HY000): Got error 168 from storage engine apparmorErrorGoAPP
- 取出字典中value最大值對應的key
- Python中字典dictPython
- Python字典dict用法Python
- 初學Python——字典Python
- Python中的字典Python