2015-06-15 108 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:塊的調用。