gpt4 book ai didi

python - 字典操作

转载 作者:太空宇宙 更新时间:2023-11-03 11:22:40 25 4
gpt4 key购买 nike

我有一本字典的字典,它调低了一两个档次,看起来像这样:

a = {114907: {114905: 1.4351310915,
114908: 0.84635577943,
114861: 61.490648372},
113820: {113826: 8.6999361654,
113819: 1.1412795216,
111068: 1.1964946282,
117066: 1.5595617822,
113822: 1.1958951003},
114908: {114906: 1.279878388,
114907: 0.77568252572,
114862: 2.5412545474}
}

我要执行的操作如下:

对于 a 的每个键:

  • 如果它的值(最里面的字典,例如,{114905: 1.435.., 114908: 0.846.., 114861: 61.490..})包含作为键出现在最外层的键还有一个(在本例中为 114908),将它们替换为后者的 k, v 值并将其完全删除。
  • 最后,将最外层的键转换为包含原始键和从最里面的字典中弹出的键的元组。

期望的输出是这样的:

b = {(114907, 114908): {114905: 1.4351310915,
114906: 1.279878388,
114862: 2.5412545474,
114861: 61.490648372},
113820: {113826: 8.6999361654,
113819: 1.1412795216,
111068: 1.1964946282,
117066: 1.5595617822,
113822: 1.1958951003}
}

我真的希望你能得到我想要在这里实现的目标,因为这甚至无法描述。

这是我目前所拥有的,但它在几个方面都失败了,我深信我走错了路。最终我会到达那里,但这将是有史以来最低效的编码。

from copy import deepcopy

temp = deepcopy(a)
for item in temp:
for subitems, values in temp[item].items():
if values < 1.0:
for k, v in temp[subitems].items():
if k != item:
a[item][k] = v
# a[item].pop(subitems)
for i in a:
print(i, a[i])
#114908 {114905: 1.4351310915, 114906: 1.279878388, 114907: 0.77568252572, 114861: 61.490648372, 114862: 2.5412545474}
#114907 {114905: 1.4351310915, 114906: 1.279878388, 114908: 0.84635577943, 114861: 61.490648372, 114862: 2.5412545474}
#113820 {113826: 8.6999361654, 113819: 1.1412795216, 111068: 1.1964946282, 117066: 1.5595617822, 113822: 1.1958951003}

附带问题,为什么字典中的 pop 只返回 value 而不是 key:value 对?

编辑

一个可能使事情变得更容易的重要细节是另一种查找必须修改的内容的方法是内部字典值。如果它们低于 1.0,它们的键也必然是外部字典的键。

最佳答案

这应该可行

a = {114907: {114905: 1.4351310915,
114908: 0.84635577943,
114861: 61.490648372},
113820: {113826: 8.6999361654,
113819: 1.1412795216,
111068: 1.1964946282,
117066: 1.5595617822,
113822: 1.1958951003},
114908: {114906: 1.279878388,
114907: 0.77568252572,
114862: 2.5412545474}
}

# Lets call the keys leaders and its value is a dict of
# keys ( call them members ) to floats.
# if a member is also a leader, then the two leaders combine.

leaders = set(a.keys())
leaders_to_members = { leader: set(member_dict.keys()) for leader, member_dict in a.items() }
seen_leaders =set()
b = {}
for leader, members in leaders_to_members.items():
if leader in seen_leaders:
continue

members_as_leaders = members.intersection(leaders)
members_as_leaders.add(leader)

v = {}
for member_leader in members_as_leaders:
v.update(a[member_leader])

seen_leaders.update(members_as_leaders)

# if its just one element, you want it as the key directly
b_key = tuple(members_as_leaders) if len(members_as_leaders) > 1 else members_as_leaders.pop()
# as per your output, you've removed the key to float value if it is a leader
b_val = { k: float_val for k, float_val in v.items() if k not in members_as_leaders }
b[b_key] = b_val

print(b)

输出

{113820: {111068: 1.1964946282,
113819: 1.1412795216,
113822: 1.1958951003,
113826: 8.6999361654,
117066: 1.5595617822},
(114907, 114908): {114861: 61.490648372,
114862: 2.5412545474,
114905: 1.4351310915,
114906: 1.279878388}}

The side question: why does pop in dictionaries return the value only and not the key: value pair?

>>> a.pop()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: pop expected at least 1 arguments, got 0

>>> help(a.pop)
"""
Help on built-in function pop:

pop(...) method of builtins.dict instance
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised
"""

如您所见,pop 需要键,因此它可以弹出值。由于您需要向它提供 key ,因此它不必将 key 还给您。

关于python - 字典操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39709147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com