My input is
input_dic = {
1: [2,3],
2: [10],
3: [4],
4: [],
5:[6,7] ,
6: [],
7: [8],
8: [9],
9: [10],
10: []
}
i want output as follows
output = {1 : { 2 : {10 : { } },3 : {4: { } }, 5 : { 6 : { }, 7 : { 8 : { 9 : { } } } } }}
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
My input is
input_dic = {
1: [2,3],
2: [10],
3: [4],
4: [],
5:[6,7] ,
6: [],
7: [8],
8: [9],
9: [10],
10: []
}
i want output as follows
output = {1 : { 2 : {10 : { } },3 : {4: { } }, 5 : { 6 : { }, 7 : { 8 : { 9 : { } } } } }}
Hi!
The code below is an example, adapt it as you need.
input_dict = {1: [2, 3], 2: [10], 3: [4], 4: [], 5: [6, 7], 6: [], 7: [8], 8: [9], 9: [10], 10: []}
def get_values(number):
result = {}
current_values = input_dict[number]
if current_values.__len__() >= 1:
for current_value in current_values:
if input_dict[current_value].__len__() >= 1:
result[current_value] = dict.fromkeys(input_dict[current_value],
get_values(input_dict[current_value][0]) or {})
return result
values = {}
for i in range(1, input_dict.__len__()):
if input_dict[i].__len__() > 1:
values[i] = get_values(i)
Best regards!
Tạo tài khoản ngay hôm nay để tận hưởng các tính năng độc đáo và tham gia cộng đồng tuyệt vời của chúng tôi!
Đăng kýBài viết liên quan | Trả lời | Lượt xem | Hoạt động | |
---|---|---|---|---|
|
2
thg 3 15
|
4460 | ||
|
1
thg 3 15
|
31737 | ||
|
1
thg 3 15
|
3817 | ||
|
0
thg 1 20
|
3743 | ||
|
0
thg 6 19
|
3449 |