In python, implement multiplication of integer elements in a list or array elements in a list
1. Assuming that the elements in the list are integers, the following functions can be called:
def list_any_two_mul(mylist): num = 1 temp = [] for i in mylist[:-1]: ([i * j for j in mylist[num:]]) num = num + 1 # Turn multiple lists into only one list results = [y for x in temp for y in x] return results
Suppose there is a list mylist = [3, 5, 6, 8], and the above function is calledlist_any_two_mul(mylist)
Then return [15, 18, 24, 30, 40, 48]
2. Assuming that the elements in the list are in an array form, the following functions can be called:
def list_any_two_and(mylist2): temp2 = [] num2 = 1 # First, compare the elements in the list for i in mylist2[:-1]: ([cv2.bitwise_and(i, j) for j in mylist2[num2:]]) num2 = num2 + 1 # Put the results after the match in a list results2 = [] for i in range(len(temp2)): for j in range(len(temp2[i])): (temp2[i][j]) return results2
Verify the above functions, the elements are listed in the array, and then call the function. For specific implementations, please refer to the following code:
>>> import numpy as np >>> a=(1,13).reshape(3,4) >>> a >>> array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]]) >>> b=(13,25).reshape(3,4) >>> b >>> array([[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24]]) >>> c=(0,2,(3,4)) >>> c >>> array([[0, 1, 1, 1], [0, 0, 1, 1], [0, 0, 1, 1]]) >>> d=[a,b,c] >>> g=list_any_two_and(g) >>> g >>> [array([[1, 2, 3, 0], [1, 2, 3, 0], [1, 2, 3, 8]], dtype=int32), array([[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0]], dtype=int32), array([[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0]], dtype=int32)]
This is the end of this article about Python's implementation of any two numbers in the list. For more related content on any number operation of Python list, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!