2016-09-26 32 views
0

我是新來的蟒蛇,並試圖附加在列表的末端重複項內部消除改變順序追加複製在列表的最後項目內部消除改變順序

testlist = [1, 2, 32, 8, 1, 17, 5, 2, 42, 13, 56] 

def duplicate(alist): 
    p = len(alist) 
    duplicate = False 
    for i in range(0, p): 
     for j in range (i + 1, p): 
      if alist[i] == alist[j]: 
       b = alist.index(alist[j]) 
       a = alist.pop(b) 
       alist.append(a) 
       p -= 1 
       duplicate = True 

    print alist 

if duplicate == False: 
    print "No duplicate item found" 

duplicate(testlist) 

OUTPUT:[32, 8, 1, 17, 5, 2, 42, 13, 56, 1, 2]

所需的輸出:[1, 2, 32, 8, 17, 5, 42, 13, 56, 1, 2]

任何幫助,我在這裏做什麼錯

+0

你能解決您的代碼的縮進?我們不能告訴你是否有不正確的縮進問題:) – kfb

+1

你認爲'alist.pop(b)'有什麼作用? –

+0

您的代碼至少應該沒有語法錯誤(除非它是您需要幫助的語法)。此代碼目前不是語法Python – holdenweb

回答

2

我認爲,在這種情況下,建立一個新的立ST是更有效和明確與原列表中的排列比較:

testlist = [1, 2, 32, 8, 1, 17, 5, 2, 42, 13, 56] 

def duplicate(alist): 

    filtered, duplicates = [], [] 
    for element in alist: 
     if element in filtered: 
      duplicates.append(element) 
      continue 
     filtered.append(element) 

    if not duplicates: 
     print "No duplicate item found" 
     return alist 
    return filtered + duplicates 

new_list = duplicate(testlist) 
print new_list 
+0

我做了類似的事情,只是使用了一套已經看過的元素。我認爲這是最好的答案。 – polku

+0

'set'不是保存順序,所以在這裏不需要 – ShabashP

+0

是的,它不是很清楚,我的意思是一個集合,知道哪些元素已經被看到,但重複的元素確實放在列表中。無論如何沒有太大的區別,這就是爲什麼我沒有做出答案。 – polku

0

您可以使用收藏模塊獲得OrderedDict維護元素的順序。

我們在這裏使用的技術是創建一個字典來存儲數組中每個元素的出現次數,並使用dict來查找出現次數以備後用。

for循環中,我們使用get方法查找dict中是否存在元素。如果true那麼我們增加計數器,否則將計數器初始化爲零。

import collections 
lst = [1, 2, 32, 8, 1, 17, 5, 2, 42, 13, 56] 

# Create a dictionary storing the the number of occurences 
occurences_dict = collections.OrderedDict() 
for i in lst: 
    occurences_dict[i] = occurences_dict.get(i, 0) + 1 

final = occurences_dict.keys() + [k for k, v in occurences_dict.items() if v>1] 

print final 
>>> [1, 2, 32, 8, 17, 5, 42, 13, 56, 1, 2] 
+0

您的解決方案涉及字典的創建和填充,列表的創建和連接......對於列表的排列不太昂貴? – ShabashP

+0

但是,那永遠不會到達'O(n^2)',如果你看得清楚,它仍然是'O(n)'。 – ZdaR

+0

好的,它會是'O(n)',但是你迭代列表兩次。另外你在第二個循環中使用比較。 – ShabashP

0

我這樣解決了。我也做了一些改變,使代碼更加Python化。

test_list = [1, 2, 32, 8, 1, 17, 5, 2, 42, 13, 56] 

def duplicate(a_list): 
    list_length = len(a_list) 
    duplicate = False 
    checked = [] 
    for i in range(list_length): 
     if a_list.count(a_list[i]) > 1: 
      if a_list[i] not in checked: 
       duplicate = True 
       a_list.append(a_list[i]) 
       checked.append(a_list[i]) 
    if duplicate == False: 
     print("No duplicate item found") 
     return None 
    return a_list 

print(duplicate(test_list)) 
0

而不是檢查值,比較索引。

請檢查驗證碼:

testlist = [1, 2, 32, 8, 1, 17, 5, 2, 42, 13, 56,32] 

print("Original list - ",testlist) 
tmp_list = [] 
exclude =[] 
for i in range(len(testlist)): 
    if i == testlist.index(testlist[i]): 

     tmp_list.append(testlist[i]) 
    else: 
     exclude.append(testlist[i]) 
tmp_list.extend(exclude) 
testlist = tmp_list 

print("Updated list - ",testlist) 

輸出:

C:\Users\dinesh_pundkar\Desktop>python c.py 
Original list - [1, 2, 32, 8, 1, 17, 5, 2, 42, 13, 56, 32] 
Updated list - [1, 2, 32, 8, 17, 5, 42, 13, 56, 1, 2, 32] 

C:\Users\dinesh_pundkar\Desktop>