0
我想從字典中使用另一個鍵的值中刪除鍵。密鑰存在,它表示它存在於我鍵入value in dict.keys()
時,但當我試圖刪除它時,我收到了錯誤信息。Python中的KeyError當從字典中刪除一個鍵時,但存在鍵
def remove_duplicate_length(choices):
print choices
print choices['scheme']
print type(choices['scheme'])
print choices['scheme'] in choices.keys()
print 'AABB' in choices.keys()
scheme = choices['scheme']
print type(scheme)
del choices[scheme]
打印此:
{'ABAB': '2', 'AABB': '6', 'scheme': 'AABB', 'authors': ['Bukowski']}
AABB
<type 'str'>
True
True
<type 'str'>
None
,並試圖嘗試直接打印結果時引用return語句,或keyerror: AABB
的結果時給TypeError: 'NoneType' object has no attribute '__getitem__'
。
我打印的結果是這樣的:
@route('/', method='POST')
def getUserChoice():
user_selection = parse_data(request.body.read())
print user_selection
你打算如何打印結果? – karthikr
'del choices ['scheme']' – user2255757
'None'是因爲'del'沒有給你返回類型。如果你檢查'選擇',你會發現這個鍵不再是字典 – karthikr