拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 使用串列推导Python将值添加到阵列串列的第一个索引

使用串列推导Python将值添加到阵列串列的第一个索引

白鹭 - 2022-02-13 2110 0 0

下面的 Vals 串列理解进行了修改Values,使得对于第 n 行它索引阵列值。我如何能够在Vals串列理解中添加一个增量,它在所有修改后的串列前面添加 100?我只想修改串列理解函式来做到这一点。

import numpy as np 

first_index_val = 100
Values = np.array([[130,123,135.3,139.05,156.08,163.88,173.72],
                  [130,123,135.3,139.05,156.08,163.88,173.72],
                  [130,123,135.3,139.05,156.08,163.88,173.72],
                  [130,123,135.3,139.05,156.08,163.88,173.72],
                  [130,123,135.3,139.05,156.08,163.88,173.72],
                  [130,123,135.3,139.05,156.08,163.88,173.72],
                  [130,123,135.3,139.05,156.08,163.88,173.72]])

Vals = np.array([arr[i:] for i,arr in enumerate(Values.tolist())])

输出:

[list([130.0, 123.0, 135.3, 139.05, 156.08, 163.88, 173.72])
 list([123.0, 135.3, 139.05, 156.08, 163.88, 173.72])
 list([135.3, 139.05, 156.08, 163.88, 173.72])
 list([139.05, 156.08, 163.88, 173.72]) list([156.08, 163.88, 173.72])
 list([163.88, 173.72]) list([173.72])]

预期输出:

[list([100, 130.0, 123.0, 135.3, 139.05, 156.08, 163.88, 173.72])
 list([100, 123.0, 135.3, 139.05, 156.08, 163.88, 173.72])
 list([100, 135.3, 139.05, 156.08, 163.88, 173.72])
 list([100, 139.05, 156.08, 163.88, 173.72]) list([100, 156.08, 163.88, 173.72])
 list([100, 163.88, 173.72]) list([100, 173.72])]

uj5u.com热心网友回复:

这是我的看法,简单而明确。

import numpy as np

first_index_val = 100
values = np.array([[130, 123, 135.3, 139.05, 156.08, 163.88, 173.72],
               [130, 123, 135.3, 139.05, 156.08, 163.88, 173.72],
               [130, 123, 135.3, 139.05, 156.08, 163.88, 173.72],
               [130, 123, 135.3, 139.05, 156.08, 163.88, 173.72],
               [130, 123, 135.3, 139.05, 156.08, 163.88, 173.72],
               [130, 123, 135.3, 139.05, 156.08, 163.88, 173.72],
               [130, 123, 135.3, 139.05, 156.08, 163.88, 173.72]])

values = np.array([ [100]   arr[i:] for i, arr in enumerate(values.tolist())])
print(values)

uj5u.com热心网友回复:

只需添加到串列理解中。

Vals = np.array([[100]   arr[i:] for i,arr in enumerate(Values.tolist())])
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *