[英]A “pythonic” strategy to check whether a key already exists in a dictionary
I often deal with heterogeneous datasets and I acquire them as dictionaries in my python routines. 我经常处理异构数据集,并在python例程中将它们作为字典获取。 I usually face the problem that the key of the next entry I am going to add to the dictionary already exists.
我通常会遇到这样一个问题,即将添加到字典中的下一个条目的键已经存在。 I was wondering if there exists a more "pythonic" way to do the following task: check whether the key exists and create/update the corresponding pair key-item of my dictionary
我想知道是否存在更“ pythonic”的方式来执行以下任务:检查密钥是否存在,并创建/更新我字典的相应对密钥项
myDict = dict()
for line in myDatasetFile:
if int(line[-1]) in myDict.keys():
myDict[int(line[-1])].append([line[2],float(line[3])])
else:
myDict[int(line[-1])] = [[line[2],float(line[3])]]
Use a defaultdict
. 使用
defaultdict
。
from collections import defaultdict
d = defaultdict(list)
# Every time you try to access the value of a key that isn't in the dict yet,
# d will call list with no arguments (producing an empty list),
# store the result as the new value, and give you that.
for line in myDatasetFile:
d[int(line[-1])].append([line[2],float(line[3])])
Also, never use thing in d.keys()
. 另外, 切勿
thing in d.keys()
使用thing in d.keys()
。 In Python 2, that will create a list of keys and iterate through it one item at a time to find the key instead of using a hash-based lookup. 在Python 2中,这将创建一个键列表,并一次遍历其中一项来查找键,而不是使用基于哈希的查找。 In Python 3, it's not quite as horrible, but it's still redundant and still slower than the right way, which is
thing in d
. 在Python 3中,它并没有那么可怕,但是它仍然是多余的,并且仍然比正确的方法慢,这是
thing in d
。
Its what that dict.setdefault
is for. 它是
dict.setdefault
的用途。
setdefault(key[, default])
If key is in the dictionary, return its value.
如果key在字典中,则返回其值。 If not, insert key with a value of default and return default.
如果不是,请插入具有默认值的密钥,然后返回默认值。 default defaults to None.
默认默认为无。
example : 例如:
>>> d={}
>>> d.setdefault('a',[]).append([1,2])
>>> d
{'a': [[1, 2]]}
Python follows the idea that it's easier to ask for forgiveness than permission. Python遵循这样的想法:请求宽容比允许容易。
so the true Pythonic way would be: 所以真正的Python方式是:
try:
myDict[int(line[-1])].append([line[2],float(line[3])])
except KeyError:
myDict[int(line[-1])] = [[line[2],float(line[3])]]
for reference: 以供参考:
https://docs.python.org/2/glossary.html#term-eafp https://docs.python.org/2/glossary.html#term-eafp
https://stackoverflow.com/questions/6092992/why-is-it-easier-to-ask-forgiveness-than-permission-in-python-but-not-in-java https://stackoverflow.com/questions/6092992/why-is-it-easier-to-ask-forgiveness-than-permission-in-python-but-not-in-java
Try to catch the Exception
when you get a KeyError
遇到
KeyError
时尝试捕获Exception
myDict = dict()
for line in myDatasetFile:
try:
myDict[int(line[-1])].append([line[2],float(line[3])])
except KeyError:
myDict[int(line[-1])] = [[line[2],float(line[3])]]
Or use: 或使用:
myDict = dict()
for line in myDatasetFile:
myDict.setdefault(int(line[-1]),[]).append([line[2],float(line[3])])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.