-1

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
4444
  • 3,541
  • 10
  • 32
  • 43
Luis
  • 3
  • 1

3 Answers3

0

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)

Daniel
  • 42,087
  • 4
  • 55
  • 81
0

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.

Community
  • 1
  • 1
EdCornejo
  • 741
  • 13
  • 14
0
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
Emilio M Bumachar
  • 2,532
  • 3
  • 26
  • 30