{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "**array.typecode**
\n", "The typecode character used to create the array." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from array import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'i'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# How to get the type code of an array?\n", "array_num = array('i', [1,3,5,7,9])\n", "array_num.typecode" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.itemsize**
\n", "The length in bytes of one array item in the internal representation." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "array_num = array('i', [1,3,5,7,9,10,15])\n", "array_num.itemsize" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.append(x)**
\n", "Append a new item with value x to the end of the array." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array('i', [1, 3, 5, 7, 9, 10, 15, 100])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# How to append a value to the end of an array?\n", "array_num = array('i', [1,3,5,7,9,10,15])\n", "array_num.append(100)\n", "array_num" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.buffer_info()**
\n", "Return a tuple (address, length) giving the current memory address and the length in elements of the buffer used
\n", "to hold array’s contents." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(81782832, 7)" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# How to get the memory address and number of elements in an array?\n", "array_num = array('i', [1,3,5,7,9,10,15])\n", "array_num.buffer_info()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.byteswap()**
\n", "“Byteswap” all items of the array. It is useful when reading data from a file written on a machine
\n", "with a different byte order. This is only supported for values which are 1, 2, 4, or 8 bytes in size.
\n", "For other types of values, RuntimeError is raised." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "array_num = array('i', [1,3,5,7,9,10,15])\n", "array_num.byteswap()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.count(x)**
\n", "Return the number of occurrences of x in the array." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# How to count the number of occurrences of an element in an array?\n", "array_num = array('i', [1,3,5,7,9,10,15,10])\n", "array_num.count(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.extend(iterable)**
\n", "Append items from iterable to the end of the array. If iterable is another array, it must have exactly the same
\n", "type code; if not, TypeError will be raised." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original array: array('i', [1, 3, 5, 7, 9])\n", "Extended array: array('i', [1, 3, 5, 7, 9, 1, 3, 5, 7, 9])\n" ] } ], "source": [ "# How to extend an array with values from a list?\n", "from array import *\n", "array_num = array('i', [1, 3, 5, 7, 9])\n", "print(\"Original array: \"+str(array_num))\n", "array_num.extend(array_num)\n", "print(\"Extended array: \"+str(array_num))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.frombytes(s)**
\n", "Appends items from the string, interpreting the string as an array of machine values." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a1: array('i')\n" ] } ], "source": [ "a1 = array('i')\n", "as_bytes = a1.tobytes()\n", "a1.frombytes(as_bytes)\n", "print('a1:', a1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.fromfile(f, n)**
\n", "Read n items (as machine values) from the file object f and append them to the end of the array.
\n", "If less than n items are available, EOFError is raised, but the items that were available are still inserted
\n", "into the array. f must be a real built-in file object; something else with a read() method won’t do." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array('i', [1, 2, 3, 4])\n" ] } ], "source": [ "# How to read and write arrays in a file?\n", "f = open(\"array.bin\", \"wb\")\n", "array(\"i\", [1, 2, 3, 4]).tofile(f)\n", "f.close()\n", "\n", "nums = array(\"i\")\n", "f = open(\"array.bin\", \"rb\")\n", "nums.fromfile(f, 4)\n", "print(nums)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.fromlist(list)**
\n", "Append items from the list. This is equivalent to for x in list: a.append(x) except that if there is a type error,
\n", "the array is unchanged." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array('i', [1, 2, 3, 4, 3, 4])\n" ] } ], "source": [ "# How to add values from a list to an array?\n", "ints = array(\"i\", [1, 2])\n", "nums.fromlist([3, 4])\n", "print(nums)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.fromunicode(s)**
\n", "Extends this array with data from the given unicode string. The array must be a type 'u' array; otherwise
\n", "a ValueError is raised. Use array.frombytes(unicodestring.encode(enc)) to append Unicode data to an array of some
\n", "other type." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array('u', 'abcdefxyz')\n" ] } ], "source": [ "# How to append a unicode string to an array?\n", "unicodes = array(\"u\", u\"abcdef\")\n", "unicodes.fromunicode(u\"xyz\")\n", "print(unicodes)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.index(x)**
\n", "Return the smallest i such that i is the index of the first occurrence of x in the array." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "4\n" ] } ], "source": [ "# How to find the index of the first occurrence of a value in an array?\n", "nums = array(\"i\", [1, 2, 1, 3, 20])\n", "print(nums.index(1))\n", "print(nums.index(2))\n", "print(nums.index(20))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.insert(i, x)**
\n", "Insert a new item with value x in the array before position i. Negative values are treated as being relative
\n", "to the end of the array." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array('i', [1, 2, 3, 4])\n" ] } ], "source": [ "# How to insert a value into an array?\n", "nums = array(\"i\", [1, 2, 4])\n", "nums.insert(2, 3)\n", "print( nums)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.pop([i])**
\n", "Removes the item with the index i from the array and returns it. The optional argument defaults to -1,
\n", "so that by default the last item is removed and returned." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# How to pop elements off of an array?\n", "array_num = array('i', [1,3,5,7,9,10,15,10])\n", "array_num.pop(4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.remove(x)**
\n", "Remove the first occurrence of x from the array." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# How to remove the first occurrence of an element?\n", "array_num = array('i', [1,3,5,7,9,10,15,10])\n", "array_num.pop(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.reverse()**
\n", "Reverse the order of the items in the array." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array('i', [10, 15, 10, 9, 7, 5, 3, 1])" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# How to reverse the items in an array?\n", "array_num = array('i', [1,3,5,7,9,10,15,10])\n", "array_num.reverse()\n", "array_num" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.tobytes()**
\n", "Convert the array to an array of machine values and return the bytes representation (the same sequence of bytes
\n", "that would be written to a file by the tofile() method.)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "b'\\x00\\x00\\x01\\x00\\x02\\x00\\x03\\x00'" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "array_num = np.array([[0, 1], [2, 3]], dtype='\n", "Write all items (as machine values) to the file object f." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array('i', [1, 2, 3])\n" ] } ], "source": [ "# How to read and write arrays in a file?\n", "import array\n", "f = open(\"array.bin\", \"wb\")\n", "array.array(\"i\", [1, 2, 3, 4]).tofile(f)\n", "f.close()\n", "\n", "ints = array.array(\"i\")\n", "f = open(\"array.bin\", \"rb\")\n", "ints.fromfile(f, 3)\n", "print(ints)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.tolist()**
\n", "Convert the array to an ordinary list with the same items." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Array to list = [1, 2, 3, 4, 5]\n" ] } ], "source": [ "# How to convert an array into a list?\n", "import numpy \n", "array_num = numpy.array([1, 2, 3, 4, 5]) \n", "print(\"Array to list = \", array_num.tolist())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**array.tounicode()**
\n", "Convert the array to a unicode string. The array must be a type 'u' array; otherwise a ValueError is raised.
\n", "Use array.tobytes().decode(enc) to obtain a unicode string from an array of some other type." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hello'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# How to convert a unicode array into a unicode string?\n", "array_str = array.array('u','Hello')\n", "array_str.tounicode()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }