简体   繁体   English

Python TypeError:str,int,float

[英]Python TypeError: str, int, float

I am getting the 我正在

TypeError: 'float' object is not subscriptable 

for the last line below. 对于下面的最后一行。 I do not understand why. 我不理解为什么。 Is this erroring on the values or the indices? 这是错误的值还是索引?

Assuming it's because of the values, I tried casting these values as float and str , but neither of these approaches fixes this error and instead I get TypeErrors of type int or str, depending on how I cast them. 假定是由于这些值,我尝试将这些值强制转换为floatstr ,但是这两种方法都无法解决此错误,而是根据类型的转换方式获取了类型为int或str的TypeErrors。

I looked at several other Questions on StackOverflow with the same description, but none of these resolved my problems. 我用相同的说明查看了StackOverflow上的其他几个问题,但是没有一个解决了我的问题。 Can anyone help? 有人可以帮忙吗? Am I being clear enough? 我足够清楚吗?

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 might be a float . self.combined_scores可能是float I see the following lines in your code: 我在您的代码中看到以下几行:

normalized_dist_weight = self.dist_weight / sum_weights
...
self.combined_scores = normalized_dist_weight

The names suggest that both self.dist_weight and sum_weights could both very well be float . 名称表明self.dist_weightsum_weights都可以是float The result of the division would then be a float . 这样除法的结果将是float

Edit: 编辑:

The last line can only work, if self.combined_scores is a multidimensional container. 如果self.combined_scores是多维容器,则最后一行只能工作。 A pandas.DataFrame probably. 可能是pandas.DataFrame

I think the error is in: 我认为错误在于:

self.combined_scores = normalized_dist_weight

Something seems to be missing here. 这里似乎缺少一些东西。 The similarly named self.combined_scores_df is bound to a pandas.DataFrame in the line below. 类似的self.combined_scores_df绑定到下面一行中的pandas.DataFrame I believe, a line that created a pandas.DataFrame and bound it to self.combined_scores was erroneously deleted. 我相信,创建pandas.DataFrame并将其绑定到self.combined_scores被错误地删除了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM