亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何遍歷python list,然后刪除近似元素

如何遍歷python list,然后刪除近似元素

Helenr 2019-03-01 10:08:56
我有一個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


查看完整回答
反對 回復 2019-03-11
?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

問題不清楚, 如果有圓 x=1, 另一個 x=5 這時來一個x=3 前面兩個圓都去掉?
圓心一定是整數嗎?

查看完整回答
反對 回復 2019-03-11
  • 2 回答
  • 0 關注
  • 785 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號