簡體   English   中英

Python 從列表中刪除N個連續重復

[英]Python remove N consecutive duplicates from the list

這就是問題所在。 輸入是一個整數列表。 如果超過三個相鄰的數字彼此相鄰出現,則應刪除它們並再次進行操作。 有點類似於 Iphone 游戲,玩家需要彈出三個或更多相同 colors 的球的線。 output 應該是要移除的球的計數。

算法如下。 從說 [3,3,4,4,4,4,3,2] 的示例列表開始。 第一次迭代應刪除 4,4,4,4 - 因此列表將變為 [3,3,3,2],而刪除數字的中間 output 將為 4。第二次迭代應刪除 3,3,3 - 所以最終列表將是 [2] 和刪除數字的最終計數 - 7。

三個連續項目的第一個實現來自另一個 stackoverflow 線程 - 從列表中刪除相鄰數字的三元組

這是針對 3 個連續數字的有效 function 實現:

def balls(l):

    values = l

    while len(values) >= 3:
        print(values) #for demonstrative purposes of this question
        for x in range(0,len(values)-2):    
            if values[x] == values[x+1] and values[x] == values[x+2]:
                values = values[:x] + values[x+3:]
                break
        else:
            break

    print(values) #for demonstrative purposes of this question
   
    return len(l) - len(values)

balls([3, 3, 4, 4, 4, 3, 4])
Output:
[3, 3, 4, 4, 4, 3, 4]
[3, 3, 3, 4]
[4]
6

我如何更新實現以包含刪除 3+ 個連續數字的更通用解決方案。 我正在考慮跟蹤連續重復項的開始和結束索引,然后對列表進行子集化。 但是,不確定如何實現。 以下是應該起作用的測試。

if __name__ == "__main__":
    test1 = [3, 3, 4, 4, 4, 3, 4]
    print(balls(test1))
    #Output should be 6

    test2 = [5, 5, 5, 5, 5, 5, 5, 5]
    print(balls(test2))
    #Output should be 8

    test3 = [5, 7, 8, 3]
    print(balls(test3))
    #Output should be 0
def remove_consecutive(l, length):
    amount = len(l)
    count = 1
    start = 0
    current = l[0]
    i = 1
    while i < len(l):
        if l[i] == current:
            count += 1
        else:
            if count >= length:
                for i in range(count):
                    l.pop(start)
                start = 0
                i = 0
                current = l[0]
            else:
                start = i         
            current = l[i]
            count = 1
        i+=1
    if count >= length:
        for i in range(count):
            l.pop(start)
    return amount - len(l)

嗚嗚,我明白了。 最近腦子有點臭所以拖了很久。

這是我的代碼,它運行良好。 但我認為可能有更好的方法來實現更高的效率。

def remove_consecutive(lst):
    len_init = len(lst)
    contain_tuplets = True
    while contain_tuplets:
        for i in range(len(lst)-2):
            indices_to_pop = []
            if lst[i]==lst[i+1]==lst[i+2]:
                indices_to_pop.extend([i, i+1, i+2])
                for j in range(i+3,len(lst)):
                    if lst[j] == lst[i]:
                        indices_to_pop.append(j)
                    else:
                        break
                [lst.pop(i) for _ in indices_to_pop]
                contain_tuplets = True
                break
        else:
            contain_tuplets = False

    count_removed_numbers = len_init - len(lst)
    return count_removed_numbers, lst

測試用例1:

lst = [3,3,4,4,4,4,3,2]
remove_consecutive(lst)

output

(7, [2])

測試用例 2:

lst = [2, 2, 1, 1, 1, 2, 1]
remove_consecutive(lst)

output:

(6, [1])
def remove_consecutive(l, length):
    amount = 0
    count = 1
    current = l[0]
    for i in range(1, len(l)):
        if l[i] == current:
           
            count += 1
            if count > length:
                amount += 1
            elif count == length:
                amount += length
               
        else:
            current = l[i]
            count = 1
    return amount
           

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM