How to create a numpy array of all True or all False?

前端 未结 7 2073
醉话见心
醉话见心 2020-12-07 09:24

In Python, how do I create a numpy array of arbitrary shape filled with all True or all False?

7条回答
  •  囚心锁ツ
    2020-12-07 09:37

    benchmark for Michael Currie's answer

    import perfplot
    
    bench_x = perfplot.bench(
        n_range= range(1, 200),
        setup  = lambda n: (n, n),
        kernels= [
            lambda shape: np.ones(shape, dtype= bool),
            lambda shape: np.full(shape, True)
        ],
        labels = ['ones', 'full']
    )
    
    bench_x.show()
    

提交回复
热议问题