Simple Python Array Programs
Simple Python Array Programs
To count occurrences of a specific element in an array, use the `count()` method. For example, `arr = [1, 2, 3, 2, 4]`, to count `2`s, call `arr.count(2)`, resulting in `2` as the count because the number `2` appears twice in the array .
To find the largest element in an array in Python, you can use the built-in `max()` function. For example, given an array `arr = array('i', [2, 3, 1, 5, 4])`, you can determine the largest element by calling `max(arr)`, which would return `5`. This function compares each element in the array and returns the largest one .
To reverse the order of items in an array, apply the `reverse()` method or list slicing. For `arr = [1, 2, 3]`, use `arr.reverse()` to get `[3, 2, 1]`. Alternatively, slicing with `arr = arr[::-1]` achieves the same result, rearranging elements in reverse order .
To find the remainder of the multiplication of elements in an array divided by a number `n`, iterate through the array to calculate the product, then use the modulus operator with `n`. For example, for `arr = [1, 2, 3]` and `n = 3`, compute the product `product = 1 * 2 * 3 = 6`, and then `remainder = product % n`, which gives `remainder = 6 % 3 = 0` .
To reconstruct an array replacing each element with `(arr[i-1] + 1) % M`, iterate through each element, applying the transformation. Assume `arr = [3, 5, 9]` and `M = 10`; create a new list where `new_arr[i] = (arr[i-1] + 1) % M`. Start `new_arr[0] = arr[0]`. Continue with the transformation: `new_arr = [3, 4, 6]` applying the formula successively on each element .
To append items from an iterable to an array in Python, use the `extend()` method. For example, if `arr = [1, 2, 3]` and you have an iterable `more_items = [4, 5]`, use `arr.extend(more_items)` to produce `arr = [1, 2, 3, 4, 5]`. This method appends each item from the iterable to the end of the array .
To append an item to the end of an array, use the `append()` method. If `arr = [1, 2, 3]` and you append `4` using `arr.append(4)`, it becomes `[1, 2, 3, 4]`. This increases the total number of elements by one, shifting the last index from 2 to 3, and the newly appended item takes this new last index position .
To split an array and move the first part to the end, you can use slicing. For instance, consider `arr = [1, 2, 3, 4, 5]` and you want to split and reposition after index `k`. Applying slices, you can arrange it as `arr = arr[k:] + arr[:k]`, thus splitting after the third element: `k=3`, resulting in `arr = [4, 5, 1, 2, 3]`. This operation divides the array at the specified index and reorders it .
To remove the first occurrence of a specified element from an array, you can use the `remove()` method. For instance, if you have `arr = [1, 2, 3, 2, 4]` and want to remove the first `2`, call `arr.remove(2)`. This will modify `arr` to `[1, 3, 2, 4]`, removing the first `2` it encounters .
To convert an array to a list in Python, you can use the `list()` function. For example, if you have an array `arr = array('i', [1, 2, 3, 4])`, you can convert it to a list using `lst = list(arr)`. This will produce a list `lst` containing the same elements in the same order as the array.