Getting IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
Getting IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
我在 运行 使用此功能时遇到此索引错误。此函数查找城市邮政编码的平均租金价格。我有一个名为 city 的城市字典,以邮政编码为键,以城市名称为值。一些城市有多个邮政编码,arrRent 是一个包含城市房屋租金列表的数组,我想找到平均租金价格。
def meanPrice(self, city):
total = 0
cityLoc = 0
for keys, cities in self.city.items():
if self.city[keys] == city:
for i in self.arrRent[cityLoc]:
total += int(self.arrRent[cityLoc][i])
mean = total / i
print(total / i)
else:
cityLoc += 1
这是字典的一个片段:
{'95129': 'San Jose'}
{'95128': 'San Jose'}
{'95054': 'Santa Clara'}
{'95051': 'Santa Clara'}
{'95050': 'Santa Clara'}
这是数组的一个片段:
[['2659' '2623.5' '2749.5' '2826.5' '2775' '2795' '2810' '2845' '2827'
'2847' '2854' '2897.5' '2905' '2925' '2902.5' '2869.5']
['3342.5' '3386' '3385' '3353' '3300' '3190' '3087.5' '3092' '3170'
'3225' '3340' '3315' '3396' '3470' '3480' '3380']
['2996' '2989' '2953' '2950' '2884.5' '2829' '2785' '2908' '2850' '2761'
'2997.5' '3020' '2952' '2997.5' '2952' '2923.5']
['2804.5' '2850.5' '2850' '2850' '2867' '2940' '2905' '2945' '2938'
'2860' '2884' '2946' '2938' '2986.5' '2931.5' '3032.5']
['2800' '3074' '2950' '2850' '2850' '2875' '2757' '2716' '2738.5' '2696'
'2809' '2891' '3000' '2960' '2950' '2831']]
我看到 2 个问题:
- 你的列表中的问题'arrRent':它应该是一个包含租金的列表列表。但是不同的租金在你的列表中没有用逗号分隔:
[['2659' '2623.5' '2749.5' '2826.5' '2775' '2795' '2810' '2845' '2827'
'2847' '2854' '2897.5' '2905' '2925' '2902.5' '2869.5']
['3342.5' '3386' '3385' '3353' '3300' .......
- 您的代码问题似乎出在这段代码中。我这里是实际租金,不是指数:
for i in self.arrRent[cityLoc]:
total += int(self.arrRent[cityLoc][i])
改成这样:
for i in self.arrRent[cityLoc]:
total += int(i)
我在 运行 使用此功能时遇到此索引错误。此函数查找城市邮政编码的平均租金价格。我有一个名为 city 的城市字典,以邮政编码为键,以城市名称为值。一些城市有多个邮政编码,arrRent 是一个包含城市房屋租金列表的数组,我想找到平均租金价格。
def meanPrice(self, city):
total = 0
cityLoc = 0
for keys, cities in self.city.items():
if self.city[keys] == city:
for i in self.arrRent[cityLoc]:
total += int(self.arrRent[cityLoc][i])
mean = total / i
print(total / i)
else:
cityLoc += 1
这是字典的一个片段:
{'95129': 'San Jose'}
{'95128': 'San Jose'}
{'95054': 'Santa Clara'}
{'95051': 'Santa Clara'}
{'95050': 'Santa Clara'}
这是数组的一个片段:
[['2659' '2623.5' '2749.5' '2826.5' '2775' '2795' '2810' '2845' '2827'
'2847' '2854' '2897.5' '2905' '2925' '2902.5' '2869.5']
['3342.5' '3386' '3385' '3353' '3300' '3190' '3087.5' '3092' '3170'
'3225' '3340' '3315' '3396' '3470' '3480' '3380']
['2996' '2989' '2953' '2950' '2884.5' '2829' '2785' '2908' '2850' '2761'
'2997.5' '3020' '2952' '2997.5' '2952' '2923.5']
['2804.5' '2850.5' '2850' '2850' '2867' '2940' '2905' '2945' '2938'
'2860' '2884' '2946' '2938' '2986.5' '2931.5' '3032.5']
['2800' '3074' '2950' '2850' '2850' '2875' '2757' '2716' '2738.5' '2696'
'2809' '2891' '3000' '2960' '2950' '2831']]
我看到 2 个问题:
- 你的列表中的问题'arrRent':它应该是一个包含租金的列表列表。但是不同的租金在你的列表中没有用逗号分隔:
[['2659' '2623.5' '2749.5' '2826.5' '2775' '2795' '2810' '2845' '2827'
'2847' '2854' '2897.5' '2905' '2925' '2902.5' '2869.5']
['3342.5' '3386' '3385' '3353' '3300' .......
- 您的代码问题似乎出在这段代码中。我这里是实际租金,不是指数:
for i in self.arrRent[cityLoc]:
total += int(self.arrRent[cityLoc][i])
改成这样:
for i in self.arrRent[cityLoc]:
total += int(i)