首页 > 解决方案 > 将 numpy 数组转换为没有 zip 的列表列表

问题描述

我想将包含 2 个列表的数组转换为排名列表。目前我的代码产生:

[['txt1.txt' 'txt2.txt' 'txt3.txt' 'txt4.txt' 'txt5.txt' 'txt6.txt'
  'txt7.txt' 'txt8.txt']
 ['0.13794219565502694' '0.024652340886571225' '0.09806335128916213'
  '0.07663118536707426' '0.09118273488073968' '0.06278926571143634'
  '0.05114729750522118' '0.02961812647701087']]

我想让 txt1.txt 与第一个值一起使用,txt2 与第二个值一起使用,依此类推。

所以像这样

[['txt1.txt', '0.13794219565502694'], ['txt2.txt', '0.024652340886571225']... etc ]]

我不希望它通过使用 zip 变成元组。

我当前的代码:

def rankedmatrix():
    matrix = numpy.array([names,x])
    ranked_matrix = sorted(matrix.tolist(), key=lambda score: score[1], reverse=True)
    print(ranked_matrix)

名称为:names = ['txt1.txt', 'txt2.txt', 'txt3.txt', 'txt4.txt', 'txt5.txt', 'txt6.txt', 'txt7.txt', 'txt8 。文本']

x 是:

x = [0.1379422 0.01540234 0.09806335 0.07663119 0.09118273 0.06278927 0.0511473 0.02961813]

任何帮助表示赞赏。

标签: python

解决方案


您可以使用map将 转换tuplelist.

list(map(list, zip(names, x)))
[['txt1.txt', 0.1379422],
 ['txt2.txt', 0.01540234],
 ['txt3.txt', 0.09806335],
 ['txt4.txt', 0.07663119],
 ['txt5.txt', 0.09118273],
 ['txt6.txt', 0.06278927],
 ['txt7.txt', 0.0511473],
 ['txt8.txt', 0.02961813]]

推荐阅读