简体   繁体   中英

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. 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

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 .

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() . 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. 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 .

Its what that dict.setdefault is for.

setdefault(key[, default])

If key is in the dictionary, return its value. 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.

so the true Pythonic way would be:

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://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

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])])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM