您可以使用字典理解內拆箱作業:
>>> my_dict={i:j for i,*j in [l[i:i+4] for i in range(0,len(l),4)]}
>>> my_dict
{'Non Recurring': ['-', '-', '-'], 'Total Other Income/Expenses Net': [33000, 41000, 39000], 'Selling General and Administrative': [6469000, 6384000, 6102000], 'Net Income From Continuing Ops': [4956000, 4659000, 4444000], 'Effect Of Accounting Changes': ['-', '-', '-'], 'Net Income Applicable To Common Shares': [4956000, 4659000, 4444000], 'Net Income': [4956000, 4659000, 4444000], 'Other Items': ['-', '-', '-'], 'Others': ['-', '-', '-'], 'Earnings Before Interest And Taxes': [7168000, 6707000, 6522000], 'Income Before Tax': [7026000, 6562000, 6351000], 'Extraordinary Items': ['-', '-', '-'], 'Total Operating Expenses': ['-', '-', '-'], 'Interest Expense': [142000, 145000, 171000], 'Preferred Stock And Other Adjustments': ['-', '-', '-'], 'Gross Profit': [15374000, 14765000, 14219000], 'Total Revenue': [31821000, 30871000, 29904000], 'Income Tax Expense': [2028000, 1841000, 1840000], 'Operating Income or Loss': [7135000, 6666000, 6483000], 'Cost of Revenue': [16447000, 16106000, 15685000], 'Minority Interest': [-42000, -62000, -67000], 'Research Development': [1770000, 1715000, 1634000], 'Discontinued Operations': ['-', '-', '-']}
如果你想保留的順序,你可以使用collections.OrderedDict
:
>>> from collections import OrderedDict
>>> my_dict=OrderedDict((i,j) for i,*j in [l[i:i+4] for i in range(0,len(l),4)])
>>> my_dict
OrderedDict([('Total Revenue', [31821000, 30871000, 29904000]), ('Cost of Revenue', [16447000, 16106000, 15685000]), ('Gross Profit', [15374000, 14765000, 14219000]), ('Research Development', [1770000, 1715000, 1634000]), ('Selling General and Administrative', [6469000, 6384000, 6102000]), ('Non Recurring', ['-', '-', '-']), ('Others', ['-', '-', '-']), ('Total Operating Expenses', ['-', '-', '-']), ('Operating Income or Loss', [7135000, 6666000, 6483000]), ('Total Other Income/Expenses Net', [33000, 41000, 39000]), ('Earnings Before Interest And Taxes', [7168000, 6707000, 6522000]), ('Interest Expense', [142000, 145000, 171000]), ('Income Before Tax', [7026000, 6562000, 6351000]), ('Income Tax Expense', [2028000, 1841000, 1840000]), ('Minority Interest', [-42000, -62000, -67000]), ('Net Income From Continuing Ops', [4956000, 4659000, 4444000]), ('Discontinued Operations', ['-', '-', '-']), ('Extraordinary Items', ['-', '-', '-']), ('Effect Of Accounting Changes', ['-', '-', '-']), ('Other Items', ['-', '-', '-']), ('Net Income', [4956000, 4659000, 4444000]), ('Preferred Stock And Other Adjustments', ['-', '-', '-']), ('Net Income Applicable To Common Shares', [4956000, 4659000, 4444000])])
同樣作爲一個更加pythonic和有效的方式來分組你的列表你可以使用以下從蟒蛇石斑魚功能itertools recipes
>>> def grouper(iterable, n, fillvalue=None):
... "Collect data into fixed-length chunks or blocks"
... # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
... args = [iter(iterable)] * n
... return zip_longest(*args, fillvalue=fillvalue)
...
>>>
>>> from itertools import zip_longest
>>> my_dict=OrderedDict((i,j) for i,*j in grouper(l,4))
>>> my_dict
OrderedDict([('Total Revenue', [31821000, 30871000, 29904000]), ('Cost of Revenue', [16447000, 16106000, 15685000]), ('Gross Profit', [15374000, 14765000, 14219000]), ('Research Development', [1770000, 1715000, 1634000]), ('Selling General and Administrative', [6469000, 6384000, 6102000]), ('Non Recurring', ['-', '-', '-']), ('Others', ['-', '-', '-']), ('Total Operating Expenses', ['-', '-', '-']), ('Operating Income or Loss', [7135000, 6666000, 6483000]), ('Total Other Income/Expenses Net', [33000, 41000, 39000]), ('Earnings Before Interest And Taxes', [7168000, 6707000, 6522000]), ('Interest Expense', [142000, 145000, 171000]), ('Income Before Tax', [7026000, 6562000, 6351000]), ('Income Tax Expense', [2028000, 1841000, 1840000]), ('Minority Interest', [-42000, -62000, -67000]), ('Net Income From Continuing Ops', [4956000, 4659000, 4444000]), ('Discontinued Operations', ['-', '-', '-']), ('Extraordinary Items', ['-', '-', '-']), ('Effect Of Accounting Changes', ['-', '-', '-']), ('Other Items', ['-', '-', '-']), ('Net Income', [4956000, 4659000, 4444000]), ('Preferred Stock And Other Adjustments', ['-', '-', '-']), ('Net Income Applicable To Common Shares', [4956000, 4659000, 4444000])])
你試過用字典理解嗎?你能否包含你嘗試的代碼? –