Python sort 🗄️

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 พ.ย. 2024

ความคิดเห็น • 58

  • @BroCodez
    @BroCodez  3 ปีที่แล้ว +14

    # sort() method = used with lists
    # sort() function = used with iterables
    students = (("Squidward", "F", 60),
    ("Sandy", "A", 33),
    ("Patrick","D", 36),
    ("Spongebob","B", 20),
    ("Mr.Krabs","C", 78))
    grade = lambda grades:grades[1]
    # students.sort(key=age) # sorts current list
    sorted_students = sorted(students,key=grade) # sorts and creates a new list
    for i in sorted_students:
    print(i)

  • @shamaeelahmed3561
    @shamaeelahmed3561 2 ปีที่แล้ว +11

    He breaks down these complex topics into 3 minute videos!!
    Truly Amazing

  • @oguzhantopaloglu9442
    @oguzhantopaloglu9442 3 ปีที่แล้ว +5

    wow you explained it so simply!

  • @CpTech97
    @CpTech97 2 หลายเดือนก่อน

    Loving your videos, short but great explanation! thx for sharing your knowledge 🎉😊

  • @samirbekgozikulov8999
    @samirbekgozikulov8999 ปีที่แล้ว +2

    bro thank you i always learn things faster if i watch u

  • @Udara397
    @Udara397 5 หลายเดือนก่อน

    Great explanations 😇

  • @shinystick241
    @shinystick241 3 ปีที่แล้ว +3

    Keep this good work!!

  • @loveSG999
    @loveSG999 3 ปีที่แล้ว +16

    Honestly my brain went into the stupid mode when I was trying to digest the lambda function used to obtain the key by which the dataframe was going to be sorted
    Honestly I am not up to speed with lambda, and found the function defined in the old way more readable and easier to understand, as per my workings below
    # define a function to return a Series of items in a column (grades here), by which the Dataframe will be sorted
    def grab_item(items):
    return items[1] # the index number for grade items is 1
    # now sort the DataFrame using the key 'grab_item' as established above
    students.sort(key=grab_item)
    # execute below for loop to print out the list of tuples, sorted by the grades column
    for i in students:
    print(i)
    Output per below
    ('Sandy', 'A', 33)
    ('Spongebob', 'B', 20)
    ('Mr.Krabs', 'C', 78)
    ('Patrick', 'D', 36)
    ('Squidward', 'F', 60)

    • @majdnassar3257
      @majdnassar3257 3 ปีที่แล้ว +1

      Try this it is the same: mylist = [(3, 5, 8), (6, 2, 8), ( 2, 9, 4), (6, 8, 5)]
      print(sorted(mylist, key=lambda x: x[1]))

    • @majdnassar3257
      @majdnassar3257 3 ปีที่แล้ว +1

      cars = [('citroen', 'xsara', 1100), ('lincoln', 'navigator', 2000), ('bmw', 'x5', 1700)]
      print(sorted(cars, key=lambda car: car[0]))
      print(sorted(cars, key=lambda car: car[1]))
      print(sorted(cars, key=lambda car: car[2]))

    • @Craulback
      @Craulback 3 ปีที่แล้ว +5

      i posted this on another comment, but i'll reply here too just in case you still wonder & aren't notified on the other thread;
      "grade = lambda grades:grades[1]" just marks the index to look at when passing this as a key in the sorted() function later on.
      grade = lambda grades:grades[1] # "grades" is just an argument you can name anything you want
      # in this case the way it's used later will actually refer to the student tuple & all 3 keys
      # "grades[1]" just refers to the index within the argument passed as "grades", 1 being grade, 2 would be the age.
      sorted_students = sorted(students,key=grade) # sorted takes 2 args, 1st: the list/tuple, 2nd: a key to sort by, in this case "key=grades", which just points to an index
      # written in regular function style below, replacing "grades" with "student" for clarity:
      students = (("Squidward", "F", 60),
      ("Sandy", "A", 33),
      ("Patrick","D", 36),
      ("Spongebob","B", 20),
      ("Mr.Krabs","C", 78))
      def get_grades(student):
      return student[1]
      grade = get_grades
      sorted_students = sorted(students,key=grade)
      for i in sorted_students:
      print(i)

    • @5nakeB01
      @5nakeB01 2 ปีที่แล้ว

      can you pls explain how the function works as well

  • @syarookirimaa6885
    @syarookirimaa6885 2 ปีที่แล้ว +1

    This video is so great!

  • @KaylaLo
    @KaylaLo 2 ปีที่แล้ว

    Example is easy to understand

  • @ahiamatagabriel5696
    @ahiamatagabriel5696 2 ปีที่แล้ว +1

    thank you

  • @armaji
    @armaji 11 หลายเดือนก่อน

    I like the way I learned from you bro 😅❣️✌👌

  • @EissaAlahdul
    @EissaAlahdul 2 ปีที่แล้ว

    جزاك الله خيرا

  • @rubenc4696
    @rubenc4696 7 หลายเดือนก่อน

    Thanks

  • @uuhju7004
    @uuhju7004 3 ปีที่แล้ว +1

    nice video

  • @qcdominates7503
    @qcdominates7503 3 ปีที่แล้ว +8

    So.... Functions and methods are different?
    And, in the lambda function in 6:13 what exactly is ages and ages[2]? I mean, there is no defined list called ages...
    Is that list of tuple passed there with alias ages?
    And, can we create the key function normally? Like without the lambda operator?
    Thanks!
    PS: sorry for number and length of questions

    • @loveSG999
      @loveSG999 3 ปีที่แล้ว +1

      You are not alone on this one, please see my comments above

    • @majdnassar3257
      @majdnassar3257 3 ปีที่แล้ว

      The use of lambda creates an anonymous function (which is callable). In the case of sorted the callable only takes one parameters. Python's lambda is pretty simple. It can only do and return one thing really.

    • @Craulback
      @Craulback 3 ปีที่แล้ว +3

      "grade = lambda grades:grades[1]" just marks the index to look at when passing this as a key in the sorted() function later on.
      grade = lambda grades:grades[1] # "grades" is just an argument you can name anything you want
      # in this case the way it's used later will actually refer to the student tuple & all 3 keys
      # "grades[1]" just refers to the index within the argument passed as "grades", 1 being grade, 2 would be the age.
      sorted_students = sorted(students,key=grade) # sorted takes 2 args, 1st: the list, 2nd: a key to sort by, in this case "key=grade", which just points to an index
      # written in regular function style below:
      students = (("Squidward", "F", 60),
      ("Sandy", "A", 33),
      ("Patrick","D", 36),
      ("Spongebob","B", 20),
      ("Mr.Krabs","C", 78))
      def get_grades(student):
      grades = student[1]
      return grades
      grade = get_grades
      sorted_students = sorted(students,key=grade)
      for i in sorted_students:
      print(i)

    • @partlycloudy2346
      @partlycloudy2346 2 ปีที่แล้ว

      @@Craulback so the labda fuction just returns an index of 1 and it doesnt hav anything to do with the student list right? cause we arnt passing in students as an argument. but the the index that it passes is used when sorting the list. correct me if im wrong pls

    • @Craulback
      @Craulback 2 ปีที่แล้ว

      ​@@partlycloudy2346 the lambda function returns whatever is actually **in** index 1, but in the function itself no list is passed.
      you can then do what you want with that, like pass any list you want to sort by that index position through the built in sorted function, using the "grade" variable as the key "sorted_students = sorted(students,key=grade)"
      if you want to see what's happening you can print it:
      for student in students:
      print(grade(student))

  • @MY-pj3gf
    @MY-pj3gf 2 ปีที่แล้ว

    thank you good sir

  • @huntercraft5674
    @huntercraft5674 ปีที่แล้ว

    always surprising me!

  • @xushnudallaberganov4847
    @xushnudallaberganov4847 ปีที่แล้ว

    quite good

  • @shavindasilva
    @shavindasilva 2 ปีที่แล้ว

    thank you sir

  • @lw9954
    @lw9954 2 ปีที่แล้ว

    Thanks bro

  • @Amangupta-rh7cy
    @Amangupta-rh7cy 3 ปีที่แล้ว +2

    Nice video

    • @BroCodez
      @BroCodez  3 ปีที่แล้ว +3

      Thanks Aman!

  • @cristiucvladimir7909
    @cristiucvladimir7909 6 หลายเดือนก่อน

    Thanks bro!!!

  • @beingzero7541
    @beingzero7541 2 ปีที่แล้ว

    Wow!

  • @EmirKaanOgsarim
    @EmirKaanOgsarim 7 หลายเดือนก่อน

    ty bro

  • @Amir_Plays_non_stop
    @Amir_Plays_non_stop 3 ปีที่แล้ว +1

    Thank you

  • @gemcyborgd1202
    @gemcyborgd1202 3 ปีที่แล้ว

    thanks

  • @passportbro904
    @passportbro904 11 หลายเดือนก่อน

    his only video ive ever thought he didnt explain well, guys, his human, get him lol

  • @galonpanda3926
    @galonpanda3926 8 หลายเดือนก่อน

    Bruh thank you so much, i was thinking about this literally overnight, and suddenly found this video.

  • @tarikyassa1076
    @tarikyassa1076 3 ปีที่แล้ว +1

    Thanks Bro!

  • @bekturasanbekov1979
    @bekturasanbekov1979 ปีที่แล้ว

    thx 4 vid bro !

  • @gamePlays9699
    @gamePlays9699 ปีที่แล้ว +1

    I did it but i dont understand very well of lambda function

  • @SuperStarEevee
    @SuperStarEevee 2 ปีที่แล้ว

    Thank you!

  • @piotrkopcewicz5227
    @piotrkopcewicz5227 2 ปีที่แล้ว

    thx Bro !!

  • @MrNess2911
    @MrNess2911 ปีที่แล้ว

    Thank you Bro!

  • @abdullahamjad9090
    @abdullahamjad9090 3 ปีที่แล้ว

    keep going Bro .....
    great job !

  • @gustavoaponte1814
    @gustavoaponte1814 ปีที่แล้ว

    meow~!

  • @farhan8834
    @farhan8834 2 ปีที่แล้ว

    Here is my random comment for your BRO ❤

  • @BurhanuddinbhaiYusufbhaiPetiwa
    @BurhanuddinbhaiYusufbhaiPetiwa 9 หลายเดือนก่อน

    can anyone please explain what value does grades hold in grades[1]

  • @avinabani8749
    @avinabani8749 5 หลายเดือนก่อน

    ily bro

  • @KeymanJump
    @KeymanJump ปีที่แล้ว

    why the lambda is refering to the list of tuples? I don't get that

    • @MoreBilaINoFilter
      @MoreBilaINoFilter ปีที่แล้ว

      Bro I literally watched it like +10 times and my brain still not able to compute this shit. HOW IS AGE FUNCTION USED WITHOUT ANY ARGUMENTS AND WHAT RELATE THE AGES[2] VARIABLE WITH THE LIST I DONT GET IT!!

    • @KeymanJump
      @KeymanJump ปีที่แล้ว

      @@MoreBilaINoFilter now I got it, age is just a variable wich contains a lambda but it doesn't do anything by itself, he just use thtat for the pick of each second parameter of each tuple in the dictionaire

  • @ilordepic
    @ilordepic 2 ปีที่แล้ว

    here's a comment for the algorithm bro ily

  • @BigBrother4Life
    @BigBrother4Life 3 ปีที่แล้ว

    comment

  • @ANS-t3m
    @ANS-t3m หลายเดือนก่อน +1

    You are my life saver 💕💕💕 thanks a lot bro🫴🫴💕💕

  • @abdullahsaid181
    @abdullahsaid181 8 หลายเดือนก่อน

    thanks

  • @Rajkumar_Tripat
    @Rajkumar_Tripat ปีที่แล้ว

    thanks bro