使用列表理解向数组列表的第一个索引添加值 Python
Adding a value to the first index of a list of array by using a list comprehension Python
下面的 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])]
只需在列表理解中添加。
Vals = np.array([[100] + arr[i:] for i,arr in enumerate(Values.tolist())])
这是我的看法,简单明了。
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)
下面的 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])]
只需在列表理解中添加。
Vals = np.array([[100] + arr[i:] for i,arr in enumerate(Values.tolist())])
这是我的看法,简单明了。
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)