Python: Use a dictionary to output the mode of a text file

Question:

I have a text file with an unknown amount of numbers included. My program reads in the values and calculates the minimum value, the maximum value, the range and the median. The last thing that I have to do is find the mode of the set.

I have a sorted list that I am iterating through with a loop in order to place all of the numbers in a dictionary where the key is the number and the value for each number is how many times the number occurs. I understand how to do all of that, but now I need to output the mode of the set.

num_count = {}

for num in num_list:
    if num in num_count:
        num_count[num] += 1
    if num not in num_count:
        num_count[num] = 1

The above code is what places all of the numbers in the dictionary from the list and gives them the corresponding value with how many times that number occurs in the file, so now I just need to output the mode in a fashion such as [mode_1, mode_2, mode_3, etc..], but I can only get one number to appear.

I have a text file where the mode should output as:

Mode: [660, 476]

However, the code I have outputs as:

Mode: 660
Mode: 476

This is what I have so far:

maximum = 0

for num in num_count:
    count = num_count[num]
    if count > maximum:
        maximum = count
    if count == maximum:
        print('Mode: ', num)

Using a dictionary seems like the easiest way to go about this. Any help would be greatly appreciated. Basic python loop would be best, without imported things like counter and such.

Asked By: CBiegel8

||

Answers:

Use a list

maximum = 0
modes = []

for num in num_count:
    count = num_count[num]
    if count > maximum:
        maximum = count
    if count == maximum:
        modes.append(num)

print('Mode: ', modes)
Answered By: nosklo

Create a function compute_mode with a argument which takes in a list(in this case num_count). The function finds mode and stores them in a temp_list which is then printed out

Append all the mode values to a list like this:

temp_list.append(num)

Then print the list like this:

print("Mode: " ,temp_list)

Final code:

temp_list = []
def compute_mode(my_list):
    counts = {}
    maxcount = 0
    for number in my_list:
        if number not in counts:
            counts[number] = 0
        counts[number] += 1
        if counts[number] > maxcount:
            maxcount = counts[number]

    for number, count in counts.items():
        if count == maxcount:
            temp_list.append(number)

num_count = [1,2,3,4,1,2,3,4,4] #any list
compute_mode(num_count)
print('Mode:', temp_list)
Answered By: Agile_Eagle
  • You can achieve that with the built-in module collections.

  • Use Counter to count the occurrence of each element in the list and save it in a dictionay.

  • Use most_common(n) function to give the most common n numbers in the dictionary.

    import collections
    from collections import Counter
    
     num_count = [1,2,3,4,1,2,3,4,4]    
     count_dict = Counter(num_count)
     print(count_dict.most_common(1)[0])  # <--- you can change 1 to 2 or 3
    

output:

(4, 3)
Answered By: Hamzah
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.