增量值我在python數據字典:搜索的Python解釋並在發現
[
{u'PhoneOwner': u'Bob', u'Frequency': 0, u'PhoneNumber': u'123456789'},
{u'PhoneOwner': u'Sarah', u'Frequency': 0, u'PhoneNumber': u'98765431'}
]
我使用******中國的呼叫列表,我想嘗試和名單與字典和更新當從我的列表中的號碼在字典中,頻率最終前往:
[
{u'PhoneOwner': u'Bob', u'Frequency': 5, u'PhoneNumber': u'123456789'},
{u'PhoneOwner': u'Sarah', u'Frequency': 8, u'PhoneNumber': u'98765431'}
]
目前我有:
with open("CallLog.txt") as connectedNumbers:
for line in connectedNumbers:
try:
phoneNumberDictionary[PhoneNumber] += phoneNumberDictionary[Frequency]1
except KeyError:
phoneNumberDictionary[PhoneNumber] = phoneNumberDictionary[Frequency]1
我無法找到如何搜索字典的一個字段,如果找到匹配更新其他任何細節。 我哪裏錯了?
您的輸入不是一本字典,而是一個字典列表。 – DainDwarf
問題是,你沒有字典;你有一個列表的字典。沒有辦法通過電話號碼爲phoneNumberDictionary編制索引,而無需每次迭代都找到正確的字典。 –
如果你想索引的電話號碼字典,你需要:'{123456789:(u'Bob」,0),......}'或'{123456789:{ '所有者': '鮑勃', '頻率': 0},...}' – DainDwarf