简体   繁体   English

在Python中使用zip映射2D数组的列表

[英]Mapping list with 2D arrays using zip in Python

I have the following labels and values and trying to get the Output below using the code: 我有以下标签和值,并尝试使用代码获取以下输出:

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)

Expected Output: 预期产量:

  a: 7963.92759169
  b: -2930.99452794
  c: 3362.47036823

But I don't get the expected output. 但是我没有得到预期的输出。 Could anyone help on what is going wrong with the zip function? 有人可以帮助您解决zip功能出什么问题吗?

Try the below code. 试试下面的代码。 Hope this would help. 希望这会有所帮助。 Normally Zip function will give this ouput. 通常,Zip函数将提供此输出。

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

values= [[ 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[0]))

Output will be: 输出将是:

a:7963.92759169
b:7964.28495515
c:7965.60367246

But if you want diagonal elements then you have use a counter or you can use enumerate. 但是,如果要使用对角线元素,则可以使用计数器,也可以使用枚举。

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

Ouput will be : Ouput将是:

a:7963.92759169
b:-2930.99452794
c:3362.47036823

Using enumerate 使用enumerate

Ex: 例如:

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]))

Output: 输出:

a:7963.92759169
b:-2930.99452794
c:3362.47036823

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM