擅长:python、mysql、java
<p>将数据结构切换到元组列表,然后使用最小值作为关键函数进行排序(使用memoization以提高效率):</p>
<pre><code>M = [(20, 10), (3, 4), (5,6), (1.2, 7), (6.5, 4)]
def minimum_value(coordinate, dictionary={}): # intentional dangerous default value
if coordinate not in dictionary:
dictionary[coordinate] = coordinate[0]**2 + coordinate[1]**2
return dictionary[coordinate]
M_sorted = sorted(M, key=minimum_value)
print(M_sorted)
</code></pre>
<p><strong>输出</strong></p>
<pre><code>[(3, 4), (1.2, 7), (6.5, 4), (5, 6), (20, 10)]
</code></pre>
<p>因为我们只是排序,我们不需要计算平方根,平方就足够了。你知道吗</p>