To do the procedure you describe, you basically need the type Counter
found in lib collections
and function zip
, python native. I will first paste the sample code and at the end I will explain.
from collections import Counter
m = [
[1,1,1,1,2,3,4,5,9,8],
[1,2,3,3,3,4,5,6,7,8],
[1,1,1,1,3,4,4,4,4,4]
]
inversa = []
contadores = []
resultado = []
for x in zip(m[0],m[1],m[2]):
inversa.append(list(x))
for x in inversa:
a = Counter(x)
b = a.most_common(1)
contadores.append(b)
for x in contadores:
a = x[0]
b = a[0]
resultado.append(b)
print(resultado)
We started the work importing the Counter
and defining the work matrix. For didactic purposes set a matrix 3 by 10, but don’t worry, you can increase its size and the code will still work.
After that I also defined three auxiliary lists, which will be explained throughout the code.
The first thing to do is to get the inverse (Mathematically, the correct term is transposed) of the inserted matrix, as this will facilitate our work. We do this using a for loop that traverses all terms from the list of tuples returned from the function zip and saves each tuple as a line from the reverse list.
if you broke the code there and gave a print in inverse would see something like:
[[1, 1, 1],
[1, 2, 1],
[1, 3, 1],
[1, 3, 1],
[2, 3, 3],
[3, 4, 4],
[4, 5, 4],
[5, 6, 4],
[9, 7, 4],
[8, 8, 4]]
After that we use one more for loop to run all rows of the inverse matrix (which are the columns of our original matrix) and we use the Counter to count the number of repetitions of each term.
Still inside our for loop we use the method most_common accompanied by the parameter 1 to obtain the most common term of each line and save the tuples resulting from that loop in the list accountants.
If you stop the code here and give a print in accountants would see something like:
[[(1, 3)], [(1, 2)], [(1, 2)], [(1, 2)], [(3, 2)], [(4, 2)], [(4, 2)], [(4, 1)], [(9, 1)], [(8, 2)]]
Being that for each column of our matrix we have a [(x, y)]
whereas x
represents the most repeated term and y
the amount of times this repeated.
Finally, we ran all the tuples from the list of counters, saved in a
the first field of each line (this section is necessary because although it is a list of tuples with only one component, it is still a list).
Still inside the final loop, rescued in b
the first term within a
, For it is the most repeated term that concerns us, and not how many times it has been repeated.
And finally we put all the values of b
within the results list and display this.
At the end of the code you will get this result:
[1, 1, 1, 1, 3, 4, 4, 4, 9, 8]
The list results being the vector composed by the most common terms :)
Do you know any other language? Why Python?
– Woss
Put the code you already have so we can understand your doubts and help you progress.
– Isac
Anderson, because the rest of the program is in python, that part of the doubt is about 1/3 of the code, the rest is done and right :)
– LVoltz