2015-04-01 124 views
1

我試圖累積列表中多次出現的項目索引列表。不知道該怎麼做,因爲我的代碼只能在終止前比較pattern[1]pattern[2]查找列表中多個發生項目的索引

def test(pattern): 
    """(list) -> list of int 

    >>> test(['A', 'B', 'A', 'C', 'A']) 
    [0, 2, 4] 
    >>> test(['A', 'B']) 
    [] 
    """ 
    indices = [] 
    new_list = [] 

    for i in range(len(pattern) - 1): 
      if pattern[i][-1] == pattern[i + 1]: 
       indices.append(i) 
       new_list = phoneme_list[max(indices):] 

     return new_list 
+0

您使用的列表有多長? – TigerhawkT3 2015-04-01 23:56:19

回答

3
>>> lst = ['A', 'B', 'A', 'C', 'A'] 
>>> [i for i in range(len(lst)) if lst.count(lst[i]) > 1] 
[0, 2, 4] 

也就是說,組裝指數列表可能表明你的算法還有待改進。

2

我應該這樣做:

import collections 

d = collections.defaultdict(list) 

for idx,el in enumerate(lst): 
    d[el].append(idx) 
    # you can also do this with d as a normal dict, and instead do 
    # d.setdefault(el, []).append(idx) 

# this builds {'A':[0,1,3], 'B':[2,4], 'C':[5]} from 
# ['A','A','B','A','B','C'] 

result = [idx for idxs in d.values() for idx in idxs if len(idxs) > 1] 
# this builds [0,1,3,2,4] from 
# {'A':[0,1,3], 'B':[2,4], 'C':[5]} 

這也避免了需要調用list.count N次,這應該大量更快地執行了一個更大的數據集。

或者,您可以利用collections.Counter來獲取多次發生的所有值,然後一次拉出所有的索引。

import collections 

c = set([el for el,val in collections.Counter(lst).items() if val>1]) 
# gives {'A','B'} 
result = [idx for idx,el in enumerate(lst) if el in c] 
# gives [1,2,3,4]