我正在尝试更新在方法/函数开始时初始化的字典,以后应使用函数中的计数器变量更新键的相应值。但是,由于某些原因,我的词典没有更新。请告知。
代码:
def quizresult(quiz_df):
maxmarks=10.0
noofpresent,lessthanfifty,betweenfiftyandsixty,betweensixtyandseventy,betweenseventyandeighty,greaterthaneighty=0.0,0.0,0.0,0.0,0.0,0.0
quiz_result={'noofpresent':None,'lessthan50':None,'between50and60':None,'between60and70':None,'between70and80':None,'greaterthan80':None}
for i in range(len(quiz_df)):
noofpresent=len(quiz_df.index)
if(float(quiz_df.loc[i,'Grade/10.00'])/maxmarks)<0.5:
lessthanfifty += 1
if((float(quiz_df.loc[i,'Grade/10.00'])/maxmarks)>=0.5 and (float(quiz_df.loc[i,'Grade/10.00'])/maxmarks)<0.6):
betweenfiftyandsixty += 1
if((float(quiz_df.loc[i,'Grade/10.00'])/maxmarks)>=0.6 and (float(quiz_df.loc[i,'Grade/10.00'])/maxmarks)<0.7):
betweensixtyandseventy += 1
if((float(quiz_df.loc[i,'Grade/10.00'])/maxmarks)>=0.7 and (float(quiz_df.loc[i,'Grade/10.00'])/maxmarks)<0.8):
betweenseventyandeighty += 1
if(float(quiz_df.loc[i,'Grade/10.00'])/maxmarks)>=0.8:
greaterthaneighty += 1
quiz_result.update({'noofpresent':noofpresent,'lessthan50':lessthanfifty,'between50and60':betweenfiftyandsixty,'between60and70':betweensixtyandseventy,'between70and80':betweenseventyandeighty,'greaterthan80':greaterthaneighty})
return quiz_result
P.S:缩进在这里不是问题。我收到以下消息:
“ SettingWithCopyWarning: 试图在DataFrame的切片副本上设置一个值。 尝试改用.loc [row_indexer,col_indexer] = value
请参阅文档中的警告:http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy 从sys.path中删除cwd之后。”
答案 0 :(得分:0)
您可以手动执行以下操作,以使用计数器变量更新字典的值。 例如,
quiz_result['noofpresent'] = noofpresent #You can do the following for all other keys.
编辑:
您能尝试以下代码并告诉我会发生什么吗?请保留缩进。除此之外,还要确保len(quiz_df)> 0。
def quizresult(quiz_df):
maxmarks=10.0
noofpresent,lessthanfifty,betweenfiftyandsixty,betweensixtyandseventy,betweenseventyandeighty,greaterthaneighty=0.0,0.0,0.0,0.0,0.0,0.0
quiz_result={'noofpresent':0,'lessthan50':0,'between50and60':0,'between60and70':0,'between70and80':0,'greaterthan80':0}
for i in range(len(quiz_df)):
noofpresent=len(quiz_df.index)
if(float(quiz_df.loc[i,'Grade/10.00'])/maxmarks)<0.5:
lessthanfifty += 1
if((float(quiz_df.loc[i,'Grade/10.00'])/maxmarks)>=0.5 and (float(quiz_df.loc[i,'Grade/10.00'])/maxmarks)<0.6):
betweenfiftyandsixty += 1
if((float(quiz_df.loc[i,'Grade/10.00'])/maxmarks)>=0.6 and (float(quiz_df.loc[i,'Grade/10.00'])/maxmarks)<0.7):
betweensixtyandseventy += 1
if((float(quiz_df.loc[i,'Grade/10.00'])/maxmarks)>=0.7 and (float(quiz_df.loc[i,'Grade/10.00'])/maxmarks)<0.8):
betweenseventyandeighty += 1
if(float(quiz_df.loc[i,'Grade/10.00'])/maxmarks)>=0.8:
greaterthaneighty += 1
quiz_result.update(noofpresent =noofpresent ,lessthan50=lessthanfifty ,between50and60=betweenfiftyandsixty,between60and70=betweensixtyandseventy, between70and80=betweenseventyandeighty,greaterthan80= greaterthaneighty)
return quiz_result
我确信您的错误与循环未运行或循环内的条件语句失败有关,因此不会增加值
答案 1 :(得分:0)
您确定缩进正确吗?我运行了以下代码,并获得了预期的字典(字典值为0.0):
date = [self.tableWidget.item(row, 0).text() for row in range(self.tableWidget.rowCount())]
name = [self.tableWidget.item(row, 1).text() for row in range(self.tableWidget.rowCount())]
location = [self.tableWidget.item(row, 2).text() for row in range(self.tableWidget.rowCount())]
item1 = [self.tableWidget.item(row, 3).text() for row in range(self.tableWidget.rowCount())]
item2 = [self.tableWidget.item(row, 4).text() for row in range(self.tableWidget.rowCount())]
item3 = [self.tableWidget.item(row, 5).text() for row in range(self.tableWidget.rowCount())]
item4 = [self.tableWidget.item(row, 6).text() for row in range(self.tableWidget.rowCount())]
item5 = [self.tableWidget.item(row, 7).text() for row in range(self.tableWidget.rowCount())]
item6 = [self.tableWidget.item(row, 8).text() for row in range(self.tableWidget.rowCount())]
conn = sqlite3.connect('test.db')
conn.execute("INSERT INTO maintable(date,name,location,item1,item2,item3,item4,item5,item6) \
VALUES ('%s', '%s','%s', '%s', '%s', '%s', '%s', '%s', '%s')" %(''.join(date),
''.join(name),
''.join(location),
''.join(item1),
''.join(item2),
''.join(item3),
''.join(item4),
''.join(item5),
''.join(item6)))