擅长:python、mysql、java
<p>使用<code>key</code>参数,以便在只比较一项时返回整点</p>
<pre><code>from operator import itemgetter
max_x = max(p, key=itemgetter(0))
min_x = min(p, key=itemgetter(0))
max_y = max(p, key=itemgetter(1))
min_y = min(p, key=itemgetter(1))
</code></pre>
<p>您也可以使用单个循环来完成此操作,而不是调用<code>min()</code>和<code>max()</code></p>
<pre><code>min_x = max_x = min_y = max_y = p[0]
for point in p[1:]:
if point[0] < min_x[0]:
min_x = point
if point[0] > max_x[0]:
max_x = point
if point[1] < min_y[1]:
min_y = point
if point[1] > max_y[1]:
max_y = point
</code></pre>