Python:如何从字典中创建两个列表

标签 python

我有一本字典。

{1 : [1.2, 2.3, 4.9, 2.0],  2 : [4.1, 5.1, 6.3],  3 : [4.9, 6.8, 9.5, 1.1, 7.1]}

我想将每个键值对作为两个列表传递给 matplotlib.pyplot 的实例:x 值和 y 值。

每个键都是与其值中的每个项目关联的 x 值。

所以我想要每个键的两个列表:

[1,1,1,1] [1.2,2.3,4.9,2.0]

[2,2,2] [4.1,5.1,6.3]

[3,3,3,3,3] [4.9,6.8,9.5,1.1,7.1]

有没有一种优雅的方式来做到这一点?

或者也许有一种方法可以将字典传递给 matplotlib.pyplot

最佳答案

for k, v in dictionary.iteritems():
    x = [k] * len(v)
    y = v
    pyplot.plot(x, y)

关于Python:如何从字典中创建两个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1738392/

相关文章:

python - 使用 Python,如何获取我的 Google protobuf 消息的二进制序列化?

python - py.test : How to generate tests from fixtures

python - 手指宽度(计算机视觉)

python - 移动项目时,QStandardItem 无法正确克隆

python - Python 中不常见的 OOP 的原因?

python - 实验确定矩阵行列式的计算复杂度

python - 简化嵌套 for 循环

python - 使用 openpyxl 获取特定单元格的值

python - Beautifulsoup - 当链接也在容器中时,findAll 找不到字符串

python - 由于数据库连接,无法在 Django 1.10.1 中运行服务器