So I have the values:
所以我有这些值:
values = {(0, 0): 0, (0, 1): 1, (1, 0): 1, (1, 1): 0}
and I want to convert the above dictionary to be:
我想把上面的字典转换成:
values = {0: {0: 0, 1: 1}, 1: {0: 1, 1: 0}}
my function:
我的函数:
def convert(values : {(int,int): int}) -> {int: {int: int}}:
dictionary = {}
l = []
for k in d.keys():
l.append(k)
for k,v in d.items():
for i in l:
if i == k:
dictionary[v] = dict(l)
return dictionary
but I'm getting this as my output instead:
但我把它作为输出结果:
values = {0: {0: 1, 1: 1}, 1: {0: 1, 1: 1}}
4 个解决方案
#1
9
A loop and dict.setdefault()
can do that like:
一个循环和dict.setdefault()可以这样做:
Code:
values = {(0, 0): 0, (0, 1): 1, (1, 0): 1, (1, 1): 0}
result = {}
for k, v in values.items():
result.setdefault(k[0], {})[k[1]] = v
print(result)
Results:
{0: {0: 0, 1: 1}, 1: {0: 1, 1: 0}}
#2
9
You just want to group things. The idiomatic way is to use a defaultdict
:
你只需要分组。惯用的方法是使用违约法:
>>> from collections import defaultdict
>>> values = {(0, 0): 0, (0, 1): 1, (1, 0): 1, (1, 1): 0}
>>> new_values = defaultdict(dict)
>>> for (x,y), v in values.items():
... new_values[x][y] = v
...
>>> new_values
defaultdict(<class 'dict'>, {0: {0: 0, 1: 1}, 1: {0: 1, 1: 0}})
>>>
#3
2
I propose for you a more general approach:
我建议你采取更一般的方法:
from collections import defaultdict
def tree():
def the_tree():
return defaultdict(the_tree)
return the_tree()
t = tree()
for (x, y), z in values.items():
t[x][y] = z
To "close" any node of the tree from further additions, just set its default factory to None
. For example, to seal it at the trunk:
若要“关闭”树的任何节点,只需将其默认工厂设置为None。例如,要把它密封在箱子里:
>>> t.default_factory = None
>>> t[2]
# KeyError
#4
1
A solution for an arbitrary depth:
任意深度的解:
def convert_tupledict(d):
result = {}
for ks, v in d.items():
subresult = result
*kis, ks = ks
for ki in kis:
if ki in subresult:
subresult = subresult[ki]
else:
subresult[ki] = subresult = {}
subresult[ks] = v
return result
We can then call it with:
我们可以这样称呼它:
convert_tupledict({(0, 0): 0, (0, 1): 1, (1, 0): 1, (1, 1): 0})
For 2-tuples, the following sulution should be sufficient:
对于2元组,以下的清洗应该是足够的:
def convert_2tupledict(d):
result = {}
for (k1, k2), v in d.items():
result.setdefault(k1, {})[k2] = v
return result
#1
9
A loop and dict.setdefault()
can do that like:
一个循环和dict.setdefault()可以这样做:
Code:
values = {(0, 0): 0, (0, 1): 1, (1, 0): 1, (1, 1): 0}
result = {}
for k, v in values.items():
result.setdefault(k[0], {})[k[1]] = v
print(result)
Results:
{0: {0: 0, 1: 1}, 1: {0: 1, 1: 0}}
#2
9
You just want to group things. The idiomatic way is to use a defaultdict
:
你只需要分组。惯用的方法是使用违约法:
>>> from collections import defaultdict
>>> values = {(0, 0): 0, (0, 1): 1, (1, 0): 1, (1, 1): 0}
>>> new_values = defaultdict(dict)
>>> for (x,y), v in values.items():
... new_values[x][y] = v
...
>>> new_values
defaultdict(<class 'dict'>, {0: {0: 0, 1: 1}, 1: {0: 1, 1: 0}})
>>>
#3
2
I propose for you a more general approach:
我建议你采取更一般的方法:
from collections import defaultdict
def tree():
def the_tree():
return defaultdict(the_tree)
return the_tree()
t = tree()
for (x, y), z in values.items():
t[x][y] = z
To "close" any node of the tree from further additions, just set its default factory to None
. For example, to seal it at the trunk:
若要“关闭”树的任何节点,只需将其默认工厂设置为None。例如,要把它密封在箱子里:
>>> t.default_factory = None
>>> t[2]
# KeyError
#4
1
A solution for an arbitrary depth:
任意深度的解:
def convert_tupledict(d):
result = {}
for ks, v in d.items():
subresult = result
*kis, ks = ks
for ki in kis:
if ki in subresult:
subresult = subresult[ki]
else:
subresult[ki] = subresult = {}
subresult[ks] = v
return result
We can then call it with:
我们可以这样称呼它:
convert_tupledict({(0, 0): 0, (0, 1): 1, (1, 0): 1, (1, 1): 0})
For 2-tuples, the following sulution should be sufficient:
对于2元组,以下的清洗应该是足够的:
def convert_2tupledict(d):
result = {}
for (k1, k2), v in d.items():
result.setdefault(k1, {})[k2] = v
return result