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 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
) |
) |
>>> a = array([1,2,3]) >>> print a.type() numarray type: Int32 >>> 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,100) >>> a.tostring() >>> a.tostring() 'A\000\000\000B\000\000\000C\000\000\000D\000\000\000E\000\000\000F\000\000\000G\000\000\000H\000\000\000I\000\000\000J\000\000\000K\000\000\000L\000\000\000M\000\000\000N\000\000\000O\000\000\000P\000\000\000Q\000\000\000R\000\000\000S\000\000\000T\000\000\000U\000\000\000V\000\000\000W\000\000\000X\000\000\000Y\000\000\000Z\000\000\000[\000\000\000\\\000\000\000]\000\000\000^\000\000\000_\000\000\000`\000\000\000a\000\000\000b\000\000\000c\000\000\000'
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.