2015-06-15 76 views
0

我有一个字符串如下:的Python 3.4:列表到词典

['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] 

有是1的图案:3列表内。无论如何,我可以将此列表转换为下面例示的字典。

{Total Revenue : [31821000, 30871000, 29904000], Cost of Revenue : [16447000, 16106000, 15685000] ... ... ... } 

我认为这可以通过字典理解完成。

+1

你试过用字典理解吗?你能否包含你尝试的代码? –

回答

4

您可以使用字典理解内拆箱作业:

>>> 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])]) 
+1

梦幻般的答案。 –

+1

@卡斯拉美丽! –

0

它的确可以:

assert len(lst) % 4 == 0 
{lst[4*i]: lst[4*i+1:4*(i+1)] for i in range(len(lst)/4)} 
+2

这个答案根本不对。 '因为我在len(lst)/ 4'会告诉你一个int(在Python 2中)或float(Python 3)对象是不可迭代的。如果不纠正,我会投票结束。即使修正了这个问题,由于所有的指数运算都是不可理解的,所以即使它没有错误,也不是一个好的答案。 – holdenweb

+0

他打算明显使用'range(len(lst)/ 4)'。虽然很丑。 ;-) – woot

1

我会做一个辅助函数,它接受您的数据和块产生它,用钥匙一起:

def generate_revenues(data): 
    data = iter(data) 
    while True: 
     key = next(data) 
     values = [next(data), next(data), next(data)] 
     yield key, values 

使得字典是如此的简单:

>>> dict(generate_revenues(data)) 

这给:

{'Cost of Revenue': [16447000, 16106000, 15685000], 
'Discontinued Operations': ['-', '-', '-'], 
'Earnings Before Interest And Taxes': [7168000, 6707000, 6522000], 
... 
'Total Operating Expenses': ['-', '-', '-'], 
'Total Other Income/Expenses Net': [33000, 41000, 39000], 
'Total Revenue': [31821000, 30871000, 29904000]} 

来自未来的人的技术说明:发生器产生了一个StopIteration异常,以停止迭代。在未来的Python版本中,这将被禁止,并且您需要保留nexttry: except StopIteration:块的调用。