为什么这个 python 函数 return 是一个重复列表?

Why does this python function return a list of duplicates?

我正在尝试编写一个代码块,它作用于两组地理坐标(在本例中为街区和暴力犯罪)。它计算每个街区 500 米半径内的犯罪数量,将这些数字添加到列表中,然后将该列表与街区数据集合并。

#For each neighbourhood in city find all crimes committed within a 500m radius

from geopy import distance

def Crime_in_Hood(crimes):
#counts number of crimes in 500m radius of neighbourhood
   crime_count = 0
   
   for index, crime in crimes.iterrows():
       test_point = [{crime['Latitude'], crime['Longitude']}]
       radius = 0.5# in km

       center_point_tuple = tuple(center_point)
       test_point_tuple = tuple(test_point)

       dis = distance.distance(center_point_tuple, test_point_tuple).km

       if dis <= radius:
           crime_count = crime_count + 1
           
   return crime_count


def Crime_in_City(crimes, hoods):
#returns number of crimes for each neighbourhood in city
   
#instantiate crime tally
   violent_crimes = []
   
   for index, hood in hoods.iterrows():
       center_point = [{hood['Latitude'], hood['Longitude']}]
       crime_count = Crime_in_Hood(crimes, hoods)
       violent_crimes.append(crime_count)
       
   return violent_crimes

       
def Crime_Counter(crimes, hoods):

#Add the number of crimes committed to dataframe for each neighbourhood in city
   violent_crimes = Crime_in_City(crimes, hoods)

   #Add violent_crimes to hoods
   hoods["Violent Crimes"] = violent_crimes
   return hoods 


ny_hoods = Crime_Counter(ny_c, ny_hoods)
ny_hoods.head()  

问题是它 returns 一个数据框,其中“暴力犯罪”列中的每个值都是相同的。

我用不同的值测试了 Crime_in_Hood 函数,它按预期工作。 Crime_in_City,然而,returns 一个列表,其中最后一个 crime_count 值一遍又一遍地重复。所以我现在这就是我的问题所在,而不是如何解决它。据我所知,Crime_in_Hood 应该为 Crime_in_City 的每次迭代返回不同的值并将其附加到 violent_crimes,但事实并非如此。

有什么想法吗?

Crime_in_Hood(crimes) 只有一个定义参数,但您在这里使用 2 个参数调用它:crime_count = Crime_in_Hood(crimes, hoods)。这就是您的代码的外观(如下)。 ny_c 和 ny_hoods 都应该是 pandas DataFrame,其中 'Latitude' 和 'Longitude' 列具有 float64 值。除此之外,我不确定 geopy 在以下方面的工作原理:{Latitude, Longitude} 表示法。

#For each neighbourhood in city find all crimes committed within a 500m radius

from geopy import distance

def Crime_in_Hood(crimes, center_point):
#counts number of crimes in 500m radius of neighbourhood
   crime_count = 0
   
   for index, crime in crimes.iterrows():
       test_point = [{crime['Latitude'], crime['Longitude']}]
       radius = 0.5# in km

       center_point_tuple = tuple(center_point)
       test_point_tuple = tuple(test_point)

       dis = distance.distance(center_point_tuple, test_point_tuple).km

       if dis <= radius:
           crime_count = crime_count + 1
           
   return crime_count


def Crime_in_City(crimes, hoods):
#returns number of crimes for each neighbourhood in city
   
#instantiate crime tally
   violent_crimes = []
   
   for index, hood in hoods.iterrows():
       center_point = [{hood['Latitude'], hood['Longitude']}]
       crime_count = Crime_in_Hood(crimes, center_point)
       violent_crimes.append(crime_count)
       
   return violent_crimes


def Crime_Counter(crimes, hoods):

#Add the number of crimes committed to dataframe for each neighbourhood in city
   violent_crimes = Crime_in_City(crimes, hoods)

   #Add violent_crimes to hoods
   hoods["Violent Crimes"] = violent_crimes
   return hoods 


ny_hoods = Crime_Counter(ny_c, ny_hoods)
ny_hoods.head()