2016-07-26 32 views
-2

我有一個類似的名單一起: 列表類型的字典的:消除重複和疊加的獨特價值

[ 
    {'code': 'ABCDEFGH', 
    'message':'Everything is not OK', 
    'name': 'Tom Sawyer', 
    'course': 'Networks'}, 

    {'code': 'ABCDEFGH', 
    'message':'Alright', 
    'name': 'Julien Sorel', 
    'course': 'Networks'}, 

    {'code': 'KQPRADBC', 
    'message':'Hello there', 
    'name': 'Jacques Paganel', 
    'course': 'Cooking'}, 

    {'code': 'KQPRADBC', 
    'message':'Hello there', 
    'name': 'Jacques Paganel', 
    'course': 'Cooking'}, 
] 

而且我希望它看起來像這樣:

[ 
    {'code': 'ABCDEFGH', 
    'message': ['Everything is not OK', 'Alright'], 
    'name': ['Tom Sawyer', 'Julien Sorel'], 
    'course': 'Networks'}, 

    {'code': 'KQPRADBC', 
    'message':'Hello there', 
    'name': 'Jacques Paganel', 
    'course': 'Cooking'}, 
] 

所以這完全複製條目將被刪除,而那些具有唯一字典值的條目將被合併到列表中。順序並不重要。

這對我來說看起來像一個巨大的黑客。提前致謝。

! Python 3.5!

+1

我認爲,解決辦法是寫一個循環,用於構建另一個字典。循環需要查看「代碼」字段,確定它是否唯一或不在新詞典中,然後適當添加(即在必要時比較其他列)。 – BrandonM

+0

顯示你寫的代碼和你的結果。什麼是或不在工作? –

+0

@ joelgoldstick需要一個關於如何嘗試這個的一般想法(我認爲這應該通過刪除工作)和布蘭登給了我。我會嘗試這種方式。 – light2yellow

回答

2

爲了保證具有相當簡單的可讀代碼的唯一性,可以使用集合。請參閱代碼中的註釋。

我建議,假如lst包含您的字典清單:

res = list() # start from scratch 
for k in lst: 
    for d in res: # check the previously created entries 
     if d['code'] == k['code']: 
      # add to entry 
      for field in ['message', 'name', 'course']: 
       d[field].add(k[field]) # will not do anything if value is already here 
      break 
    else: # this will be executed if the for didn't break 
     # create new entry 
     tmp = {'code': k['code']} 
     for field in ['message', 'name', 'course']: 
      tmp[field] = set([k[field]]) 
     res.append(tmp) 
print res 

有了您的字典作爲進入名單,我得到如下:

[ 
{'course': set(['Networks']), 
'message': set(['Alright', 'Everything is not OK']), 
'code': 'ABCDEFGH', 
'name': set(['Julien Sorel', 'Tom Sawyer'])}, 

{'course': set(['Cooking']), 
'message': set(['Hello there']), 
'code': 'KQPRADBC', 
'name': set(['Jacques Paganel'])} 
] 

如果你想擁有一模一樣的輸出,就像你在你的問題中寫的一樣,你可能想在最後添加類似這樣的東西:

for d in res: 
    for field in ['message', 'name', 'course']: 
     if len(d[field]) > 1: 
      d[field] = list(d[field]) 
     else: 
      d[field] = list(d[field])[0] 
+0

非常感謝您的幫助。 else有一個縮進問題。 – light2yellow

+1

沒有。它的意思是和'for'在同一條垂直線上;)參見[this](http://psung.blogspot.com/2007/12/for-else-in-python.html)進一步啓發 – BusyAnt

+0

哦。我會引用其中一位評論者:「哇,我從來不知道這個!」 – light2yellow

0
a = [ 
{'code': 'ABCDEFGH', 
'message':'Everything is not OK', 
'name': 'Tom Sawyer', 
'course': 'Networks'}, 

{'code': 'ABCDEFGH', 
'message':'Alright', 
'name': 'Julien Sorel', 
'course': 'Networks'}, 

{'code': 'KQPRADBC', 
'message':'Hello there', 
'name': 'Jacques Paganel', 
'course': 'Cooking'}, 

{'code': 'KQPRADBC', 
'message':'Hello there', 
'name': 'Jacques Paganel', 
'course': 'Cooking'}, 
] 
out=[] 
for i in set([tuple(d.items()) for d in a]): 
    out.append(dict(i)) 
print out 

輸出:

[{'course': 'Cooking', 'message': 'Hello there', 'code': 'KQPRADBC', 'name': 'Jacques Paganel'}, 
{'course': 'Networks', 'message': 'Everything is not OK', 'code': 'ABCDEFGH', 'name': 'Tom Sawyer'}, 
{'course': 'Networks', 'message': 'Alright', 'code': 'ABCDEFGH', 'name': 'Julien Sorel'}] 
相關問題