python - 在 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 功能出了什么问题吗?
解决方案
使用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
推荐阅读
- reactjs - 预检响应中的 Access-Control-Allow-Headers 不允许 Sails.js 请求标头字段授权
- webpack - Webpack 4 splitChunks all + multi-configuration 导致无法在 ./webpack/bootstrap 读取未定义的属性“调用”
- angular - 尝试使用 Angular TestBed 时出现奇怪的 RxJS 错误
- laravel - Intervention\Image\Exception\NotWritableException: Can't write image data to path Image\Image.php on line 143
- visual-studio - 即使已登录,Visual Studio 也会不断要求选择 Azure 帐户
- elixir - 在并行长生不老药中运行多个进程
- css - 第一行具有右对齐静态项目的 Flex 项目
- python - 如何过滤和替换 Pandas df 中给定列中的数据?
- json - 使用 T-SQL 解析 JSON
- python - 保存类型的对象
到磁盘