2012-03-02 39 views
1

方式我有一個字典更好的處理字典的名單或改善我的代碼

my_dict = 
    { '[email protected]' : [ { 'event': 'event1', 'oldval' = 1, 'newval = 2}, { 'event': 'event2', 'oldval' = 11, 'newval': 22}, { 'event': 'event3', 'oldval' = 33, 'newval': 44}] 
    {'[email protected]' : { 'event1': 'oldval': 45, 'newval': 66}, 
    {'[email protected]': [ {'event':'event2', 'oldval': 77, 'newval': 99}, { 'event': 'event3', 'oldval': '99', 'newval': 100'}] 

所以基本上我的目標是建立一個列表

[ ['[email protected]', (2,1,True), (22,11, True), (44,33,True)], ['[email protected]', (66,45,True), ('','',False),('','',False)], ['[email protected]', ('',''False), (99,77,True), (100,99)]] 

所以我的目標是奪取如果該值事件被更改並獲取值並標記爲true。

所以其我寫的代碼如下:

for key,value in my_dict.items(): 
    if str(my_dict).find('event1') >- 1 or str(my_dict).find('even2') > 1 or str(my_dict).find('event3') > -1: 
    tmp_list = [] 
    tmp_list.append(key) 
    if type(value) is dict: 
    if value['event'] == 'event1': 
     @ get all the value and append to tmp_list 
    if value['event'] == 'event2': 
     @ do the same thing as above 
    else: 
    for val in value: 
     if val['name'] == 'event': 
     @ do the same stuff. 
     @do the same stuff again for rest of event. 

所以基本上我能夠按照上面的代碼模式來實現我的目標。

所以任何人都可以幫助我更好地編碼,我會非常感激。 我不喜歡我編碼的方式。

感謝

+2

所以你真的很喜歡用「所以」開始一個句子,對吧? – 2012-03-02 07:05:31

+0

另外,你發佈的兩個片段都不是真正有效的Python,所以你如何發佈你的真實代碼,以便我們可以爲你找到正確的答案? – 2012-03-02 07:06:55

回答

0

具有異質名單像['[email protected]', (2,1,True), (22,11, True), (44,33,True)]是一個代碼味道。它幾乎總是意味着你試圖用簡單的數據類型做太多事情,並且實際上使用一個類可能會更好。假設Python的2.X:

class Event(object): 
    def __init__(self, event, oldval, newval): 
     self.event = event 
     self.oldval = oldval 
     self.newval = newval 

class EventThing(object): 
    def __init__(self, key, event_list): 
     self.key = key 
     self.event_list = [Event(**e) for e in event_list] 

    def produceCondensedList(self): 
     return [(e.oldval, e.newval, True) for e in self.event_list] 

thing_list = [EventThing(key, event_list) for key, event_list in my_dict.items()] 

這是不完全一樣的,你給的例子,但不知道更多關於你的確切使用情況下,很難精確提示答案。上面的內容應該足以讓你開始。

0

這是我可以考慮編碼的最佳方式。

def value_finder(val): 
    return lambda ev,li: next((di[val] for di in li if di['event']==ev),'') 

def event_finder(ev,li): 
    return any(di['event']==ev for di in li) 

def tuple_return(ev,li,nv,ov): 
    return (nv(ev,li),ov(ev,li),event_finder(ev,li)) 

def main(my_dict): 
    lis = [] 
    nv = value_finder('newval') 
    ov = value_finder('oldval') 
    events = ['event1','event2','event3'] 
    for key,value in my_dict.items(): 
     temp_list = [key] 
     temp_list += [tuple_return(ev,value) for ev in events] 
     lis.append(temp_list) 
    print lis 

my_dict = {'[email protected]':[{'event':'event1','oldval':1,'newval':2}, 
         {'event':'event2','oldval':11,'newval':22}, 
         {'event':'event3','oldval':33,'newval':44}], 
      '[email protected]':[{'event':'event1','oldval':45,'newval':66}], 
      '[email protected]':[{'event':'event2','oldval': 77,'newval': 99}, 
         {'event':'event3','oldval':'99','newval':100}]} 
main(my_dict)