I am wondering why the results of these two pieces of code are different,and want to know how to use only for loop to achieve the same result as list comprehension:
a = []
c = []
n = np.array([1,2],dtype=np.float32)
m = np.array([1,3],dtype=np.float32)
b = np.array([3], dtype=np.float32)
a.append([n, m, b])
a.append([n, m, b])
a.append([n, m, b])
e = np.array([])
for obs,_,act in a:
g = [obs,act]
e = np.concatenate([e,g])
e = np.array([e])
f = np.array( [np.concatenate([obs,act]) for obs,_,act in a ])
print("using for loop:\n", e)
print("using list comprehension:\n", f)
The result is:
using for loop:
[[array([1., 2.], dtype=float32) array([3.], dtype=float32)
array([1., 2.], dtype=float32) array([3.], dtype=float32)
array([1., 2.], dtype=float32) array([3.], dtype=float32)]]
using list comprehension:
[[1. 2. 3.]
[1. 2. 3.]
[1. 2. 3.]]
why?and how to make the for loop to have the same result as list comprehension?