I have the following input file 'r1'
我有以下输入文件'r1'
14 14
15 15
I would like to create the following output file 'r2'.
我想创建以下输出文件'r2'。
14 14 less than 15
15 15 equal to 15
I am trying to do so using the following code.
我试图使用以下代码这样做。
import numpy as np
s=open('r1')
r=open('r2','w+')
r1=np.loadtxt(s)
atim=r1[:,[0]]
alat=r1[:,[1]]
if atim<15 and alat<15:
print >> r,atim,alat,'less than 15'
if atim==15 and alat==15:
print >> r,atim,alat,'equal to 15'
However, when I run the program I get the following error if atim<15 and alat<15: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
但是,当我运行程序时,如果atim <15并且alat <15,则会出现以下错误:ValueError:具有多个元素的数组的真值是不明确的。使用a.any()或a.all()
3 个解决方案
#1
1
You want to do a comparison like
你想做一个比较
all(i < 15 for i in r1[0])
all(i == 15 for i in r1[0])
so you could do:
所以你可以这样做:
for row in len(r1):
if all(i < 15 for i in r1[row]):
print >> r,r1[row][0], r1[row][1], 'less than 15'
if all(i == 15 for i in r1[row]):
print >> r,r1[row][0], r1[row][1], 'equal to 15'
#2
0
with numpy, it's pretty easy:
有numpy,很容易:
[(a < 15).all() for a in r1]
or
要么
[(a == 15).all() for a in r1]
#3
0
import numpy as np
r1 = np.array([[11, 15],
[15, 15],
[14, 14]])
equal_to_15 = (r1[:,0] == 15) & (r1[:,1] == 15)
less_than_15 = (r1[:,0] < 15) & (r1[:,1] < 15)
Result:
结果:
>>> equal_to_15
array([False, True, False], dtype=bool)
>>> less_than_15
array([False, False, True], dtype=bool)
Error Message:
错误信息:
When you compare an array to integer you get a boolean array.
将数组与整数进行比较时,会得到一个布尔数组。
>>> np.array([13, 15]) == 15
array([False, True], dtype=bool)
>>> if _:
... print 'Hi'
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
And numpy does not evaluate the whole array for truthness, but if we did:
并且numpy并不评估整个数组的真实性,但如果我们这样做:
>>> if (np.array([13, 15]) == 15).any():
... print 'Hi'
...
Hi
#1
1
You want to do a comparison like
你想做一个比较
all(i < 15 for i in r1[0])
all(i == 15 for i in r1[0])
so you could do:
所以你可以这样做:
for row in len(r1):
if all(i < 15 for i in r1[row]):
print >> r,r1[row][0], r1[row][1], 'less than 15'
if all(i == 15 for i in r1[row]):
print >> r,r1[row][0], r1[row][1], 'equal to 15'
#2
0
with numpy, it's pretty easy:
有numpy,很容易:
[(a < 15).all() for a in r1]
or
要么
[(a == 15).all() for a in r1]
#3
0
import numpy as np
r1 = np.array([[11, 15],
[15, 15],
[14, 14]])
equal_to_15 = (r1[:,0] == 15) & (r1[:,1] == 15)
less_than_15 = (r1[:,0] < 15) & (r1[:,1] < 15)
Result:
结果:
>>> equal_to_15
array([False, True, False], dtype=bool)
>>> less_than_15
array([False, False, True], dtype=bool)
Error Message:
错误信息:
When you compare an array to integer you get a boolean array.
将数组与整数进行比较时,会得到一个布尔数组。
>>> np.array([13, 15]) == 15
array([False, True], dtype=bool)
>>> if _:
... print 'Hi'
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
And numpy does not evaluate the whole array for truthness, but if we did:
并且numpy并不评估整个数组的真实性,但如果我们这样做:
>>> if (np.array([13, 15]) == 15).any():
... print 'Hi'
...
Hi