【发布时间】:2020-10-18 14:46:27
【问题描述】:
我已经设法在上述数组中创建了一个综合列表,但是,如果不创建新的 for 循环,我无法只打印出每个子数组的最后一个元素。 有人可以解释一下如何组合这两个循环吗?
import numpy as np
b = np.array([[[0.55867166, 0.06210792, 0.08147297],
[0.82579068, 0.91512478, 0.06833034]],
[[0.05440634, 0.65857693, 0.30296619],
[0.06769833, 0.96031863, 0.51293743]],
[[0.09143215, 0.71893382, 0.45850679],
[0.58256464, 0.59005654, 0.56266457]],
[[0.71600294, 0.87392666, 0.11434044],
[0.8694668 , 0.65669313, 0.10708681]],
[[0.07529684, 0.46470767, 0.47984544],
[0.65368638, 0.14901286, 0.23760688]]])
list5 = [[[e for e in r if e <= 0.5] for r in s] for s in b]
print(list5)
for i in list5:
print((i[-1]))
【问题讨论】:
-
你是指
list5中列表的最后一个元素吗? -
我的目标是从 b 获取每个子数组中的最后一个值
标签: arrays python-3.x numpy list-comprehension