Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除列表中类似的数字_Python_List_Python 2.7 - Fatal编程技术网

Python 删除列表中类似的数字

Python 删除列表中类似的数字,python,list,python-2.7,Python,List,Python 2.7,如何从以下列表中删除86.1和90.1(或86.2和90.2)之类的数字 86.1 86.2 90.1 90.2 定义一个阈值,迭代排序后的数字,并将阈值内的数字相加: 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(nu

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

86.1      86.2       90.1      90.2

定义一个阈值,迭代排序后的数字,并将阈值内的数字相加:

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
结果。追加(金额/计数)

尝试以下操作:

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 post中,您有更多信息。

无法理解您的要求?将公差值定义为
公差=3
,现在用此值除以每个元素,然后再次乘以相同的值。现在使用
设置
获得所需的结果如何定义类似值?如果两个相似,将删除哪一个?
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