首页 > 解决方案 > 在 Python 中使用 zip 映射具有 2D 数组的列表

问题描述

我有以下标签和值,并尝试使用代码获取以下输出:

lables: ['a','b','c']

values: array([[ 7963.92759169, -2931.3518914 ,  3360.79428745],
               [ 7964.28495515, -2930.99452794,  3361.15165092],
               [ 7965.60367246, -2929.67581063,  3362.47036823]])

for label, score in zip(lables,values):
   print("{}:{}".format(label,score)

预期输出:

  a: 7963.92759169
  b: -2930.99452794
  c: 3362.47036823

但我没有得到预期的输出。任何人都可以帮助解决 zip 功能出了什么问题吗?

标签: pythonzip

解决方案


使用enumerate

前任:

import numpy as np

lables = ['a','b','c']
values = np.array([[ 7963.92759169, -2931.3518914 ,  3360.79428745],
               [ 7964.28495515, -2930.99452794,  3361.15165092],
               [ 7965.60367246, -2929.67581063,  3362.47036823]])

for idx, (label, score) in enumerate(zip(lables,values)):
   print("{}:{}".format(label,score[idx]))

输出:

a:7963.92759169
b:-2930.99452794
c:3362.47036823

推荐阅读