1
我想解析一个csv文件,并希望按升序章节对输出进行排序,然后通过主题键排序 - 同时保持章节顺序。用两个不同的键对字典列表进行排序
我设法排序下面的章节,但无法按主题排序,同时保留章节顺序。
In [35]: d = DictReader(open('gatsby-test.csv', 'rb'))
In [36]: rows = []
In [37]: for row in d:
....: rows.append(row)
....:
In [38]: sorted_d = sorted(rows, key=lambda item: item['chapter'])
In [39]: sorted_d
Out[39]:
[{'chapter': 'Chapter 1',
'character': 'Nick Carraway',
'explanation': 'explanation one',
'quote': '"quote one"',
'theme': 'love'},
{'chapter': 'Chapter 2',
'character': 'Daisy Buchanan',
'explanation': 'explanation two',
'quote': '"quote two"',
'theme': 'wealth'},
{'chapter': 'Chapter 2',
'character': 'Jordan Baker',
'explanation': 'explanation five',
'quote': '"quote five"',
'theme': 'dissatisfaction'},
{'chapter': 'Chapter 3',
'character': 'Daisy Buchanan',
'explanation': 'explanation four',
'quote': '"quote four"',
'theme': 'isolation'},
{'chapter': 'Chapter 3',
'character': 'Daisy Buchanan',
'explanation': 'explanation three',
'quote': '"quote three"',
'theme': 'isolation'}]
工程就像一个魅力!谢谢。 – puffin