python - 分割不可散列类型的字符串

标签 python python-3.x

我以前从未在 Python 中拆分过字符串,所以我不太确定这里出了什么问题。

import pyowm

owm = pyowm.OWM('####################')

location = owm.weather_at_place('Leicester, uk')
weather = location.get_weather()
weather.get_temperature('celsius')
temperature = weather.get_temperature('celsius')

print(temperature[5:10])

收到错误

sudo python weather.py
Traceback (most recent call last):
File "weather.py", line 10, in <module>
print(temperature[5:10])
TypeError: unhashable type

最佳答案

get_Temperature 返回一个字典,然后您尝试使用不可散列的 slice 对象对其进行索引。例如

>>> hash(slice(5, 10))                                                                         
Traceback (most recent call last):                                                             
  File "<stdin>", line 1, in <module>                                                          
TypeError: unhashable type

要获取温度,您需要像这样从字典中获取:

temperature['temp']

关于python - 分割不可散列类型的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40064881/

相关文章:

python - 在表格的每一行旁边添加按钮

python - 具有可变数字参数的 Python 中的字符串格式

python - Python中矩阵元素的双重求和

python - 如何将图例添加到 matplotlib 事件图中?

python - Python 中多行变成单行

Python模块搜索路径

python - float 子类以改变摄入量和 __str__ 行为

python - 如何从 4 个 csv 文件中随机抽样,以便在 Python 中每个 csv 文件中按顺序出现不超过 2/3 行

python - 为什么在 Python 中的子字符串 "not completely equivalent to slicing the string"中进行正则表达式搜索?

python - Pandas DataFrame.drop_duplicates() 缺少一些东西?