Most Frequent Value of A List - Python Tips and Tricks #15

preview_player
Показать описание
Today we will learn about different ways to find the most frequent value of a list.

◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚

🌐 Social Media & Contact 🌐

Рекомендации по теме
Комментарии
Автор

your English is excellent, respect for being a non native speaker.

joaquins.a.
Автор

You explain each code so clearly that it's really easy to understand. I truly appreciate your helpful explanations. Thank you for your efforts!

sonia
Автор

Really awesome when use set to make more efficient..

Arunkumar-xbwu
Автор

how do I do this but with a list of names? :(

aces
Автор

Hi, How can we write the number as Dates? ex: 09/21/2022..., to find reoccurrence between a set of them

therealritchy
Автор

In one line
max(mylist, key=mylist.count)

Two lines (slightly more polmorphic but there are more general ways to do it)
count = lambda e, array: sum(1 for i in array if i==e)
modal = max(array, key=lambda i: count(i, array))

My favorite (by setting greedy=True you can have count(‘aa’, ’aaa’)->2, but it doesn’t change for lists/tuples etc):
from itertools import tee, islice
from typing import Any, Iterable

gen = type(i for i in range(0))

def chords(iterable:Iterable[Any], rad:int) -> gen:
# yields consecutive sub-iterables of length “rad”
t = tee(iterable, rad)
yield from zip(*(it if n==0 else islice(it, n, None) for n, it in enumerate(t)))

def count(element, iterable:Iterable[Any], greedy:bool=False) -> int:
if
return if not greedy else sum(1 for i in chords(iterable, len(element)) if join(i)==element)
return len([i for i in iterable if i==element])

modal = max(array, key=lambda i: count(i, array))

morgengabe
Автор

"Write a function that prints out the name and count of the least frequent and most frequent colors from the previous question. Use an f string (formatted string) if possible." can anyone give me some ideas on how to do that?

el_chico