Python 为什么我会收到一封“信”;ValueError“;这个节目的消息?

Python 为什么我会收到一封“信”;ValueError“;这个节目的消息?,python,matplotlib,Python,Matplotlib,编辑(2017年3月12日[9:11 CST])-因此我能够确定为什么数字是235-235是指定进入数组的字符串中的字符总数,至少是一个温度。现在,我的问题是,为什么要计算字符而不是字符串的总数 我正在编写一个程序,从地下天气中提取实时数据,将其排序为数组,然后最终输出完整的图形和文本报告 目前,我很难将数据转换为可以使用模块matplotlib生成图形的格式。完整的代码和它的模块“计算”在这篇文章的底部 错误消息如下所示: Traceback (most recent call last):

编辑(2017年3月12日[9:11 CST])-因此我能够确定为什么数字是235-235是指定进入数组的字符串中的字符总数,至少是一个温度。现在,我的问题是,为什么要计算字符而不是字符串的总数


我正在编写一个程序,从地下天气中提取实时数据,将其排序为数组,然后最终输出完整的图形和文本报告

目前,我很难将数据转换为可以使用模块matplotlib生成图形的格式。完整的代码和它的模块“计算”在这篇文章的底部

错误消息如下所示:

Traceback (most recent call last):
  File "C:/Users/Ryan/PycharmProjects/NWS/weather_data.py", line 254, in <module>
    temperatures_array = np.array(temp).reshape(10, 2)
ValueError: cannot reshape array of size 235 into shape (10,2)
“计算”模块:


提前感谢所有帮助您的人

问题似乎是您在while循环中使用了
extend
,而不是
append

我没有运行您的所有代码,但我确信您的问题来自
while
循环:

while x > 0:
    temp.extend(areas[y])
    temp.extend(temperatures[y])
    y += 1
    x -= 1
如果您在while循环的每次迭代中打印出temp的样子,您将看到
extend
正在将每个字符串拆分为一个字符列表(因为
extend
的作用是将一个列表合并到另一个列表中):

您的代码应该是这样的per-while循环:

while x > 0:
    temp.append(areas[y])
    temp.append(temperatures[y])
    y += 1
    x -= 1
Ideone又为我工作了。下面是我用来查看结果的代码片段:

作为一个小提示:

通过使用for循环而不是while循环,可以使事情更容易完成:

temp = []
for i in range(len(areas)):
    # append the area, then the temperature
    temp.append(areas[i])
    temp.append(temperatures[i])

您还可以对代码进行一些轻微的优化,尽管我不确定此处的注释是否与原始帖子偏离太多(或者应该保留给其他StackExchange站点或帖子)。

没问题。是否需要区域/温度代码片段的优化版本?另外,如果我的解决方案解决了您的问题,请将其标记为答案。这样,其他任何人都可以很容易地指出解决方案。
['A', 'u', 's', 't', 'i', 'n', ',', ' ', 'T', 'e', 'x', 'a', 's', '1', '2', '3', '4', ' ', 'F']

['A', 'u', 's', 't', 'i', 'n', ',', ' ', 'T', 'e', 'x', 'a', 's', '1', '2', '3', '4', ' ', 'F', 'H', 'o', 'n', 'o', 'l', 'u', 'l', 'u', ',', ' ', 'H', 'a', 'w', 'a', 'i', 'i', '1', '2', '3', '4', ' ', 'F']

... and so on, until you have 235 elements.
while x > 0:
    temp.append(areas[y])
    temp.append(temperatures[y])
    y += 1
    x -= 1
temp = []
for i in range(len(areas)):
    # append the area, then the temperature
    temp.append(areas[i])
    temp.append(temperatures[i])