删除python上列表的背靠背重复项

时间:2017-02-07 23:12:50

标签: python list duplicates

我想删除列表的背靠背副本。

locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

我想得到这个清单:

myList = ["A", "B", "C", "D", "A", "A", "B", "C", "B", "B", "D"]

我尝试将其转换为一个集合然后返回到列表但我会一直删除所有重复项而不仅仅是

A, B, C, D, A, B, C, B, D

提前感谢您的帮助!

4 个答案:

答案 0 :(得分:3)

使用itertools.groupby

>>> from itertools import groupby
>>> l = ["A", "B", "C", "D", "A", "A", "B", "C", "B", "B", "D"]
>>> [x[0] for x in groupby(l)]
['A', 'B', 'C', 'D', 'A', 'B', 'C', 'B', 'D']

答案 1 :(得分:1)

可能有更好的方法,但线性扫描和跟踪前一个元素可以工作。

myList = ["A", "B", "C", "D", "A", "A", "B", "C", "B", "B", "D"]

output = []
prev = None
for x in myList:
  if prev != x:
    output.append(x)
  prev = x

>>> print(output) 
['A', 'B', 'C', 'D', 'A', 'B', 'C', 'B', 'D']

我的简单测试说这也处理两个以上的背对背元素

答案 2 :(得分:0)

一个简单的(如果不优雅)解决方案是将有效元素附加到新列表中:

mylist = ["A", "B", "C", "D", "A", "A", "B", "C", "B", "B", "D"]
newlist = []
prevchar = ''

for char in mylist:

    if char != prevchar:
        newlist.append(char)

    prevchar = char

答案 3 :(得分:0)

因为你可以,这是一个使用生成器和列表理解的函数:

# Generator which returns whether the last character is the same
# as the one you just sent it
def is_diff():
    last = char = None
    while True:
        diff = last != char
        last = char
        char = yield diff

def de_dupe(list):
    gen_diff = is_diff()
    gen_diff.send(None)  # These steps are required to initiate the generator
    return [char for char in list if gen_diff.send(char)]

用作:

de_dupe(my_list)
['A', 'B', 'C', 'D', 'A', 'B', 'C', 'B', 'D']