2017-11-01 6 views
1

このプログラミングの宿題に取り組んでいます。現在、1つのタプルの値のペアをリストのタプルペアと比較することに固執しています。組のタプルをタプルペアのリストと比較する

ペアは基本的にx座標とy座標です。リストからタプルペアに最も近いものを見つける必要があります。一例として、ポイント(-4, 3)とリスト[(10, 6), (1, 7), (6, 3), (1, 9)]が与えられた場合、最も近いものは(1, 7)となる。

これらの数値は、プログラミングのランダムな部分で常に変化しますが、上記は関数として定義されています。ここでは全体のことです。私が何をしようとしています何

def nearest(point, more_points): 
    ''' 
    Finds the nearest point to the base point 
    ''' 
    (x, y) = point 
    for i, j in more_points: 
     a = math.fabs(x - i) 
     tempI = i 
     b = math.fabs(y - j) 
     tempJ = j 
     tempA, tempB = a , b 
     if min(tempA) < a: 

point =() 
my_points = [] 
c = 0 
lpoint = list(point) 
while c < 2: 
    lpoint.append(random.randrange(-5,5,1)) # generate the "point" 
    c += 1 
tpoint = tuple(lpoint) 
c = 0 
colx = [] # x points 
coly = [] # y points 
# generate the points 
while c < 4: 
    colx.append(random.randint(0,10)) 
    coly.append(random.randint(0,10)) 
    c += 1 

my_point = list(zip(colx,coly)) 
print(my_point) 
the_nearest = nearest(tpoint,my_point) 
print(the_nearest) 

は、Xを取る時点で、yとし、「その他」のポイントを取り、その差を取得し、「最も近いを見つけるためにそれを使用することです"しかし、私は失われ、私は立ち往生しています。フォーカスは、ユーザー定義関数にあります。

+1

はあなたが適切なインデントを作るためにあなたのコードを編集することはできますか?機能に含まれるものとそうでないものについては不明です。 – FunkySayu

+0

@FunkySayu残念ですがstackoverflowを使用するのに慣れていませんでしたが、 – Gary

+0

'if min(tempA) FunkySayu

答えて

5

次関数は2ポイント以内の距離を計算すると仮定すると:私はより多くのニシキヘビを考え出すことができますが

def nearest(point, all_points): 
    closest_point, best_distance = None, float("inf") 
    for other_point in all_points: 
     d = distance(point, other_point) 
     if d < best_distance: 
      closest_point, best_distance = other_point, d 
    return closest_point 

def distance(point_a, point_b): 
    """Returns the distance between two points.""" 
    x0, y0 = point_a 
    x1, y1 = point_b 
    return math.fabs(x0 - x1) + math.fabs(y0 - y1) 

あなたはすべてのポイントを反復処理し、最小距離を見つけることができますアプローチ:

def nearest(point, all_points): 
    """Returns the closest point in all_points from the first parameter.""" 
    distance_from_point = functools.partial(distance, point) 
    return min(all_points, key=distance_from_point) 

上記の解決策の全体的な考え方は次のとおりです。部分的な機能を構築する。この部分関数は単一のパラメータをとり、パラメータとして与えられた点までの距離を返します。これはlambda other_point: distance(point, other_point)と書き直すことができますが、これはよりきれいです。

上記の関数は、nearest(point, [])の空のリストで呼び出すと、ValueErrorを呼び出すことに注意してください。必要に応じて、この場合のifを追加することができます。

+0

ニース。私は 'min()'が重要な機能を果たしたことを知らなかった。 – Harvey

+0

ここでは、私たちがfunctoolsを使用することが許されているかどうかはまだ分かりません.MurahのPython Programming @FunkySayu – Gary

+0

の教科書のユーザ定義の関数になって以来、心配はありません。コードブロック)またはラムダを使用してください:) – FunkySayu

0

key機能と使用min()

#!/usr/bin/env python3 

import math 
from functools import partial 


def distance_between_points(a, b): 
    ax, ay = a 
    bx, by = b 
    return math.sqrt(pow(ax - bx, 2) + pow(ay - by, 2)) 


def nearest(point, more_points): 
    ''' 
    Finds the nearest point to the base point 
    ''' 
    distance_to_point = partial(distance_between_points, point) 
    return min(more_points, key=distance_to_point) 


point = (-4, 3) 
my_points = [(10, 6), (1, 7), (6, 3), (1, 9)] 
n = nearest(point, my_points) 
print(n) 
関連する問題