How do I remove numbers like 86.1 and 90.1 (or 86.2 and 90.2) from the following list?
86.1 86.2 90.1 90.2
How do I remove numbers like 86.1 and 90.1 (or 86.2 and 90.2) from the following list?
86.1 86.2 90.1 90.2
Define a threshold, iterate over the sorted numbers and add up the numbers within the threshold:
numbers = [86.1, 86.2, 90.1,90.2]
threshold = 1
numbers = iter(numbers)
amount = last = next(numbers)
count = 1
result = []
for number in sorted(numbers):
if number - last > threshold:
result.append(amount/count)
amount = count = 0
amount += number
count += 1
last = number
result.append(amount/count)
Try this:
base = [86.1, 86.2, 90.1, 90.2]
# remove = [86.2, 90.2]
remove = [86.1, 90.1]
new_list = [item for item in base if item not in remove]
print(new_list)
In Stack Overflow post Remove list from list in Python you have more information.
inputList=[86.1, 86.2, 90.1, 90.2]
tolerance=1.0
out=[]
for num in inputList:
if all([abs(num-outAlready)>tolerance for outAlready in out]):
out.append(num)
print out