Python 当外部循环的一个变量在内部循环中运行时,如何执行嵌套循环?
我有三本字典,坐标如下: 第一条Python 当外部循环的一个变量在内部循环中运行时,如何执行嵌套循环?,python,pandas,loops,nested-loops,Python,Pandas,Loops,Nested Loops,我有三本字典,坐标如下: 第一条一条犯罪记录包含唯一犯罪记录 (34.0141, -118.2978) 第二条犯罪记录包含多起犯罪 {0: (34.0141, -118.2978), 1: (34.0459, -118.2545), 2: (34.1847, -118.44)} 第三个相邻线包含多个街区 {'Acton': (34.497355239240854, -118.1698101922935), 'Adams-Normandie': (34.03146149912416, -118
一条犯罪记录
包含唯一犯罪记录
(34.0141, -118.2978)
第二条犯罪记录
包含多起犯罪
{0: (34.0141, -118.2978), 1: (34.0459, -118.2545), 2: (34.1847, -118.44)}
第三个相邻线
包含多个街区
{'Acton': (34.497355239240854, -118.1698101922935), 'Adams-Normandie': (34.03146149912416, -118.30020800000014), 'Agoura Hills': (34.146736499122795, -118.75988450000015)}
我试图设置一个嵌套循环来获得每个犯罪到每个邻居的距离,然后根据最小距离将每个犯罪所属的邻居输入到外部数据框中。
到目前为止,我已经能够通过以下代码行对一个犯罪案件执行此操作:
第一行代码:
d3=[]
n_name=[]
for neigh_name, coord in neigh_cord.items():
d2=distance (one_crime_coord,coord).m
n_name.append(neigh_name)
d3.append(d2)
print(neigh_name,d2)
distan_df=pd.DataFrame()
distan_df['Neighborhood_Name']=n_name;
distan_df['Distance to Crime']=d3
print(min(d3))
distan_df
index_min = np.argmin(d3)
print('------')
print('The Crime was commited in the neighborhood:')
print(distan_df.iloc[index_min,0])
test=la_crime_refined.copy()
test['Neighborhood']=''
test.head()
test.at[index_min,'Neighborhood']= distan_df.iloc[index_min,0]
display(test.iloc[index_min])
输出:
Acton 54886.9994821537
Adams-Normandie 1938.5858465073106
Agoura Hills 45115.60137307561
472.2815720091788
------
The Crime was commited in the neighborhood:
Exposition Park
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-182-3e0c072951c2> in <module>
2 n_name2=[]
3 d5=[]
----> 4 for crime_no,crime_coord in crime_cord.items():
5 for neigh_name, coord in neigh_cord.items():
6 d4=distance (crime_coord,coord).m
AttributeError: 'tuple' object has no attribute 'items'
第二行代码:
d3=[]
n_name=[]
for neigh_name, coord in neigh_cord.items():
d2=distance (one_crime_coord,coord).m
n_name.append(neigh_name)
d3.append(d2)
print(neigh_name,d2)
distan_df=pd.DataFrame()
distan_df['Neighborhood_Name']=n_name;
distan_df['Distance to Crime']=d3
print(min(d3))
distan_df
index_min = np.argmin(d3)
print('------')
print('The Crime was commited in the neighborhood:')
print(distan_df.iloc[index_min,0])
test=la_crime_refined.copy()
test['Neighborhood']=''
test.head()
test.at[index_min,'Neighborhood']= distan_df.iloc[index_min,0]
display(test.iloc[index_min])
输出:
Acton 54886.9994821537
Adams-Normandie 1938.5858465073106
Agoura Hills 45115.60137307561
472.2815720091788
------
The Crime was commited in the neighborhood:
Exposition Park
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-182-3e0c072951c2> in <module>
2 n_name2=[]
3 d5=[]
----> 4 for crime_no,crime_coord in crime_cord.items():
5 for neigh_name, coord in neigh_cord.items():
6 d4=distance (crime_coord,coord).m
AttributeError: 'tuple' object has no attribute 'items'
第三行代码:
d3=[]
n_name=[]
for neigh_name, coord in neigh_cord.items():
d2=distance (one_crime_coord,coord).m
n_name.append(neigh_name)
d3.append(d2)
print(neigh_name,d2)
distan_df=pd.DataFrame()
distan_df['Neighborhood_Name']=n_name;
distan_df['Distance to Crime']=d3
print(min(d3))
distan_df
index_min = np.argmin(d3)
print('------')
print('The Crime was commited in the neighborhood:')
print(distan_df.iloc[index_min,0])
test=la_crime_refined.copy()
test['Neighborhood']=''
test.head()
test.at[index_min,'Neighborhood']= distan_df.iloc[index_min,0]
display(test.iloc[index_min])
在我试图填充的数据框中的输出:
Neighborhood
83 Exposition Park
我想我可以使用嵌套循环做类似的事情:
distan_matrix=pd.DataFrame()
n_name2=[]
d5=[]
for crime_no,crime_coord in crime_cord.items():
for neigh_name, coord in neigh_cord.items():
d4=distance (crime_coord,coord).m
n_name2.append(neigh_name)
d5.append(d4)
distan_matrix=pd.DataFrame()
distan_matrix['Neighborhood_Name']=n_name2;
distan_matrix['Distance to Crime']=d4
index_min = np.argmin(d4)
test.at[index_min,'Neighborhood']= distan_matrix.iloc[index_min,0]
print('------')
print('All Crimes have been assigned to a neighborhood')
test.head()
输出:
Acton 54886.9994821537
Adams-Normandie 1938.5858465073106
Agoura Hills 45115.60137307561
472.2815720091788
------
The Crime was commited in the neighborhood:
Exposition Park
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-182-3e0c072951c2> in <module>
2 n_name2=[]
3 d5=[]
----> 4 for crime_no,crime_coord in crime_cord.items():
5 for neigh_name, coord in neigh_cord.items():
6 d4=distance (crime_coord,coord).m
AttributeError: 'tuple' object has no attribute 'items'
然而,我无法让它工作。是因为我在内环中使用外环变量吗?有什么建议吗
谢谢你,很抱歉问题这么长 在第4行,您已经编写了
犯罪记录。条目[]
您的语法错误在这里,这是因为crime\u cord.items是一个函数,而不是一个列表。尝试将其替换为
crime\u cord.items()
显示的错误消息与显示的代码不对应。该错误只是一个语法错误,在您显示的代码中,您拥有正确的语法。我不明白你所说的变量“运行在”给定循环中是什么意思,但你所展示的正是嵌套循环的正常工作方式。你发布的错误中有一个明显的语法错误items
是一种方法,应该使用.items()
,而不是items[]
)调用。此外,您的代码与下面的输出不匹配,因此请仔细检查您发布的所有代码。我的错误,这是我当前收到的实际错误消息@Karlknechtel错误告诉您,crime\u cord
与您在问题顶部描述的不同。您需要修复根本问题,这不在您显示的代码部分中。另外,请尽量选择更好的名字。你一定会在某个时候把crime\u cord
和crime\u coord
搞混。我猜您已经在其他地方的另一个for
循环中完全做到了这一点,并将dict替换为它的一个值(即元组)。