2015-06-11 69 views
1

我正在嘗試使用使用嵌套列表的字典理解。只要鑰匙是唯一的,一切正常。但是,如果存在多個密鑰,我想將值附加到該密鑰而不是覆蓋該值。這可能使用理解嗎?帶有嵌套列表的字典理解,防止覆蓋

seq1 = [[1, [1,2,3,4]], [2, [5,6,7]]] 
seq2 = [[1, [1,2,3,4]], [1, [5,6,7]]] 

print {key: [val] for key, val in seq1} # Or dict(seq1) 
>>> {1: [[1, 2, 3, 4]], 2: [[5, 6, 7]]} 

print {key: [val] for key, val in seq2} 
>>> {1: [[5, 6, 7]]} # First value is overwritten 

# Desired output: 
def index_reads(reads): 
    result = {} 
    for i in reads: 
     d = dict([i]) 
     for key, val in d.iteritems(): 
      if key in result: 
       result[key].append(val) 
      else: 
       result[key] = [val] 
    return result 

print index_reads(seq1) 
>>> {1: [[1, 2, 3, 4]], 2: [[5, 6, 7]]} 

print index_reads(seq2) 
>>> {1: [[1, 2, 3, 4], [5, 6, 7]]} 

對不起,我找不到這個問題的重複。

回答

1

你可以使用groupbyitertools

import itertools 
import operator 

seq1 = [[1, [1,2,3,4]], [2, [5,6,7]]] 
seq2 = [[1, [1,2,3,4]], [1, [5,6,7]]] 

def index_reads(seq): 
    return {k: [i[1] for i in g] for k, g in itertools.groupby(seq, operator.itemgetter(0))} 

print index_reads(seq1) 
print index_reads(seq2) 

輸出

{1: [[1, 2, 3, 4]], 2: [[5, 6, 7]]} 
{1: [[1, 2, 3, 4], [5, 6, 7]]} 
2

你不需要列表理解。作爲一個更Python的方式,你可以使用dict.setdefault()方法:

>>> d={key: [val] for key, val in seq1} 
>>> for key, val in seq2: 
... d.setdefault(key,[]).append(val) 
... 
>>> d 
{1: [[1, 2, 3, 4], [1, 2, 3, 4], [5, 6, 7]], 2: [[5, 6, 7]]} 

您還可以使用collections.defaultdict此類任務。

而且它顯示了它的力量,當你有不同的密鑰在seq2例如:

>>> seq2 = [[1, [1,2,3,4]], [5, [5,6,7]]] 
>>> d={key: [val] for key, val in seq1} 
>>> for key, val in seq2: 
... d.setdefault(key,[]).append(val) 
... 
>>> d 
{1: [[1, 2, 3, 4], [1, 2, 3, 4]], 2: [[5, 6, 7]], 5: [[5, 6, 7]]} 

如果你不想讓你重複可以使用defaultdictset集裝箱:

>>> from collections import defaultdict 
>>> seq1 = [[1, [1,2,3,4]], [2, [5,6,7]]] 
>>> seq2 = [[1, [1,2,3,4]], [1, [5,6,7]]] 
>>> 
>>> d=defaultdict(set) 
>>> for key, val in seq1+seq2: 
... d[key].add(tuple(val)) 
... 
>>> d 
defaultdict(<type 'set'>, {1: set([(5, 6, 7), (1, 2, 3, 4)]), 2: set([(5, 6, 7)])}) 
+1

或使用'defaultdict' –

+0

@KarolyHorvath是啊,確實;) – Kasramvd

+0

謝謝,我會看看,但輸出與預期略有不同。我現在將編輯,對不起。 – kezzos

0

是的,defaultdict作品也:

from collections import defaultdict 

seq1 = [[1, [1,2,3,4]], [2, [5,6,7]]] 
seq2 = [[1, [1,2,3,4]], [1, [5,6,7]]] 

d={key: [val] for key, val in seq1} 
d = defaultdict(list) 
for key, val in seq2: 
    d[key].append(val) 

print d 

然後:

[(1, [[1, 2, 3, 4], [5, 6, 7]]) 

或者,如果我們刪除SEQ1,

seq2 = [[1, [1,2,3,4]], [1, [5,6,7]]] 
d={key: [val] for key, val in seq2} 
d = defaultdict(list) 
for key, val in seq2: 
    d[key].append(val) 
print d 

同樣,你將擁有:

defaultdict(<type 'list'>, {1: [[1, 2, 3, 4], [5, 6, 7]]}) 
+0

如果seq1不存在,我應該怎麼做.. – kezzos