How do I remove consecutive duplicates from a list like this in python?
如何在python中从这样的列表中删除连续的重复项?
lst = [1,2,2,4,4,4,4,1,3,3,3,5,5,5,5,5]
Having a unique list or set wouldn't solve the problem as there are some repeated values like 1,...,1 in the previous list.
拥有唯一的列表或集合无法解决问题,因为在上一个列表中存在一些重复值,如1,...,1。
I want the result to be like this:
我希望结果是这样的:
newlst = [1,2,4,1,3,5]
Would you also please consider the case when I have a list like this [4, 4, 4, 4, 2, 2, 3, 3, 3, 3, 3, 3]
and I want the result to be [4,2,3,3]
rather than [4,2,3]
.
当我有一个像这样的列表[4,4,4,4,2,3,3,3,3,3,3,3]时,你也可以考虑这个案例,我希望结果是[4,2] ,3,3]而不是[4,2,3]。
7 个解决方案
#1
8
itertools.groupby() is your solution.
itertools.groupby()是你的解决方案。
newlst = [k for k, g in itertools.groupby(lst)]
If you wish to group and limit the group size by the item's value, meaning 8 4's will be [4,4], and 9 3's will be [3,3,3] here are 2 options that does it:
如果你想按照项目的值对组大小进行分组和限制,意味着8 4将是[4,4],9 3将是[3,3,3]这里有两个选项:
import itertools
def special_groupby(iterable):
last_element = 0
count = 0
state = False
def key_func(x):
nonlocal last_element
nonlocal count
nonlocal state
if last_element != x or x >= count:
last_element = x
count = 1
state = not state
else:
count += 1
return state
return [next(g) for k, g in itertools.groupby(iterable, key=key_func)]
special_groupby(lst)
OR
要么
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return itertools.zip_longest(*args, fillvalue=fillvalue)
newlst = list(itertools.chain.from_iterable(next(zip(*grouper(g, k))) for k, g in itertools.groupby(lst)))
Choose whichever you deem appropriate. Both methods are for numbers > 0.
选择您认为合适的任何一种。两种方法都适用于数字> 0。
#2
2
If you want to use the itertools
method @MaxU suggested, a possible code implementation is:
如果你想使用@MaxU建议的itertools方法,可能的代码实现是:
import itertools as it
lst=[1,2,2,4,4,4,4,1,3,3,3,5,5,5,5,5]
unique_lst = [i[0] for i in it.groupby(lst)]
print(unique_lst)
#3
2
list1 = ['a', 'a', 'a', 'b', 'b' , 'a', 'f', 'c', 'a','a']
temp_list = []
for item in list1:
if len(temp_list) == 0:
temp_list.append(item)
elif len(temp_list) > 0:
if temp_list[-1] != item:
temp_list.append(item)
print(temp_list)
- Fetch each item from the main list(list1).
- 从主列表中获取每个项目(list1)。
- If the 'temp_list' is empty add that item.
- 如果'temp_list'为空,则添加该项。
- If not , check whether the last item in the temp_list is not same as the item we fetched from 'list1'.
- 如果没有,请检查temp_list中的最后一项是否与我们从'list1'获取的项目不同。
- if items are different append into temp_list.
- 如果项目不同,则附加到temp_list中。
#4
0
You'd probably want something like this.
你可能想要这样的东西。
lst = [1, 1, 2, 2, 2, 2, 3, 3, 4, 1, 2]
prev_value = None
for number in lst[:]: # the : means we're slicing it, making a copy in other words
if number == prev_value:
lst.remove(number)
else:
prev_value = number
So, we're going through the list, and if it's the same as the previous number, we remove it from the list, otherwise, we update the previous number.
因此,我们将浏览列表,如果它与前一个数字相同,我们会将其从列表中删除,否则,我们会更新之前的数字。
There may be a more succinct way, but this is the way that looked most apparent to me.
可能有一种更简洁的方式,但这是对我来说最明显的方式。
HTH.
HTH。
#5
0
newlist=[]
prev=lst[0]
newlist.append(prev)
for each in lst[:1]: #to skip 1st lst[0]
if(each!=prev):
newlist.append(each)
prev=each
#6
0
st = ['']
[st.append(a) for a in [1,2,2,4,4,4,4,1,3,3,3,5,5,5,5,5] if a != st[-1]]
print(st[1:])
#7
0
Check if the next element always is not equal to item. If so append.
检查下一个元素是否始终不等于item。如果这样追加。
lst = [1,2,2,4,4,4,4,1,3,3,3,5,5,5,5,5]
new_item = lst[0]
new_list = [lst[0]]
for l in lst:
if new_item != l:
new_list.append(l)
new_item = l
print new_list
print lst
#1
8
itertools.groupby() is your solution.
itertools.groupby()是你的解决方案。
newlst = [k for k, g in itertools.groupby(lst)]
If you wish to group and limit the group size by the item's value, meaning 8 4's will be [4,4], and 9 3's will be [3,3,3] here are 2 options that does it:
如果你想按照项目的值对组大小进行分组和限制,意味着8 4将是[4,4],9 3将是[3,3,3]这里有两个选项:
import itertools
def special_groupby(iterable):
last_element = 0
count = 0
state = False
def key_func(x):
nonlocal last_element
nonlocal count
nonlocal state
if last_element != x or x >= count:
last_element = x
count = 1
state = not state
else:
count += 1
return state
return [next(g) for k, g in itertools.groupby(iterable, key=key_func)]
special_groupby(lst)
OR
要么
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return itertools.zip_longest(*args, fillvalue=fillvalue)
newlst = list(itertools.chain.from_iterable(next(zip(*grouper(g, k))) for k, g in itertools.groupby(lst)))
Choose whichever you deem appropriate. Both methods are for numbers > 0.
选择您认为合适的任何一种。两种方法都适用于数字> 0。
#2
2
If you want to use the itertools
method @MaxU suggested, a possible code implementation is:
如果你想使用@MaxU建议的itertools方法,可能的代码实现是:
import itertools as it
lst=[1,2,2,4,4,4,4,1,3,3,3,5,5,5,5,5]
unique_lst = [i[0] for i in it.groupby(lst)]
print(unique_lst)
#3
2
list1 = ['a', 'a', 'a', 'b', 'b' , 'a', 'f', 'c', 'a','a']
temp_list = []
for item in list1:
if len(temp_list) == 0:
temp_list.append(item)
elif len(temp_list) > 0:
if temp_list[-1] != item:
temp_list.append(item)
print(temp_list)
- Fetch each item from the main list(list1).
- 从主列表中获取每个项目(list1)。
- If the 'temp_list' is empty add that item.
- 如果'temp_list'为空,则添加该项。
- If not , check whether the last item in the temp_list is not same as the item we fetched from 'list1'.
- 如果没有,请检查temp_list中的最后一项是否与我们从'list1'获取的项目不同。
- if items are different append into temp_list.
- 如果项目不同,则附加到temp_list中。
#4
0
You'd probably want something like this.
你可能想要这样的东西。
lst = [1, 1, 2, 2, 2, 2, 3, 3, 4, 1, 2]
prev_value = None
for number in lst[:]: # the : means we're slicing it, making a copy in other words
if number == prev_value:
lst.remove(number)
else:
prev_value = number
So, we're going through the list, and if it's the same as the previous number, we remove it from the list, otherwise, we update the previous number.
因此,我们将浏览列表,如果它与前一个数字相同,我们会将其从列表中删除,否则,我们会更新之前的数字。
There may be a more succinct way, but this is the way that looked most apparent to me.
可能有一种更简洁的方式,但这是对我来说最明显的方式。
HTH.
HTH。
#5
0
newlist=[]
prev=lst[0]
newlist.append(prev)
for each in lst[:1]: #to skip 1st lst[0]
if(each!=prev):
newlist.append(each)
prev=each
#6
0
st = ['']
[st.append(a) for a in [1,2,2,4,4,4,4,1,3,3,3,5,5,5,5,5] if a != st[-1]]
print(st[1:])
#7
0
Check if the next element always is not equal to item. If so append.
检查下一个元素是否始终不等于item。如果这样追加。
lst = [1,2,2,4,4,4,4,1,3,3,3,5,5,5,5,5]
new_item = lst[0]
new_list = [lst[0]]
for l in lst:
if new_item != l:
new_list.append(l)
new_item = l
print new_list
print lst