【问题标题】:Convert dictionary to array in Python 3在 Python 3 中将字典转换为数组
【发布时间】:2020-10-25 01:49:50
【问题描述】:

我有以下字典

{
'0':'test1'
'1':'test2'
'2':'test3'
'3':'test4'
}

我想将它转换成一个数组,如您所见,其中的索引key valuesvalues。 所以,基本上,我想要['test1', 'test2', 'test3', 'test4']

another question like this,我读过,但它没有解决我的问题,因为我想要字典的键 作为指数

【问题讨论】:

  • 如果键是例如 1-2-5-7 怎么办?结果数组应该有多大?

标签: python


【解决方案1】:

如果字典名称为d,则为list(d.values())

如果您知道键是数字 0-N,并且您希望结果数组按该顺序排列,请执行以下操作:

[x[1] for x in sorted(d.items())]

【讨论】:

  • 这将是任意顺序,而 OP 希望将键用作列表索引。
  • @interjay。我最初并没有那样阅读这个问题,但你可能是对的。无论如何,编辑我的答案。
猜你喜欢
  • 2020-10-03
  • 2017-07-21
  • 1970-01-01
  • 2018-05-04
  • 2014-02-19
  • 1970-01-01
  • 2019-04-19
相关资源
最近更新 更多