代码之家  ›  专栏  ›  技术社区  ›  salvationishere

Python类型错误:str、int、float

  •  0
  • salvationishere  · 技术社区  · 7 年前

    我得到了

    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]
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Eike    7 年前

    self.combined_scores 可能是 float .我在您的代码中看到以下几行:

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

    这些名字表明 self.dist_weight sum_weights 很可能两者都是 浮动 。除名结果将为 浮动

    编辑:

    最后一行只能在以下情况下工作 自己综合得分 是多维容器。A. pandas.DataFrame 可能

    我认为错误在于:

    self.combined_scores = normalized_dist_weight
    

    这里好像少了点什么。名称相似的 self.combined_scores_df 绑定到 熊猫。数据帧 在下面的行中。我相信,这一行创造了 熊猫。数据帧 并将其绑定到 自己综合得分 被错误删除。