我有以下清单
poles = [10, 15]
P_D_given_loc_i= np.zeros(40)
然后我会:
P_D_given_loc_i=[(( (i+1) in poles)) for i in range(40) ]
我几乎得到了我想要的。我得到了一个列表,其中在极点之前的所有位置都被标记为True,其余的都是False
事实证明这并不是我想要的。我想得到的是0.0而不是False,1.0而不是True
[ 0.0, 0.0 ,........1.0, 0.0.......]
我的问题是,如何通过在列表初始化中使用if
来实现这一点
更广泛的问题是,如何在列表初始化中使用条件句
有一种更简单的方法:
但是,如果您选择浮动,您最初的方法也会起作用:
但是,对于更大的阵列,这要慢得多
相关问题 更多 >
编程相关推荐