Python 陷入势无限循环
我只是试图从pandas数据框中列出地区名称和地区对象,但由于某些原因,代码从未运行完。我看不到任何地方可以成为一个无限循环,所以我无法理解为什么每次运行它时它都会被卡住。下面是卡住的部分(特别是j迭代for循环): 以下是地区级别,以供参考:Python 陷入势无限循环,python,python-3.x,Python,Python 3.x,我只是试图从pandas数据框中列出地区名称和地区对象,但由于某些原因,代码从未运行完。我看不到任何地方可以成为一个无限循环,所以我无法理解为什么每次运行它时它都会被卡住。下面是卡住的部分(特别是j迭代for循环): 以下是地区级别,以供参考: class District: def __init__(self, name, count): self._name = name self._count = count def get_name(self): return n
class District:
def __init__(self, name, count):
self._name = name
self._count = count
def get_name(self):
return name
def get_count(self):
return count
def updateCount(self,amount):
self._count += amount
初始的.csv文件相当大,在删除第8行和第9行中的一些数据点后,我剩下227312个数据点。我知道这是相当多的,但代码甚至在运行5分钟后还没有完成。我做错了什么?您可以使用
tqdm
包查看您的代码固定在哪个循环中
import tqdm from tqdm
for i in tqdm(range(s[0]), position=0, leave=True):
check = True
#build strings for each district
ds = table.iloc[i,1] + str(table.iloc[i,2])
#testString = str(table.iloc[i,2])
#append ds to districtNames if it isnt in already
#make array of District Objects
for j in range(len(districtNames)):
if(ds == districtNames[j]):
check = False
if(check):
districtNames.append(ds)
districts.append(District(ds,0))
这并不是说它不会终止,而是它在当前状态下效率低下。试着这样做:
import numpy as np
import pandas as pd
class District:
def __init__(self, name, count):
self._name = name
self._count = count
def get_name(self):
return name
def get_count(self):
return count
def updateCount(self,amount):
self._count += amount
#make dataframe
data = pd.read_csv('gun-violence-data_01-2013_03-2018.csv', header=0, delimiter=',')
#drop data points with null condressional district values
data = data[data.congressional_district != 0]
data.dropna(axis=0,how='any',subset=['congressional_district'],inplace= True)
#constructing working table
table = data[['incident_id','state','congressional_district']]
#list of districts. Formatting in original file must be corrected to analyze data
districtNames = (table.state + table.congressional_district.astype(str)).unique()
districts = list(map(lambda districtName: District(districtName, 0), districtNames))
放入一些打印行并调试它不是一个修复程序,但是你可以在范围内将j的
缩短(len(districtNames)):
到到districtNames中的districtName:
print是你的朋友,在每个循环的开始处粘贴一个,这样你就可以看到什么在吸引你,这些东西进展得很快!我不知道你甚至可以用Python来做这件事。非常感谢你!
import numpy as np
import pandas as pd
class District:
def __init__(self, name, count):
self._name = name
self._count = count
def get_name(self):
return name
def get_count(self):
return count
def updateCount(self,amount):
self._count += amount
#make dataframe
data = pd.read_csv('gun-violence-data_01-2013_03-2018.csv', header=0, delimiter=',')
#drop data points with null condressional district values
data = data[data.congressional_district != 0]
data.dropna(axis=0,how='any',subset=['congressional_district'],inplace= True)
#constructing working table
table = data[['incident_id','state','congressional_district']]
#list of districts. Formatting in original file must be corrected to analyze data
districtNames = (table.state + table.congressional_district.astype(str)).unique()
districts = list(map(lambda districtName: District(districtName, 0), districtNames))