#多在編譯器里嘗試新操作 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為空")