Dictionary 如何以Pythonic的方式使用参考词典更新Python词典?
我认为这很简单。我所要做的就是用另一个具有该值的词典的Dictionary 如何以Pythonic的方式使用参考词典更新Python词典?,dictionary,python,Dictionary,Python,我认为这很简单。我所要做的就是用另一个具有该值的词典的'code'更新原始词典。我对循环有一种感觉,可以进一步缩短IF循环来得到答案。在我的实际问题中,我需要更新的DICT只有1000条。谢谢大家 Python: referencedict = {'A': 'abc', 'B': 'xyz'} mylistofdict = [{'name': 'John', 'code': 'A', 'age': 28}, {'name': 'Mary', 'code': 'B', 'age': 32}, {
'code'
更新原始词典。我对循环有一种感觉,可以进一步缩短IF循环来得到答案。在我的实际问题中,我需要更新的DICT只有1000条。谢谢大家
Python:
referencedict = {'A': 'abc', 'B': 'xyz'}
mylistofdict = [{'name': 'John', 'code': 'A', 'age': 28}, {'name': 'Mary', 'code': 'B', 'age': 32}, {'name': 'Joe', 'code': 'A', 'age': 43}]
for eachdict in mylistofdict:
for key, value in eachdict.items():
if key == 'code':
eachdict[key] = referencedict[value]
print mylistofdict
[{'age': 28, 'code': 'abc', 'name': 'John'}, {'age': 32, 'code': 'xyz', 'name': 'Mary'}, {'age': 43, 'code': 'abc', 'name': 'Joe'}]
输出:
referencedict = {'A': 'abc', 'B': 'xyz'}
mylistofdict = [{'name': 'John', 'code': 'A', 'age': 28}, {'name': 'Mary', 'code': 'B', 'age': 32}, {'name': 'Joe', 'code': 'A', 'age': 43}]
for eachdict in mylistofdict:
for key, value in eachdict.items():
if key == 'code':
eachdict[key] = referencedict[value]
print mylistofdict
[{'age': 28, 'code': 'abc', 'name': 'John'}, {'age': 32, 'code': 'xyz', 'name': 'Mary'}, {'age': 43, 'code': 'abc', 'name': 'Joe'}]
无需循环所有
eachdict
的值,只需直接查找code
:
for eachdict in mylistofdict:
if 'code' not in eachdict:
continue
eachdict['code'] = referencedict[eachdict['code']]
您可能可以忽略对code
的测试,您的示例列表总是包含code
条目,但我认为最好是安全的。在referencedict结构中查找代码时,假定所有可能的代码都可用
我使用了
如果“code”不在每个ICT中:在这里继续;相反的方法同样有效(if'code'in eachdict
),但通过这种方法,如果不需要,您可以更轻松地删除该行,并为自己保存一个缩进级别。无需对eachdict
的所有值进行循环,只需直接查找code
:
for eachdict in mylistofdict:
if 'code' not in eachdict:
continue
eachdict['code'] = referencedict[eachdict['code']]
referencedict = {'A': 'abc', 'B': 'xyz'}
mylistofdict = [{'name': 'John', 'code': 'A', 'age': 28}, {'name': 'Mary', 'code': 'B', 'age': 32}, {'name': 'Joe', 'code': 'A', 'age': 43}]
for x in mylistofdict:
try:
x['code']=referencedict.get(x['code'])
except KeyError:
pass
print(mylistofdict)
您可能可以忽略对code
的测试,您的示例列表总是包含code
条目,但我认为最好是安全的。在referencedict结构中查找代码时,假定所有可能的代码都可用
我使用了如果“code”不在每个ICT中:在这里继续;相反的方法同样有效(if'code'在eachdict
),但这样,如果你不需要它,你可以更容易地删除该行,并为自己节省一个缩进级别。if'code'在eachdict:eachdict['code']=referencedict[eachdict['code']]@ralu:这是一个品味问题。@Eric:除非你在几个条件下工作,当你这样做的时候。许多python代码进入太多缩进级别,只是因为他们不知道如何执行continue
。谢谢。这很有效。我将在两种方法上都使用timeit,只是为了见鬼,但我觉得这样会更快。@ThinkCode:如果没有if
语句,它们可能会彼此一样快。选择您认为可读性和可维护性最好的内容。如果eachdict中的“code”:eachdict['code']=referencedict[eachdict['code']]@ralu:这是一个品味问题。@Eric:直到您在多种条件下工作时,您才这样做。许多python代码进入太多缩进级别,只是因为他们不知道如何执行continue
。谢谢。这很有效。我将在两种方法上都使用timeit,只是为了见鬼,但我觉得这样会更快。@ThinkCode:如果没有if
语句,它们可能会彼此一样快。选择您认为可读性和可维护性最好的内容。
referencedict = {'A': 'abc', 'B': 'xyz'}
mylistofdict = [{'name': 'John', 'code': 'A', 'age': 28}, {'name': 'Mary', 'code': 'B', 'age': 32}, {'name': 'Joe', 'code': 'A', 'age': 43}]
for x in mylistofdict:
try:
x['code']=referencedict.get(x['code'])
except KeyError:
pass
print(mylistofdict)