Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在字典中添加条目之前,如何检查重复条目_Python - Fatal编程技术网

Python 在字典中添加条目之前,如何检查重复条目

Python 在字典中添加条目之前,如何检查重复条目,python,Python,假设我有下面的字典,它存储键(entry\u id)、值(entry\u body、entry\u title)对 如何检查要添加到词典中的条目的标题是否已存在。 例如:这是我要添加的新条目 { "body": "nnnn", "title": "jack" } 您是否考虑过更改数据结构?如果没有上下文,条目的ID似乎有点无用。你的问题表明你只想存储唯一的标题,为什么不把它们作为你的钥匙呢 例如: "entries": { "jack": "ooo", "joh

假设我有下面的字典,它存储键(entry\u id)、值(entry\u body、entry\u title)对

如何检查要添加到词典中的条目的标题是否已存在。 例如:这是我要添加的新条目

{
    "body": "nnnn",
    "title": "jack"
}

您是否考虑过更改数据结构?如果没有上下文,条目的ID似乎有点无用。你的问题表明你只想存储唯一的标题,为什么不把它们作为你的钥匙呢

例如:

"entries": {
    "jack": "ooo",
    "john": "ooo"
}
这样,您就可以在条目中执行有效的
if newname
成员资格测试

编辑:

根据您的评论,您仍然可以通过扩展数据结构来保留ID:

"entries": {
    "jack": {
        "body": "ooo",
        "id": 1
    },

    "john": {
        "body": "ooo",
        "id": 2
    }
}

我认为要做到这一点,就必须遍历字典

'john' in [the_dict[en]['title'] for en in the_dict]
这应该行吗

entry_dict = {
    "1": {"body": "ooo", "title": "jack"},
    "2": {"body": "ooo", "title": "john"}
}

def does_title_exist(title):
    for entry_id, sub_dict in entry_dict.items():
        if sub_dict["title"] == title:
            print("Title %s already in dictionary at entry %s" %(title, entry_id))
            return True
    return False

print("Does the title exist? %s" % does_title_exist("jack"))

正如Christian在上文中指出的,这似乎是一种低效的数据结构。如果您只需要索引ID,列表可能会更好。

我同意@Christian König的答案,您的数据结构似乎可以变得更清晰、更高效。不过,如果您特别需要此设置的解决方案,这里有一个可行的解决方案,它会自动向
条目添加新的整数键

我添加了一个额外的案例来显示拒绝和接受的更新

def existing_entry(e, d):
    return [True for entry in d["entries"].values() if entry["title"] == e["title"]]

def update_entries(e, entries):
    if not any(existing_entry(e, entries)):
        current_keys = [int(x) for x in list(entries["entries"].keys())]
        next_key = str(max(current_keys) + 1)
        entries["entries"][next_key] = e
        print("Updated:", entries)
    else:
        print("Existing entry found.")

update_entries(new_entry_1, data)
update_entries(new_entry_2, data)
输出:

Existing entry found.
Updated: 
{'entries': 
    {'1': {'body': 'ooo', 'title': 'jack'}, 
     '2': {'body': 'ooo', 'title': 'john'}, 
     '3': {'body': 'qqqq', 'title': 'jill'}
    }
}
数据:


我会重新设计数据结构,并将
title
作为一个键。id很有用。新条目的结构是{“body”:“ooooo”,“title”:“jack”,“id”:(根据字典长度自动生成)}
Existing entry found.
Updated: 
{'entries': 
    {'1': {'body': 'ooo', 'title': 'jack'}, 
     '2': {'body': 'ooo', 'title': 'john'}, 
     '3': {'body': 'qqqq', 'title': 'jill'}
    }
}
data = {"entries": {"1": {"body": "ooo", "title": "jack"},"2": {"body": "ooo","title": "john"}}}
new_entry_1 = {"body": "nnnn", "title": "jack"}
new_entry_2 = {"body": "qqqq", "title": "jill"}