【问题标题】:Convert the dictionary with key and list of values to a matrix将带有键和值列表的字典转换为矩阵
【发布时间】:2019-04-16 16:04:33
【问题描述】:

如何将这样的字典 {1: [0, 1, 2], 2:[3, 4, 5]} 转换为矩阵 [[1, 0, 1, 2], [2, 3 , 4, 5]]???

【问题讨论】:

    标签: python python-3.x dictionary matrix


    【解决方案1】:

    遍历字典,并追加键和值,这是一个列表到一个更大的列表。

    dct =  {1: [0, 1, 2], 2:[3, 4, 5]}
    lst = [[key]+value for key,value in dct.items()]
    print(lst)
    #[[1, 0, 1, 2], [2, 3, 4, 5]]
    

    【讨论】:

      猜你喜欢
      • 2021-12-09
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      • 2020-05-11
      • 2021-10-11
      相关资源
      最近更新 更多