使用同一词典中的键扩展dict值
我尝试使用同一词典中的键来扩展字典的值。换句话说,我尝试替换dic 值(使用另一个/潜在扩展的集合现有设置)。当值包含字典中的键时,则会发生扩展。
输入:
dic = {
'10': {'01', '02'},
'11': {'03', '04'},
'20': {'05', '10'},
'21': {'06', '11'},
'30': {'07', '20'},
'40': {'08', '21', '30'},
'50': {'09', '40'}
}
预期输出:
{
'10': {'01', '02'},
'11': {'03', '04'},
'20': {'05', '01', '02'},
'21': {'06', '03', '04'},
'30': {'07', '05', '01', '02'},
'40': {'08', '06', '03', '04', '07', '05', '01', '02'},
'50': {'09', '08', '06', '03', '04', '07', '05', '01', '02'}
}
我试图创建递归功能...
def transform_dic(d):
def func(k):
v = d.get(k, k)
if v != k:
for e in v:
v = func(v)
return v
d2 = {}
for k, v in d.items():
d2[k] = {func(i) for i in v}
return d2
print(transform_dic(dic))
TypeError:不可用的类型:'SET'
I try to expand the values of a dictionary using keys in the same dictionary. In other words, I try to replace dic
values (existing set with another/ potentially expanded set). The expansion happens when the value contains a key in the dictionary.
Input:
dic = {
'10': {'01', '02'},
'11': {'03', '04'},
'20': {'05', '10'},
'21': {'06', '11'},
'30': {'07', '20'},
'40': {'08', '21', '30'},
'50': {'09', '40'}
}
Expected output:
{
'10': {'01', '02'},
'11': {'03', '04'},
'20': {'05', '01', '02'},
'21': {'06', '03', '04'},
'30': {'07', '05', '01', '02'},
'40': {'08', '06', '03', '04', '07', '05', '01', '02'},
'50': {'09', '08', '06', '03', '04', '07', '05', '01', '02'}
}
I was trying to create a recursive function...
def transform_dic(d):
def func(k):
v = d.get(k, k)
if v != k:
for e in v:
v = func(v)
return v
d2 = {}
for k, v in d.items():
d2[k] = {func(i) for i in v}
return d2
print(transform_dic(dic))
TypeError: unhashable type: 'set'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您要递归扩展的事物是dict中的集合,而不是dict本身。您总是只有一个命令。
因此,我相信编写递归功能以扩展集合,然后使用此功能扩展dict会更容易。
请注意,
Expanded_set
在不修改输入集或输入dict的情况下返回新集合;Expanded_dict
返回一个新的dict,而无需修改输入dict。很好的做法是清楚您的功能是返回新对象还是修改输入对象。例如,
排序
在不修改其输入的情况下返回新列表,而list.Sort
修改列表而无需返回任何内容。这是一个通过对其进行修改而无需返回任何内容而扩展命令的函数:
用您的示例进行测试:
The things that you want to expand recursively are the sets in the dict, not the dict itself. You always only have one dict.
For this reason, I believe it's easier to write a recursive function to expand a set, and then use this function to expand the dict.
Note that
expanded_set
returns a new set without modifying the input set nor the input dict; andexpanded_dict
returns a new dict without modifying the input dict.It's good practice to make it clear whether your function returns a new object, or modifies the input object. For instance,
sorted
returns a new list without modifying its input, whereaslist.sort
modifies a list without returning anything.Here is a function which expands a dict by modifying it without returning anything:
Testing with your examples:
您可以迭代输出dict,并检查集合内部的值是否是结果dict的成员:
输出将为
You can iterate over output dict and check if values inside the set is member of result dict or not:
the output will be
它必须是递归吗?
如果您不想修改原始字典使用
deepcopy
输出:
Does it have to be recursive?
If you don't want to modify the original dictionary use
deepcopy
Output: