ValueError: need more than 1 value to unpack how can i fix it

ValueError: need more than 1 value to unpack how can i fix it

我正在尝试在 python (2.7) 中绘图,但我收到此 ValueError:需要超过 1 个值才能解包。

我的代码如下所示:

x, y = zip(*list_with_data) 
xlocs = np.arange(len(x))

fig = plt.figure()
ax = fig.gca()
ax.bar(xlocs + 0.6, y)
ax.set_xticks(xlocs + 1)
ax.set_xticklabels(x)
ax.set_xlim(0.0, xlocs.max() + 2)

plt.show()

我的

list_with_data = Counter(numbers(text))

我希望 x-label 是文本包含的数字:

1,2,3,4,5,6...

并且我希望我的 y 标签表示:它们在文本中出现了多少次。

示例:

x: 1, y: 150
x: 2, y: 20

如何从我的数据集中绘制此图?

您只压缩了字典键的键,因此您只有一个值,因此出现解包错误:

In [12]: list_with_data = Counter("1 2 3 2 3 4 5 6")

In [13]: zip(*list_with_data)
Out[13]: [(' ', '1', '3', '2', '5', '4', '6')] 

如果要解压两个值,则需要两个值:

In [14]: x, y = zip(*list_with_data.items())  
In [15]: x
Out[15]: (' ', '1', '3', '2', '5', '4', '6')

In [16]: y
Out[16]: (7, 1, 2, 2, 1, 1, 1