如何使用 isclose() 将坐标列表中的重复项数作为元组获取?
How to get the number of duplicates in a list of coordinates as tuples with isclose()?
我有一个元组坐标列表:
listoc = ['(-87.5, -960.8147117258)',
'(91.3147117258, -960.8147117258)',
'(85.5, 99.50000000000011)',
'(85.5, -966.8147117258)',
'(91.3147117258, -968.8147117258)',
'(87.5, -968.8147117258)',
'(93.3147117258, -966.8147145072)',
'(93.3147117258, -962.8147117258)',
'(-87.5, -968.8147117258)',
'(-91.3147117258, -155.00000000000009)',
'(-85.5, -966.8147145204)',
'(-85.5, -962.8147117258)',
'(-91.3147117258, -155.0)',
'(-87.5, -960.8147117258)',
'(-93.3147117258, -962.8147089444)',
'(85.500000071, 99.50)',
'(-73.0, -517.5)']
随着math.isclose()
的使用我想得到重复的个数
我在没有 isclose()
的情况下尝试过这个,因为我不知道如何:
for cord in listoc:
number_cc += listoc.count(cord)
print('Number of duplicates: '+ str(number_cc))
输出:
Number of duplicates: 19
但应该是:
Number of duplicates: 3
我正在使用 python3.6
使用:
for cord in listoc:
number_cc += listoc.count(cord) - 1 ### here the difference
print('Number of duplicates: '+ str(number_cc/2)) ### here the difference
否则你至少会得到 listoc 的元素数量,因为 cord 它总是至少在列表中出现一次,并且你计算了两次相同的数字
根据你的标题,如果你想使用 math.isclose(),我最终得到:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 20 12:34:52 2021
@author: Pietro
https://whosebug.com/questions/68054705/how-to-get-the-number-of-duplicates-in-a-list-of-coordinates-as-tuples-with-iscl
"""
import math
import itertools
listoc = ['(-87.5, -960.8147117258)',
'(91.3147117258, -960.8147117258)',
'(85.5, 99.50000000000011)',
'(85.5, -966.8147117258)',
'(91.3147117258, -968.8147117258)',
'(87.5, -968.8147117258)',
'(93.3147117258, -966.8147145072)',
'(93.3147117258, -962.8147117258)',
'(-87.5, -968.8147117258)',
'(-91.3147117258, -155.00000000000009)',
'(-85.5, -966.8147145204)',
'(-85.5, -962.8147117258)',
'(-91.3147117258, -155.0)',
'(-87.5, -960.8147117258)',
'(-93.3147117258, -962.8147089444)',
'(85.500000071, 99.50)',
'(-73.0, -517.5)']
number_cc = 0
for cord, cord2 in itertools.combinations(listoc, 2):
cordx, cordy = cord.split(',')[0].strip('('), cord.split(',')[1].strip(')')
cord2x, cord2y = cord2.split(',')[0].strip('('), cord2.split(',')[1].strip(')')
if math.isclose(float(cordx),float(cord2x)) and math.isclose(float(cordy),float(cord2y)):
number_cc +=1
print('ok #: ' , number_cc,' index : ',listoc.index(cord),cord,' index : ', listoc.index(cord2),cord2)
else:
pass
print(number_cc)
输出为:
ok #: 1 index : 0 (-87.5, -960.8147117258) index : 0 (-87.5, -960.8147117258)
ok #: 2 index : 2 (85.5, 99.50000000000011) index : 15 (85.500000071, 99.50)
ok #: 3 index : 9 (-91.3147117258, -155.00000000000009) index : 12 (-91.3147117258, -155.0)
3
所以 3 个重复,要小心,因为这里:
ok #: 1 index : 0 (-87.5, -960.8147117258) index : 0 (-87.5, -960.8147117258)
第二个索引 0 应该是 13 但是 list.index(x) return 第一个索引有 x 作为值,它实际上是你列表中唯一的 x == y
你可以玩:
math.isclose(a, b, rel_tol, abs_tol)
其中:
a Required. The first value to check for closeness
b Required. The second value to check for closeness
rel_tol = value Optional. The relative tolerance. It is the maximum allowed difference between value a and b. Default value is 1e-09
abs_tol = value Optional. The minimum absolute tolerance. It is used to compare values near 0. The value must be at least 0
import collections
listoc = ['(-87.5, -960.8147117258)',
'(91.3147117258, -960.8147117258)',
'(85.5, 99.50000000000011)',
'(85.5, -966.8147117258)',
'(91.3147117258, -968.8147117258)',
'(87.5, -968.8147117258)',
'(93.3147117258, -966.8147145072)',
'(93.3147117258, -962.8147117258)',
'(-87.5, -968.8147117258)',
'(-91.3147117258, -155.00000000000009)',
'(-85.5, -966.8147145204)',
'(-85.5, -962.8147117258)',
'(-91.3147117258, -155.0)',
'(-87.5, -960.8147117258)',
'(-93.3147117258, -962.8147089444)',
'(85.500000071, 99.50)',
'(-73.0, -517.5)']
print([(item, count) for item, count in collections.Counter(listoc).items() if count > 1])
我有一个元组坐标列表:
listoc = ['(-87.5, -960.8147117258)',
'(91.3147117258, -960.8147117258)',
'(85.5, 99.50000000000011)',
'(85.5, -966.8147117258)',
'(91.3147117258, -968.8147117258)',
'(87.5, -968.8147117258)',
'(93.3147117258, -966.8147145072)',
'(93.3147117258, -962.8147117258)',
'(-87.5, -968.8147117258)',
'(-91.3147117258, -155.00000000000009)',
'(-85.5, -966.8147145204)',
'(-85.5, -962.8147117258)',
'(-91.3147117258, -155.0)',
'(-87.5, -960.8147117258)',
'(-93.3147117258, -962.8147089444)',
'(85.500000071, 99.50)',
'(-73.0, -517.5)']
随着math.isclose()
的使用我想得到重复的个数
我在没有 isclose()
的情况下尝试过这个,因为我不知道如何:
for cord in listoc:
number_cc += listoc.count(cord)
print('Number of duplicates: '+ str(number_cc))
输出:
Number of duplicates: 19
但应该是:
Number of duplicates: 3
我正在使用 python3.6
使用:
for cord in listoc:
number_cc += listoc.count(cord) - 1 ### here the difference
print('Number of duplicates: '+ str(number_cc/2)) ### here the difference
否则你至少会得到 listoc 的元素数量,因为 cord 它总是至少在列表中出现一次,并且你计算了两次相同的数字
根据你的标题,如果你想使用 math.isclose(),我最终得到:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 20 12:34:52 2021
@author: Pietro
https://whosebug.com/questions/68054705/how-to-get-the-number-of-duplicates-in-a-list-of-coordinates-as-tuples-with-iscl
"""
import math
import itertools
listoc = ['(-87.5, -960.8147117258)',
'(91.3147117258, -960.8147117258)',
'(85.5, 99.50000000000011)',
'(85.5, -966.8147117258)',
'(91.3147117258, -968.8147117258)',
'(87.5, -968.8147117258)',
'(93.3147117258, -966.8147145072)',
'(93.3147117258, -962.8147117258)',
'(-87.5, -968.8147117258)',
'(-91.3147117258, -155.00000000000009)',
'(-85.5, -966.8147145204)',
'(-85.5, -962.8147117258)',
'(-91.3147117258, -155.0)',
'(-87.5, -960.8147117258)',
'(-93.3147117258, -962.8147089444)',
'(85.500000071, 99.50)',
'(-73.0, -517.5)']
number_cc = 0
for cord, cord2 in itertools.combinations(listoc, 2):
cordx, cordy = cord.split(',')[0].strip('('), cord.split(',')[1].strip(')')
cord2x, cord2y = cord2.split(',')[0].strip('('), cord2.split(',')[1].strip(')')
if math.isclose(float(cordx),float(cord2x)) and math.isclose(float(cordy),float(cord2y)):
number_cc +=1
print('ok #: ' , number_cc,' index : ',listoc.index(cord),cord,' index : ', listoc.index(cord2),cord2)
else:
pass
print(number_cc)
输出为:
ok #: 1 index : 0 (-87.5, -960.8147117258) index : 0 (-87.5, -960.8147117258)
ok #: 2 index : 2 (85.5, 99.50000000000011) index : 15 (85.500000071, 99.50)
ok #: 3 index : 9 (-91.3147117258, -155.00000000000009) index : 12 (-91.3147117258, -155.0)
3
所以 3 个重复,要小心,因为这里:
ok #: 1 index : 0 (-87.5, -960.8147117258) index : 0 (-87.5, -960.8147117258)
第二个索引 0 应该是 13 但是 list.index(x) return 第一个索引有 x 作为值,它实际上是你列表中唯一的 x == y
你可以玩:
math.isclose(a, b, rel_tol, abs_tol)
其中:
a Required. The first value to check for closeness
b Required. The second value to check for closeness
rel_tol = value Optional. The relative tolerance. It is the maximum allowed difference between value a and b. Default value is 1e-09
abs_tol = value Optional. The minimum absolute tolerance. It is used to compare values near 0. The value must be at least 0
import collections
listoc = ['(-87.5, -960.8147117258)',
'(91.3147117258, -960.8147117258)',
'(85.5, 99.50000000000011)',
'(85.5, -966.8147117258)',
'(91.3147117258, -968.8147117258)',
'(87.5, -968.8147117258)',
'(93.3147117258, -966.8147145072)',
'(93.3147117258, -962.8147117258)',
'(-87.5, -968.8147117258)',
'(-91.3147117258, -155.00000000000009)',
'(-85.5, -966.8147145204)',
'(-85.5, -962.8147117258)',
'(-91.3147117258, -155.0)',
'(-87.5, -960.8147117258)',
'(-93.3147117258, -962.8147089444)',
'(85.500000071, 99.50)',
'(-73.0, -517.5)']
print([(item, count) for item, count in collections.Counter(listoc).items() if count > 1])