请问python调取数组某一位时出现list indices must be integers or slices, not str如何处理?
问题描述:
customer 和 wei内全部为数字,抓取数字进行计算时报错如图。
wei数组用于记录n个项目的权重,customer第一位是用户权重,第二位是项目数,第三位是项目编号。目的是读到项目编号x然后在wei数组的第x位加上这个权重值。
答
list indices must be integers or slices
说得很清楚,下表索引必须是整数或者切片。
而你customer[i][j]是字符串
wei[int(customer[i][j])] = int(wei[int(customer[i][j])]) + int(customer[i][0])
答
你这个customer元素是str类型的,譬如‘36’、‘1’,应该加一个int转换,改为
wei[int(customer[i][j])] = wei[int(customer[i][j]) + int(customer[i][0])