我正在尝试将一些代码从Python移植到R,并且遇到了我无法完全理解的列表理解。这是一个类似于代码的玩具示例
import numpy as np
theta = np.random.rand(5, 2, 2, 3)
thetai = theta[0]
logp = [theta[np.newaxis, ...] for theta in thetai]
如果运行并打印结果,则会得到:
print(logp)
[array([[[0.779, 0.461, 0.766],
[0.245, 0.189, 0.045]]]), array([[[0.229, 0.288, 0.173],
[0.011, 0.541, 0.528]]])]
Ok output is a list of two arrays. What I cannot understand is the for theta in thetai
clause. Why? Because theta
is a bigger array than thetai
. Theta
has shape (5,2,2,3) but thetai
has shape (2,2,3). So what is the list comprehension actually doing when the code says for biggerthing in smallerthing
???