How to find N-Largest and N-Smallest Numbers in Python!

preview_player
Показать описание
Quick Python tip how to efficiently find the n largest or smallest numbers of a List.

A note from the official docs: "The latter two functions perform best for smaller values of n. For larger values, it is more efficient to use the sorted() function. Also, when n==1, it is more efficient to use the built-in min() and max() functions. If repeated usage of these functions is required, consider turning the iterable into an actual heap."

If you enjoyed this video, please subscribe to the channel:

~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~

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

Python tutorials be like:
Ohh so you wanna *do something* ? Just import the *dosomething* library and that's all, easy right?

obo
Автор

def min(list, n): return sorted(list)[:n]
def max(list, n): return sorted(list)[::-1][:n]

Write your fonctions youself, do not import useless modules that you do not understand

roulio
Автор

Your subscribers help make this one of my favourite channels! Keep up the great work.

tentaklaus
Автор

Another interesting function.
If you want to know how many times every number or word occur, you can do.
e.g
from collections import Counter
a = [1, 2, 1, 3, 2, 1, 2, 3, 1, 4, 5, 3, 5, 1, 2]

print(Counter(a))

It will give:
Counter({1: 5, 2: 4, 3: 3, 5: 2, 4: 1})

Here the result has given 5 instances of 1 and 4 instances of 2 and so on.

Counter is a dictionary subclass. All dictionary methods can be used in it.
Here elements are keys and count of these elements is value.
Just wanted to share.

asadqureshi
Автор

I read that as "How to find the n-word in python" as was mildly concerned for your channel

Finkelfunk
Автор

There’s always candidates telling me because we call a built in function, the time complexity is O(1)…

NoProblem
Автор

I have been using your tips in my codes these are so good. Thank you for the tips

manojm
Автор

It is much better in terms of complexity to use the build-in function of Python for sorting (Tim sort algorithm) approx. O(nlog(n)) and checking for the number of mins or maxS at the beginning or end of the vector respectively.

nicolasvillagranprieto
Автор

Indexing on sorted list will works aswell

NaveenKumar-vxtk
Автор

Edit: Turns out I misunderstood the problem statement. I thought they were just trying to get the smallest in a list. They were trying to get n-smallest. Then I agree that using min/max heap would be a good idea!

Old comment:
-I think this is a bad tip. heap queue creates unnecessary memory overhead. if you just want to know the min/max, simple loop function should be a way to go.
you want to pick your data structures based on your needs, like if you are frequently adding/removing things from a set and want to know min/max at any given time, then heap queue should be a way to go.-

Yutaro-Yoshii
Автор

The reason why i failed at coding is what reason would we have to use something like this for?

slumz
Автор

How to use this channel:

1. Check the title
2. Pause the video
3. Get the correct answer in the comments and leave

Imboredas
Автор

How about
Thats what i would do. Requires pandas but is faster i guess. I think Pandas knows evryone and is therefore best suited.

wissenistmacht
Автор

No one seems to have mentioned collections.Counter class? That's what I immediately came up off the top of my head and IMO it should be the most useful in most cases :/

Counter accounts for duplicate elements (given that they are hashable, which integers are) and nlargest() and nsmallest() don't really handle those (they're basically equivalent to sorted(reverse=True)[:n] and sorted()[:n] according to the documentation). Say if you have a counter object `c`, then you can use sorted(c)[-n:] or sorted(c)[:n] to obtain the n-largest or n-smallest elements. On top of that if you need the number of occurrences too you can just change `c` into `c.items()`; the item in the sorted list would then be a tuple (element, count)

If you don't care about the count then of course sorted(set(some_list))[-n:] and sorted(set(some_list))[n:] would be more efficient. (No imports!)

(Also I believe heapq.nlargest() and heapq.nsmallest() are just _functions_, not _methods_; the dot notation isn't specific to methods)

niuniujunwashere
Автор

Anything is possible in python as long as the first line has import 😁

scanticmediatv
Автор

Top python tip: don't do anything this guy says, just use sorted()[:n]. Its more readable, doesn't require an import, if n is large it's more efficient and if n is small then performance gain is negligible

seventeeen
Автор

Hey can I know what color theme you are using pls

hamdhyharoon
Автор

Ok but now how do you "actually" do it

dylanprice
Автор

Any advice on doing it on a list of dictionaries? And getting the whole dictionary object itself and not just the largest number?

CaptainLian
Автор

It's very useful. What is font name?

korngsamnang