-1

I'm trying to get the most common character from a matrix but instead I'm only getting the most common from the last entered row

R = int(input("Enter the number of rows:"))
C = int(input("Enter the number of columns:"))
matrix = []
print("Enter the elements rowwise:")
for i in range(R):
    a=[]
    for j in range(C):
        a.append((input()))
    matrix.append(a)
for i in range(R):
    for j in range(C):
        print(matrix[i][j], end = " ")
    print()
from collections import Counter
count = Counter(a)
print(count.most_common())

output:

Enter the number of rows:3
Enter the number of columns:3
Enter the elements rowwise:
a
a
a
e
e
d
c
c
a
a a a 
e e d 
c c a 
[('c', 2), ('a', 1)]
Prune
  • 76,765
  • 14
  • 60
  • 81
Ehab651
  • 3
  • 1
  • I suggest You put all Your imports at the beginning unless it is necessary to not do that – Matiiss Apr 21 '21 at 21:49
  • 1
    Just do `Counter(matrix)` and not `Counter(a)` – Onyambu Apr 21 '21 at 21:50
  • also try this: `count = Counter(matrix)` – Matiiss Apr 21 '21 at 21:50
  • @Onyambu, @Matiiss - Counter will not flatten your multidim lists for you. `Counter(matrix)` would complain about list not being hashable. – Faboor Apr 21 '21 at 21:56
  • in that case, you will have to do `Counter(sum(matrix, []))` – Onyambu Apr 21 '21 at 21:58
  • What are functions `int` and `input`? They are not base R functions. What packages are you loading? It would be simpler to read the values into a vector with `X <- scan(what="character")`. Then convert to a matrix with `X <- matrix(X, R, C, byrow=TRUE)` and then get the maximum with `mx <- max(table(X))`. Then `which(table(X)==mx)` tells you which letters are the most common. You do not need a loop to read characters at the console. – dcarlson Apr 21 '21 at 22:16
  • Flatten the matrix using a loop, and then pass the flat matrix to your counter. See this post for a very active discussion flattening Python lists. [https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists](https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-a-list-of-lists) – Donna Apr 22 '21 at 00:23

1 Answers1

1

You are using Counter(a). When you get the input you do:

for i in range(R):
   a=[]

so a will always be just a single row and be the last row when the for loop ends.

Because Counter only counts the outermost iterable, you cannot do Counter(matrix). However, you use a single Counter and update it for each row:

counts = Counter()
for i in range(R):
   a=[]
   for j in range(C):
       a.append((input()))
   matrix.append(a)
   counts.update(a)
Faboor
  • 1,365
  • 2
  • 10
  • 23
  • It worked! thank you. I removed the a = [] from the for loop. I'm sorry if the question was not very clear, it's my first time using stack overflow and still learning to code. – Ehab651 Apr 21 '21 at 23:08
  • may I ask if I want the output to only show the character without the count what should I modify? the output I'm getting is like [The most common character is: ('A', 10)]. but what if I want the output to be [The most common character is: A]? – Ehab651 Apr 21 '21 at 23:11
  • @Ehab651Keeping `a` in the loop is fine, as long as you understand what it means and what it contains after the loop. To get the most common character, you can do `counter.most_common(1)[0][0]` (first `[0]` specifies you have want the first pair in the most common list - watch out, if the counter is empty, this will throw an error; second `[0]` specifies you want the first element of the pair, which will be the actual character). – Faboor Apr 23 '21 at 12:54