[英]Python TypeError: str, int, float
我正在
TypeError: 'float' object is not subscriptable
對於下面的最后一行。 我不理解為什么。 這是錯誤的值還是索引?
假定是由於這些值,我嘗試將這些值強制轉換為float
和str
,但是這兩種方法都無法解決此錯誤,而是根據類型的轉換方式獲取了類型為int或str的TypeErrors。
我用相同的說明查看了StackOverflow上的其他幾個問題,但是沒有一個解決了我的問題。 有人可以幫忙嗎? 我足夠清楚嗎?
def initialize_combined_scores(self):
sum_weights = self.dist_weight + self.coc_weight + self.optimization_weight + self.productivity_weight
normalized_dist_weight = self.dist_weight / sum_weights
normalized_coc_weight = self.coc_weight / sum_weights
normalized_optimization_weight = self.optimization_weight / sum_weights
normalized_productivity_weight = self.productivity_weight / sum_weights
self.combined_scores = normalized_dist_weight
self.combined_scores_df = pandas.DataFrame({'WorkerID':self.worker_ids})
self.combined_scores_df['WorkerClusterLat'] = [xx for xx,yy in self.worker_ll]
self.combined_scores_df['WorkerClusterLon'] = [yy for xx,yy in self.worker_ll]
for col_name in self.worker_col_keepers:
self.combined_scores_df[col_name] = self.worker_df[col_name]
for pidx, patient_id in enumerate(self.patient_ids):
self.combined_scores_df[str(patient_id)] = self.combined_scores[:, pidx]
self.combined_scores
可能是float
。 我在您的代碼中看到以下幾行:
normalized_dist_weight = self.dist_weight / sum_weights
...
self.combined_scores = normalized_dist_weight
名稱表明self.dist_weight
和sum_weights
都可以是float
。 這樣除法的結果將是float
。
編輯:
如果self.combined_scores
是多維容器,則最后一行只能工作。 可能是pandas.DataFrame
。
我認為錯誤在於:
self.combined_scores = normalized_dist_weight
這里似乎缺少一些東西。 類似的self.combined_scores_df
綁定到下面一行中的pandas.DataFrame
。 我相信,創建pandas.DataFrame
並將其綁定到self.combined_scores
被錯誤地刪除了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.