Array conversion tolist

The contents of self as a nested list.

>>> a = array([[1,2,3],[4,5,6]]); print a.tolist() [[1, 2, 3], [4, 5, 6]]

If no arguments are passed in, then this method only works for arrays with one element (a.size == 1). In this case, it returns a standard Python scalar object (if possible) copied from the first element of self. When the data type of self is longdouble or clongdouble, this returns a scalar array object because there is no available Python scalar that would not lose information. Void arrays return a buffer object for item() unless fields are defined in which case a tuple is returned.

>>> asc = a[0,0],item() >>> type(asc) <type ' int' > >>> asc

>>> type(a[0,0]) <type 'numpy.int32'>

If arguments are provided, then they indicate indices into the array (either a flat index or an nd-index). A standard Python scalar corresponding to the item at the given location is then returned. This is very similar to self[args] except instead of an array scalar, a standard Python scalar is returned. This can be useful for speeding up access to elements of the array and doing arithmetic on elements of the array using Python's optimized math.

itemset (*args)

There must be at least 1 argument and define the last argument as item. Then, this is equivalent to but faster than self[args] = item. The item should be a scalar value and args must select a single item in the array.

tostring (order='C')

A Python string showing a copy of the raw contents of data memory. The string can be produced in either 'C' or 'Fortran', or 'Any' order (the default is 'C'-order). 'Any' order means C-order unless the F_CONTIGUOUS flag in the array is set, then 'Fortran' order.

Write the contents of self to the open file object. If file is a string, then open a file of that name first. If sep is the empty string, then write the file in binary mode. If sep is any other string, write the array in simple text mode separating each element with the value of the sep string. When the file is written in text mode, the format string can be used to alter the appearance of each entry. If format is the empty string, then it is equivalent to ,l%s''. Each element of the array will be converted to a Python scalar, o, and written to the file as 1 1 format' ' % o. Note that writing an array to a file does not store any information about the shape, type, or endianness of an array. When written in binary mode, tofile is functionally equivalent to fid. write (self . tostring ()) .

>>> a.tofile('myfile.txt',sep=':',format='%03d') Contents of myfile.txt 001:002 :0 03 :004 :005:006

dump (file)

Pickle the contents of self to the file object represented by file. Equivalent to cPickle.dump(self, file, 2)

Return pickled representation of self as a string. Equivalent to cPickle.dumps(self,

Force conversion of this array to an array with the data type provided as the argument. If the argument is None, or equal to the data type of self, then return a copy of the array.

byteswap ({False})

Byteswap the elements of the array and return the byteswapped array. If the argument is True, then byteswap in-place and return a reference to self. Otherwise, return a copy of the array with the elements byteswapped. The data-type descriptor is not changed so the array will have changed numbers.

Return a copy of the array (which is always single-segment, and ALIGNED). However, the data-type is preserved (including whether or not the data is byteswapped).

Return a new array using the same memory area as self. If the optional argument is given, it can be either a typeobject that is a sub-type of the ndarray or an object that can be converted to a data-type descriptor. If the argument is a typeobject then a new array of that Python type is returned that uses the information from self. If the argument is a data-type descriptor, then a new array of the same Python type as self is returned using the given data-type.

>>> print a.view(single)

[[ 1.40129846e-45 2.80259693e-45

4,20389539e-45]

[ 5.60519386e-45 7.00649232e-45

8,40779079e-45]]

>>> a.view(ubyte)

array([[1, 0, 0, 0, 2, 0, 0, 0, 3, 0,

0, 0] ,

[4, 0, 0, 0, 5, 0, 0, 0, 6, 0, 0, 0]]

, dtype-uint8)

getfield (dtype=, offset=0)

Return a field of the given array as an array of the given data type. A field is a view of the array's data at a certain byte offset interpreted as a given data type. The returned array is a reference into self, therefore changes made to the returned array will be reflected in self. This method is particularly useful for record arrays that use a void data type, but it can also be used to extract the low (high)-order bytes of other array types as well. For example, using getfield, you could extract fixed-length substrings from an array of strings.

>>> a = array(['Hello','World','NumPy']) >>> a.getfield('S2',1) array(['el', 'or', 'um'], dtype='|S2')

setflags (write=None, align=None, uic=None)

Set array flags WRITEABLE, ALIGNED, and UPDATEIFCOPY, respectively. The ALIGNED flag can only be set to True if the data is actually aligned according to the type. The UPDATEIFCOPY flag can never be set to True. The flag WRITEABLE can only be set True if the array owns its own memory or the ultimate owner of the memory exposes a writeable buffer interface (or is a string). The exception for string is made so that unpickling can be done without copying memory.

fill (scalar)

Fill an array with the scalar value (appropriately converted to the type of self). If the scalar value is an array or a sequence, then only the first element is used. This method is usually faster than a[...]=scalar or self.flat=scalar, and always interprets its argument as a scalar.

Table 3.2: Array conversion methods

Method

Arguments

Description

as type

(dtype {None})

Cast to another data type

byteswap

(inplace {False})

Byteswap array elements

copy

0

Copy array

dump

(file)

Pickle to stream or file

dumps

0

Get pickled string

fill

(scalar)

Fill an array with scalar value

getfield

(dtype=, offset=0)

Return a field of the array

setflags

(write=None, align=None, uic=None)

Set array flags

tofile

(file=, sep=", format=")

Raw write to file

tolist

0

Array as a nested list

item

(*args)

Python scalar extraction

itemset

(*args)

Insert scalar (last argument) into array

tostring

(order='C)

String of raw memory

view

(obj)

View as another data type or class

Was this article helpful?

0 0

Post a comment