简体   繁体   English

如何防止函数向python中的字典添加重复键

[英]How to prevent a function from adding repeated keys to a dictionary in python

Let be the follow function bellow.让我们下面的函数。 The function takes the elements line by line from a file and adds it to a dictionary (each line has a key and a value).该函数从文件中逐行获取元素并将其添加到字典中(每行都有一个键和一个值)。

with open("archive.txt") as f:
      for line in f:
         (key, val) = line.split()
          dictionary[key] = val

Example: Let be the following .txt file:示例:让我们成为以下 .txt 文件:

aaa 111
bbb 222
aaa 333
ccc 444

Insert in the following dict:插入以下字典:

dictionary = ['aaa':111, 'bbb':222, 'aaa':333, 'ccc':444]

however, I would like to prevent repeated keys from being inserted, so the end result should be:但是,我想防止插入重复的密钥,因此最终结果应该是:

dictionary = ['aaa':111, 'bbb':222, 'ccc':444]

How to prevent the insertion of a duplicate key?如何防止插入重复键?

If you want to always keep the first value associated with a given key, a simple trick is touse setdefault to set the value only if the key doesn't already exist:如果您想始终保留与给定键关联的第一个值,一个简单的技巧是当键不存在时才使用setdefault设置值:

with open("archive.txt") as f:
    for line in f:
        key, val = line.split()
        dictionary.setdefault(key, val)

dictionary.setdefault(key, val) is essentially equivalent to the somewhat more verbose: dictionary.setdefault(key, val)本质上等同于更冗长的:

if key not in dictionary:
     dictionary[key] = val

though (at least on CPython, though not necessarily all alternate interpreters) the former is atomic (when the key is a built-in type, eg str in this case), while the latter isn't (two threads could both perform the test, find no such key, then both insert, in arbitrary order, and both threads would believe they set the value, even though only the second to write would win).虽然(至少在 CPython 上,虽然不一定是所有替代解释器)前者是原子的(当键是内置类型时,例如在这种情况下的str ),而后者不是(两个线程都可以执行测试,找不到这样的键,然后两个线程都以任意顺序插入,并且两个线程都相信他们设置了该值,即使只有第二个写入会获胜)。

If keeping the last value is what you want, then your original code is already fine;如果保留最后一个值是您想要的,那么您的原始代码已经没问题了; dict s only store one copy of a key, so replacing the associated value if it's reassigned, so your original code only keeps the last value already. dict只存储一个键的副本,因此如果重新分配相关值,则替换相关值,因此您的原始代码只保留最后一个值。

Check if the dictionary contains the key before overwriting a key/value that's already there在覆盖已经存在的键/值之前检查字典是否包含键

with open("archive.txt") as f:
  for line in f:
     (key, val) = line.split()
     if key not in dictionary:
         dictionary[key] = val

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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