-1

I'm trying to count the frequency of occurrence of the first element of a list within a list.

the list is:

list = [[4, 2, 1, 3], [4, 3, 1, 2], [4, 3, 1, 2], [1, 3, 4, 2], [2, 3, 4, 1], [2, 1, 3, 4]]

I want to count the first element of each list within the entire list and print to screen the number that appears the most frequent. Which in this example would be 4.

Any suggestions on how to do this?

If I create a counter to count frequencies using:

Counter(l[0] for l in firstValue).most_common())

If there was 2 or more numbers with the highest frequency of occurrences how would I select the smallest number?

E.g

counter = [(4, 3), (3, 3), (2, 2), (1, 1)]

How would I be able to sort this to print the smallest number that has the greatest frequency of occurrence. (e.g 3 in this example)

2 Answers2

1

Just split your task into a two parts:

  1. Retrieve first element from each inner list;
  2. Sort elements in descending order by number of occurrences.

You can easily find an answer of each of this problem on Stack Overflow:

  1. Extract first item of each sublist;
  2. Sorting a List by frequency of occurrence in a list.

Even question you've additionally answered in comments already have been answered: How to access the first element or number in counter using Python.

You should make some research efforts before asking question otherwise you waste both yours time and time of users who will answer your question.

Anyway, let's go back to your problem.

  1. To get first value of inner list you should iterate over outer list and retrieve first element from every item. With simple for loop it will look like this:

    source_list = [[4, 2, 1, 3], [4, 3, 1, 2], [4, 3, 1, 2], 
                   [1, 3, 4, 2], [2, 3, 4, 1], [2, 1, 3, 4]]
    first_items_list = []
    for inner_list in source_list:
        first_items_list.append(inner_list[0])
    

    You can also use list comprehension:

    source_list = [[4, 2, 1, 3], [4, 3, 1, 2], [4, 3, 1, 2], 
                   [1, 3, 4, 2], [2, 3, 4, 1], [2, 1, 3, 4]]
    first_items_list = [inner_list[0] for inner_list in source_list]
    
  2. Now we have to find max element in list of first items by frequency. To not reinvent the wheel you can apply collections.Counter and specifically it's Counter.most_common() method.

    To initialize Counter you need to pass first_items_list generated in code above to Counter() constructor. Then you need to call Counter.most_common() and pass 1 to arguments as you need just most common element:

    from collections import Counter
    ...
    counter = Counter(first_items_list)
    item, number_of_occurrences = counter.most_common(1)[0]
    

To simplify the code you can change list comprehension to generator expression and pass it directly into Counter constructor:

source = [[4, 2, 1, 3], [4, 3, 1, 2], [4, 3, 1, 2], 
          [1, 3, 4, 2], [2, 3, 4, 1], [2, 1, 3, 4]]
print("Most common:", Counter(inner[0] for inner in source).most_common(1)[0][0])
Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
0

You can use pandas for this, and the mode() function:

df = pd.DataFrame([[4, 2, 1, 3], [4, 3, 1, 2], [4, 3, 1, 2], [1, 3, 4, 2], [2, 3, 4, 1], [2, 1, 3, 4]]) # creating a dataframe out of your list of list
df[0].mode() # returns an int64 object which is the most frequent item in the column (first column = first element of each of your list)
int(df[0].mode()) # to get the raw int value

Returns:

>>> df[0].mode()
0    4
dtype: int64
>>> int(df[0].mode())
4

Docs:
How to create a dataframe from a list of list: https://stackoverflow.com/a/43175477/2681435
mode() idea: https://stackoverflow.com/a/48590361/2681435

kelyen
  • 202
  • 1
  • 8