在嵌套字典中切换两个字典
Switch two dictionaries in a nested dictionary
我正在尝试在另一个词典中交换两个词典。
我希望看到以下结果:
input: {10: {"name": "name_value1", "price1": "price_value1"},
30: {"name": "name_value3", "price3": "price_value3"},
20: {"name": "name_value2", "price2": "price_value2"}}
output:{10: {"name": "name_value1", "price1": "price_value1"},
20: {"name": "name_value2", "price2": "price_value2"},
30: {"name": "name_value3", "price3": "price_value3"}}
我尝试了很多不同的方法,有时值的顺序是正确的,但键没有更新。下面的代码就是一个例子。
def my_sort(dct):
index = 0
key_list = list(dct.keys())
while index < (len(dct) - 1):
if key_list[index] > key_list[index + 1]:
dct[key_list[index]], dct[key_list[index + 1]] = dct[key_list[index + 1]], dct[key_list[index]]
key_list[index], key_list[index + 1] = key_list[index + 1], key_list[index]
index = 0
else:
index += 1
return dct
此代码输出以下内容:
{10: {"name": "name_value1", "price1": "price_value1"},
30: {"name": "name_value2", "price2": "price_value2"},
20: {"name": "name_value3", "price3": "price_value3"}}
注意:我不能为此使用外部库/框架,也不能使用像 sorted() 这样的函数。
欢迎所有帮助和建议!提前谢谢你。
你可以使用 sorted() 吗?
d = {10: {"name": "name_value1", "price1": "price_value1"},
30: {"name": "name_value3", "price3": "price_value3"},
20: {"name": "name_value2", "price2": "price_value2"}}
dict(sorted(d.items(), reverse=False))
输出:
{10: {'name': 'name_value1', 'price1': 'price_value1'},
20: {'name': 'name_value2', 'price2': 'price_value2'},
30: {'name': 'name_value3', 'price3': 'price_value3'}}
您可以使用所需的键顺序创建新词典:
old_dict = {10: {"name": "name_value1", "price1": "price_value1"},
30: {"name": "name_value3", "price3": "price_value3"},
20: {"name": "name_value2", "price2": "price_value2"}}
key_ix = [10, 20, 30]
new_dict = {k: old_dict[k] for k in key_ix}
def sort_keys(keys_):
for i in range(len(keys_)):
for j in range(i, len(keys_)):
if keys_[i] > keys_[j]:
keys_[i], keys_[j] = keys_[j], keys_[i]
return keys_
input = {10: {"name": "name_value1", "price1": "price_value1"},
30: {"name": "name_value3", "price3": "price_value3"},
20: {"name": "name_value2", "price2": "price_value2"}}
keys_ = list(input.keys())
sorted_keys = sort_keys(keys_)
out = {key:input[key] for key in sorted_keys}
我正在尝试在另一个词典中交换两个词典。
我希望看到以下结果:
input: {10: {"name": "name_value1", "price1": "price_value1"},
30: {"name": "name_value3", "price3": "price_value3"},
20: {"name": "name_value2", "price2": "price_value2"}}
output:{10: {"name": "name_value1", "price1": "price_value1"},
20: {"name": "name_value2", "price2": "price_value2"},
30: {"name": "name_value3", "price3": "price_value3"}}
我尝试了很多不同的方法,有时值的顺序是正确的,但键没有更新。下面的代码就是一个例子。
def my_sort(dct):
index = 0
key_list = list(dct.keys())
while index < (len(dct) - 1):
if key_list[index] > key_list[index + 1]:
dct[key_list[index]], dct[key_list[index + 1]] = dct[key_list[index + 1]], dct[key_list[index]]
key_list[index], key_list[index + 1] = key_list[index + 1], key_list[index]
index = 0
else:
index += 1
return dct
此代码输出以下内容:
{10: {"name": "name_value1", "price1": "price_value1"},
30: {"name": "name_value2", "price2": "price_value2"},
20: {"name": "name_value3", "price3": "price_value3"}}
注意:我不能为此使用外部库/框架,也不能使用像 sorted() 这样的函数。
欢迎所有帮助和建议!提前谢谢你。
你可以使用 sorted() 吗?
d = {10: {"name": "name_value1", "price1": "price_value1"},
30: {"name": "name_value3", "price3": "price_value3"},
20: {"name": "name_value2", "price2": "price_value2"}}
dict(sorted(d.items(), reverse=False))
输出:
{10: {'name': 'name_value1', 'price1': 'price_value1'},
20: {'name': 'name_value2', 'price2': 'price_value2'},
30: {'name': 'name_value3', 'price3': 'price_value3'}}
您可以使用所需的键顺序创建新词典:
old_dict = {10: {"name": "name_value1", "price1": "price_value1"},
30: {"name": "name_value3", "price3": "price_value3"},
20: {"name": "name_value2", "price2": "price_value2"}}
key_ix = [10, 20, 30]
new_dict = {k: old_dict[k] for k in key_ix}
def sort_keys(keys_):
for i in range(len(keys_)):
for j in range(i, len(keys_)):
if keys_[i] > keys_[j]:
keys_[i], keys_[j] = keys_[j], keys_[i]
return keys_
input = {10: {"name": "name_value1", "price1": "price_value1"},
30: {"name": "name_value3", "price3": "price_value3"},
20: {"name": "name_value2", "price2": "price_value2"}}
keys_ = list(input.keys())
sorted_keys = sort_keys(keys_)
out = {key:input[key] for key in sorted_keys}