我正在尝试删除数组中的最后一个元素,如果该元素不符合某些条件。我正在使用的代码是:
# Set the distibution parameter to 2
a = 2
# Set the size to 100
s = 100
# Create Zipf's Law distribution using a and s
x = np.random.zipf(a,s)
# Reorder list by number frequency
xb = np.unique(x,return_counts=True)
print("X",x)
print("XB",xb)
for i in reversed(xb):
if xb[-1] > xb[-2]*1.5:
xb = np.delete(xb,-1)
print("XB mod",xb)
print()
我得到python print("X",x)
和``python print(“ XB”,xb)的以下输出:
XB(array([1,2,3,4,5,6,7,8,9,9,10,11,28, 29, 31,33,56,225]),array([57,17,4,4,2,1,2,2,2,1,1,1,1,1,1,1,1], dtype = int64))
但是,当我尝试运行代码的删除部分时,出现以下错误:
回溯(最近一次通话最后一次):文件“ test2.py”,第22行,在 如果xb [-1]> xb [-2] * 1.5:ValueError:包含多个元素的数组的真值不明确。使用a.any()或a.all()
有什么办法解决它,以便在不符合条件的情况下删除XB数组中的最后一个元素吗?