I am trying to remove items from a list using a for loop in Python, if they are equal to the previous element in the list.
When I try to use del, I get a list index out of range error, which through research I have learned that it is because using del alters the length of my list.
Other advice has said to use list comprehension instead, but I'm unsure how to compare the previous element to the current element, while doing so.
This is my current code:
for x in range(1, len(items)):
if items[x] == items[x-1]:
del items[x]
del items[x - 1]
return result
Try this:
import pandas as pd
items = [1, 4, 4, 5, 5, 5, 6, 4, 5, 7]
remove_us = []
for x in range(1, len(items)):
if items[x] == items[x - 1]:
# save the index
remove_us.append(x)
# create a Series out of your items
items_temp = pd.Series(items)
items_filtered = items_temp.drop(axis=1, index=remove_us).values
# With .values you get a numpy.ndarray
# If, for some reason, you need a list you can do:
# items_filtered = list(items_filtered)
result:
array([1, 4, 5, 6, 4, 5, 7])
The following is an approach, in case you want to remove dublicates in general:
import pandas as pd
items_temp = pd.Series(items)
items_filtered = items_temp[~items_temp.duplicated(keep=False).values
I tried to address your problem changing the mechanics only where necessary:
def my_func(list):
# copy input list for data-safety reasons
result = list.copy()
# empty list and 1-element list require special treatment
if len(list)==0 or len(list)==1:
return result
# init current_index
current_index = 0
while(current_index < len(result)-1):
# validation-check for testing purposes, comment or delete for final version
print("at current_index: {}".format(current_index))
print("analyzed list is: {}".format(result))
print("analyzed list is of length: {}".format(len(result)))
print("comparing: {} with {}".format(result[current_index], result[current_index+1]))
if result[current_index] == result[current_index+1]:
result.pop(current_index)
# don't augment current_index
current_index += 0
else:
# augment current_index
current_index += 1
print()
return result
Tests:
>>> my_func([])
[]
my_func(['a'])
['a']
>>> my_func(['a','a'])
at current_index: 0
analyzed list is: ['a', 'a']
analyzed list is of length: 2
comparing: a with a
['a']
>>> my_func(['a','a','a'])
at current_index: 0
analyzed list is: ['a', 'a', 'a']
analyzed list is of length: 3
comparing: a with a
at current_index: 0
analyzed list is: ['a', 'a']
analyzed list is of length: 2
comparing: a with a
['a']
>>> my_func(['a','b','b','c','b','c','c'])
at current_index: 0
analyzed list is: ['a', 'b', 'b', 'c', 'b', 'c', 'c']
analyzed list is of length: 7
comparing: a with b
at current_index: 1
analyzed list is: ['a', 'b', 'b', 'c', 'b', 'c', 'c']
analyzed list is of length: 7
comparing: b with b
at current_index: 1
analyzed list is: ['a', 'b', 'c', 'b', 'c', 'c']
analyzed list is of length: 6
comparing: b with c
at current_index: 2
analyzed list is: ['a', 'b', 'c', 'b', 'c', 'c']
analyzed list is of length: 6
comparing: c with b
at current_index: 3
analyzed list is: ['a', 'b', 'c', 'b', 'c', 'c']
analyzed list is of length: 6
comparing: b with c
at current_index: 4
analyzed list is: ['a', 'b', 'c', 'b', 'c', 'c']
analyzed list is of length: 6
comparing: c with c
['a', 'b', 'c', 'b', 'c']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.