我有一个用户列表,每个用户都有纬度和经度值。我正在尝试为每个用户创建“附近的用户”列表。我相信我的距离计算可以正常工作,但是我仍然坚持为每个用户创建列表。
df = pd.read_excel(filename, converters={'name_of_date_column': lambda dt: dt.strftime('%d/%m/%Y')})
我希望附近的用户互惠互利,但这并不是合乎逻辑的。这是我收到的结果:
users = [
{'email':'austin1@gmail.com', 'lat': 30.3, 'long': -97.67, 'nearby_users': []},
{'email':'austin2@gmail.com', 'lat': 30.2, 'long': -97.73,'nearby_users': []},
{'email':'austin3@gmail.com', 'lat': 30.26, 'long': -97.74,'nearby_users': []},
{'email':'palm_beach@gmail.com', 'lat': 26.62, 'long': -80.05, 'nearby_users': []}
]
radius = 1
def compare_users(user_a, user_b):
a_email = user_a['email']
a_lat = float(user_a['lat'])
a_long = float(user_a['long'])
a_nearby_users = user_a['nearby_users']
b_email = user_b['email']
b_lat = float(user_b['lat'])
b_long = float(user_b['long'])
b_nearby_users = user_b['nearby_users']
if b_lat > (a_lat - radius):
if b_lat < (a_lat + radius):
if b_long > (a_long - radius):
if b_long < (a_long + radius):
b_nearby_users.append(a_email)
for user_a in range(len(users)):
for user_b in range(user_a + 1, len(users)):
compare_users(users[user_a], users[user_b])
感谢您的帮助!
答案 0 :(得分:0)
当我在评论中添加以下内容时,我认为结果正确:
users = [
{'email':'austin1@gmail.com', 'lat': 30.3, 'long': -97.67, 'nearby_users': []},
{'email':'austin2@gmail.com', 'lat': 30.2, 'long': -97.73,'nearby_users': []},
{'email':'austin3@gmail.com', 'lat': 30.26, 'long': -97.74,'nearby_users': []},
{'email':'palm_beach@gmail.com', 'lat': 26.62, 'long': -80.05, 'nearby_users': []}
]
radius = 1
def compare_users(user_a, user_b):
a_email = user_a['email']
a_lat = float(user_a['lat'])
a_long = float(user_a['long'])
a_nearby_users = user_a['nearby_users']
b_email = user_b['email']
b_lat = float(user_b['lat'])
b_long = float(user_b['long'])
b_nearby_users = user_b['nearby_users']
if b_lat > (a_lat - radius):
if b_lat < (a_lat + radius):
if b_long > (a_long - radius):
if b_long < (a_long + radius):
a_nearby_users.append(b_email)
b_nearby_users.append(a_email)
for user_a in range(len(users)):
for user_b in range(user_a + 1, len(users)):
compare_users(users[user_a], users[user_b])
print(users)
输出:
[{''nearby_users':['austin2@gmail.com','austin3@gmail.com'],'lat':30.3,'email':'austin1@gmail.com','long':- 97.67},{'nearby_users':['austin1@gmail.com','austin3@gmail.com'],'lat':30.2,'email':'austin2@gmail.com','long':-97.73 },{'nearby_users':['austin1@gmail.com','austin2@gmail.com'],'lat':30.26,'email':'austin3@gmail.com','long':-97.74} ,{'nearby_users':[],'lat':26.62,'email':'palm_beach@gmail.com','long':-80.05}]