0
如何將key: value
對添加到Python中的字典中的字典中? 我需要的密鑰類型採取詞典的輸入和排序結果:在字典中的詞典中添加鍵值對:
new_d = {'int':{}, 'float':{}, 'str':{}}
temp = {}
for key in d:
temp[key] = d[key]
print temp
if type(key) == str:
new_d['str'] = temp
temp.clear()
elif type(key) == int:
print 'int'
temp.clear()
elif type(key) == float:
print 'float'
temp.clear()
這是我並沒有什麼寫new_d
字典。
輸出應該是這樣的
>>> new_d = type_subdicts({1: 'hi', 3.0: '5', 'hi': 5, 'hello': 10})
>>> new_d[int]
{1: 'hi'}
>>> new_d[float]
{3.0: '5'}
>>> new_d[str] == {'hi': 5, 'hello': 10}
True
"""
你想讓你的輸出看起來像什麼? – IanAuld
這裏'd'是什麼可以讓您更清楚地知道給定輸入的輸出 – The6thSense
在第一個代碼塊中,'new_d'具有字符串鍵。在第二個代碼塊中,它具有類型鍵。 – TigerhawkT3