我有一个list,list里面的element是dict。[{centre:(743,1105), radius: 41},{centre:(743, 1106), radius: 48},{centre:(899, 1443), radius: 48},{centre:(900, 1442), radius: 40}]这个关于圆心和半径的一个数据结构。我想把圆心相近的圆(横坐标相差+3/-3 左右)去掉一个(保留半径较小的)def takeXAxis(input): return input['centre'][0]def sortCircles(circleDetails):
circleDetails.sort(key=takeXAxis)def removeClosedCircle(circleDetails):
newCircleDetails = [] for i in range(len(circleDetails)):
j = i + 1
for j in range(len(circleDetails)):
...接下来我就不太会了,有人能帮我看下吗?
2 回答
临摹微笑
TA贡献1982条经验 获得超2个赞
import itertools my_list = [ {'centre':(743,1105), 'radius': 41}, {'centre':(743, 1106), 'radius': 48}, {'centre':(899, 1443), 'radius': 48}, {'centre':(900, 1442), 'radius': 40} ]for a, b in itertools.combinations(my_list, 2): # only need to do something if the diff is in range.. if abs(a['centre'][0] - b['centre'][0]) <= 3: # check the radius, if bigger, remove it, else remove the other. if a['radius'] > b['radius']: my_list.remove(a) else: my_list.remove(b)print my_list
添加回答
举报
0/150
提交
取消