As we discussed at the beginning of the last chapter, there are very few array methods for good reasons, and these all depend on the implementation details. They're worth knowing, though.
) |
>>> a = arange(10) >>> a.itemsize() 4 >>> a = array([1.0]) >>> a.itemsize() 8 >>> a = array([1], type=Complex64) >>> a.itemsize() 16
) |
>>> b = a[3:8:2] >>> print a.iscontiguous() 1 >>> print b.iscontiguous() 0
) |
>>> c = a[3:8:2].copy() >>> print c.iscontiguous() 1
) |
>>> a = array([1,2,3]) >>> a.type() Int32 >>> a = array([1], type=Complex64) >>> a.type() Complex64
) |
>>> print a [1 2 3] >>> a.byteswap() >>> print a [16777216 33554432 50331648]
) |
>>> a = arange(65,70) >>> a.tostring() 'A\x00\x00\x00B\x00\x00\x00C\x00\x00\x00D\x00\x00\x00E\x00\x00\x00'
file) |
>>> a = arange(65,100) >>> a.tofile('test.dat') # writes a's binary data to file 'test.dat'. >>> f = open('test2.dat', 'w') >>> a.tofile(f) # writes a's binary data to file 'test2.dat'
) |
>>> print a [[65 66 67 68 69 70 71] [72 73 74 75 76 77 78] [79 80 81 82 83 84 85] [86 87 88 89 90 91 92] [93 94 95 96 97 98 99]] >>> print a.tolist() [[65, 66, 67, 68, 69, 70, 71], [72, 73, 74, 75, 76, 77, 78], [79, 80, 81, 82, 83, 84, 85], [86, 87, 88, 89, 90, 91, 92], [93, 94, 95, 96, 97, 98, 99]]
) |
) |
>>> a = arange(12) >>> a.setshape((3,4)) >>> print a.getshape() (3, 4) >>> print a [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]
) |
>>> print a [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] >>> print a.getflat() [ 0 1 2 3 4 5 6 7 8 9 10 11]
) |
) |
) |
) |
) |
) |
) |
>>> arange(10).sum() 45
) |
>>> arange(10).mean() 4.5
) |
>>> arange(10).min() 0
) |
>>> arange(10).max() 9
Send comments to the NumArray community.