Python Tricks: How to Find the Most Common Elements in a List

Python Tricks: How to Find the Most Common Elements in a List

Clever Programmer

6 лет назад

21,406 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@marinosterbah1883
@marinosterbah1883 - 09.11.2021 23:31

if you lazy thinking the list is long, take this :
my_groceries = ['apple', 'banana', 'banana', 'orange', 'banana', 'apple', 'beet', 'carrot', 'ginger', 'kale', 'ginger', 'ginger', 'ginger', 'kale', 'ginger']

Ответить
@nikunjdeshwal9538
@nikunjdeshwal9538 - 29.12.2020 18:09

Awesome!!!

Ответить
@meirnarts
@meirnarts - 27.08.2020 18:46

You are best

Ответить
@Dopeboyz789
@Dopeboyz789 - 14.05.2020 20:54

How does this work with a 3d list?

Ответить
@shhivram929
@shhivram929 - 28.12.2018 06:35

In the case of object(string) type pandas.series, .value_counts() would do the same but it gives out a dataframe which respect to each class within the category but this will save a lot of time. Thanks Qazi :D

Ответить
@canicemichael7010
@canicemichael7010 - 23.08.2018 15:14

Can I register to your platform when I have the money.

Ответить
@souvikghosh6966
@souvikghosh6966 - 19.08.2018 17:40

How to determine how much time just a particular word is used in a sentence, if I don't want to know the times for all of the words used in a whole sentence.. Please help me out for that!

Ответить
@sam_block
@sam_block - 18.08.2018 07:55

Which software is best for beginer to learn python ?

Ответить
@arnoldkilaini
@arnoldkilaini - 16.08.2018 23:38

Nice one qazi

Ответить
@abdulahiabdulkadir1514
@abdulahiabdulkadir1514 - 16.08.2018 23:31

Thanks to qazi I now know how to hack the us goverment

Ответить
@shreshthkaushik1937
@shreshthkaushik1937 - 16.08.2018 17:41

THIS IS WHAT I DID.


my_groceries = ['apple', 'banana', 'banana', 'orange', 'banana', 'apple', 'beet', 'carrot', 'ginger', 'kale', 'ginger', 'ginger', 'kale', 'ginger']
Final_List = []
DICT = {}
x=0

for element in my_groceries:
if element in DICT:
DICT[element]+=1
else:
DICT[element]=1

for item in DICT:
Final_List.append((item,DICT[item]))

while x<len(Final_List):

for product in range(0,len(Final_List)-1):
if Final_List[product][1] > Final_List[product+1][1]:
pass
else:
temp = Final_List[product]
Final_List[product] = Final_List[product+1]
Final_List[product+1] = temp
x += 1

print(Final_List)

Ответить
@sifo_io
@sifo_io - 16.08.2018 15:51

Thank you !!

Ответить
@amon-rawalid7846
@amon-rawalid7846 - 16.08.2018 03:42

hi kazi i am from algeria and i started learning python but i am not sure that i can find a job here in algeria so can i work from here as a freelancer or should i just quite learning it to not waste the time answer me please

Ответить
@PrimitiveFuturologist_YTC
@PrimitiveFuturologist_YTC - 15.08.2018 19:14

Not in descending order, but pretty close to the requirements (in alphabetical order, with less code :-P ).

my_groceries = ['apple', 'banana', 'banana', 'orange', 'banana', 'apple', 'beet', 'carrot', 'ginger', 'kale', 'ginger', 'ginger', 'kale', 'ginger']

First pass:

print('[' + ' '.join(sorted(map('(\'%s\', %d), '.__mod__, Counter(map(str.lower, map(str.strip, my_groceries))).items()))) +']')

Result: [('apple', 2), ('banana', 3), ('beet', 1), ('carrot', 1), ('ginger', 4), ('kale', 2), ('orange', 1) ]

Comparison to your code:

from collections import Counter
counter_thingy = Counter(my_groceries)
print(counter_thingy.most_common())

Result: [('ginger', 4), ('banana', 3), ('apple', 2), ('kale', 2), ('orange', 1), ('beet', 1), ('carrot', 1)]

Ответить
@veerpalsingh1195
@veerpalsingh1195 - 15.08.2018 16:19

Brother you make programming look easy and fun. Your channel helped me a lot to learn python. Thank you.

Ответить
@2369drew
@2369drew - 15.08.2018 00:45

awesome!

Ответить
@bhavikakapadia2462
@bhavikakapadia2462 - 14.08.2018 21:41

Wow, that was an easy trick that I didn't know;instead of writing while loop and add them.
This is really helpful. Thank you

Ответить
@lokeshsolanki1012
@lokeshsolanki1012 - 14.08.2018 19:30

Hey qazi, I am not able to find idle for windows and I don't know to use pycharm.

Ответить
@aayushmitra121
@aayushmitra121 - 14.08.2018 19:03

Yo I really like this series.

Ответить
@DRP-
@DRP- - 14.08.2018 18:33

Thanks! That was helpful

Ответить
@ritwikjha156
@ritwikjha156 - 14.08.2018 18:23

Nice...!

Ответить
@elviraeloramilosic9813
@elviraeloramilosic9813 - 14.08.2018 16:31

Thanks!

Ответить
@trendystuff6303
@trendystuff6303 - 14.08.2018 15:34

Thanks bro

Ответить
@vikastomar920
@vikastomar920 - 14.08.2018 15:23

Amazing...

Ответить
@mnmnaveed1319
@mnmnaveed1319 - 14.08.2018 10:43

After long time a video I know your bzy your with profitable program Good luck

Ответить
@palestinian5543
@palestinian5543 - 14.08.2018 09:59

Qazi !
Miss you ,bro .

Ответить
@cancerpml7433
@cancerpml7433 - 14.08.2018 09:05

I would suggest the name of this series is (Python Explorer)

Ответить
@maazs8817
@maazs8817 - 14.08.2018 08:52

Good to see u back man

Ответить
@Muslim18121
@Muslim18121 - 14.08.2018 08:49

Qazi you know how to teach!

Ответить
@amazonproducts491
@amazonproducts491 - 14.08.2018 08:40

5th😊

Ответить
@monk9008
@monk9008 - 14.08.2018 08:40

First

Ответить