我目前有一个包含六个键的字典,这些键的理想顺序是这样的:
xls = ExcelFile('quiz/all_questions.xlsx')
df = xls.parse(xls.sheet_names[0], parse_cols = 241, keep_default_na=False, na_values=[""])
questions_2 = df.to_dict()
现在,这些键中的每一个都对应于所有可能值的字典。我构建这本字典的方式是通过一个Pandas读者(不确定这是否重要,但可能有办法通过Pandas解决这个问题)。
{'id': {0: 'CB_1', 1: 'CB_2'}, 'question': {0: 'Who is Ghoulsbee Scroggins?', 1: 'Who is Ebeneezer Yakbain?}, 'choice1': {0: 'Cat', 1: 'A mathematician'}, 'choice2': {0: 'Dog', 1: 'A mathematician'}, 'choice3': {0: 'Ape: 'A mathematician'}, 'choice4': {0: 'Astrophysicist', 1: 'A mathematician'}, 'solution': {0: 'Ape', 1: 'A mathematician'}
dict的当前结构如下:
[OrderedDict([('id', '1'), ('question', 'What is the capital of China?'), ('choice1', 'Shanghai'), ('choice2', 'Guangzhou'), ('choice3', 'Hong Kong'), ('choice4', 'Beijing'), ('solution', 'Beijing')]), OrderedDict([('id', '2'), ('question', 'What year did World War 1 start?'), ('choice1', '1896'), ('choice2', '1914'), ('choice3', '1921'), ('choice4', '1929'), ('solution', '1914')]), OrderedDict([('id', '3'), ('question', 'What is the second closest planet to the sun?'), ('choice1', 'Saturn'), ('choice2', 'Mercury'), ('choice3', 'Venus'), ('choice4', 'Mars'), ('solution', 'Venus')]), OrderedDict([('id', '4'), ('question', 'What is the symbol for gold on the periodic table?'), ('choice1', 'Au'), ('choice2', 'Gd'), ('choice3', 'Gl'), ('choice4', 'Or'), ('solution', 'Au')])]
我想把它变成形式的有序词典:
net/ldap
我似乎无法弄清楚如何以正确的顺序迭代键值对,以构建元组列表。有什么想法吗?
答案 0 :(得分:1)
一种解决方案是使用OrderedDict
和for
循环。
from collections import OrderedDict
order_of_keys = ["id", "question", "choice1", "choice2", "choice3", "choice4", "solution"]
input_dict = {'id': {0: 'CB_1', 1: 'CB_2'},
'question': {0: 'Who is Ghoulsbee Scroggins?', 1: 'Who is Ebeneezer Yakbain?'},
'choice1': {0: 'Cat', 1: 'A mathematician'},
'choice2': {0: 'Dog', 1: 'A mathematician'},
'choice3': {0: 'Ape', 1: 'A mathematician'},
'choice4': {0: 'Astrophysicist', 1: 'A mathematician'},
'solution': {0: 'Ape', 1: 'A mathematician'}}
res = []
for key in input_dict['id']:
d = OrderedDict()
d['id'] = key
for k in order_of_keys[1:]:
d[k] = input_dict[k][key]
res.append(d)
<强>结果强>
[OrderedDict([('id', 0),
('question', 'Who is Ghoulsbee Scroggins?'),
('choice1', 'Cat'),
('choice2', 'Dog'),
('choice3', 'Ape'),
('choice4', 'Astrophysicist'),
('solution', 'Ape')]),
OrderedDict([('id', 1),
('question', 'Who is Ebeneezer Yakbain?'),
('choice1', 'A mathematician'),
('choice2', 'A mathematician'),
('choice3', 'A mathematician'),
('choice4', 'A mathematician'),
('solution', 'A mathematician')])]
<强>解释强>
OrderedDict
的类,并将空列表定义为默认值。id
。对于每个id
,将项目添加到与给定列表order_or_keys
对齐的有序词典中。OrderedDict
附加到结果列表。答案 1 :(得分:0)
有序字典会记住添加内容的顺序,跟踪订单。如果您无法控制插入顺序,则可以按以下方式重新排序:
# od is your ordered dictionary
for key in reversed(order_of_keys):
od.move_to_end(key)
另外,作为旁注,在Python3字典中是有序的(虽然您在技术上不应该依赖的功能),因此您可以创建另一个字典并按所需顺序插入您的项目,它是&#39; s比有序字典好,因为有序的dict存储了一个链表来跟踪顺序,所以需要更多的内存。