删除列表中的类似数字

时间:2016-09-17 06:54:49

标签: python list python-2.7

如何从以下列表中删除86.1和90.1(或86.2和90.2)等数字?

86.1      86.2       90.1      90.2

3 个答案:

答案 0 :(得分:0)

定义阈值,迭代已排序的数字并将数字加在阈值内:

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(量/计数)

答案 1 :(得分:0)

试试这个:

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)

在Stack Overflow帖子 Remove list from list in Python 中,您有更多信息。

答案 2 :(得分: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