判断np.array里面为空字符串的方法

#多在编译器里尝试新操作
import numpy as np
for i range(100):
  eval1 = {"A": ''"}
  eval2 = {"A": [[1], [2]]}
      if i%2 == 0:
      ar = np.array(eval1['A'])  #此时打印ar,里面什么都没有
      else:
      ar = np.array(eval2["A"])     #此时打印ar,是一个二维数组
    
  if ar.shape == ():  #不能用len来判断,array没有len用法;对于数组中有元素的,不能够使用if
    print("数组中什么都没有")

# if all(ar):#TypeError: iteration over a 0-d array
if ar.all(): # TypeError: cannot perform reduce with flexible type
print("ar为空")


# if not all(ar2):
if not ar2.all():
print("ar2为空")


新战场:https://blog.csdn.net/Stephen___Qin
原文地址:https://www.cnblogs.com/Stephen-Qin/p/10614026.html