Python sort 🗄️

Поделиться
HTML-код
  • Опубликовано: 29 окт 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

  • @CpTech97
    @CpTech97 Месяц назад

    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

  • @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

  • @oguzhantopaloglu9442
    @oguzhantopaloglu9442 3 года назад +5

    wow you explained it so simply!

  • @shinystick241
    @shinystick241 3 года назад +3

    Keep this good work!!

  • @Udara397
    @Udara397 4 месяца назад

    Great explanations 😇

  • @ahiamatagabriel5696
    @ahiamatagabriel5696 2 года назад +1

    thank you

  • @armaji
    @armaji 10 месяцев назад

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

  • @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))

  • @galonpanda3926
    @galonpanda3926 7 месяцев назад

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

  • @KaylaLo
    @KaylaLo Год назад

    Example is easy to understand

  • @passportbro904
    @passportbro904 10 месяцев назад

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

  • @syarookirimaa6885
    @syarookirimaa6885 2 года назад +1

    This video is so great!

  • @cristiucvladimir7909
    @cristiucvladimir7909 5 месяцев назад

    Thanks bro!!!

  • @rubenc4696
    @rubenc4696 6 месяцев назад

    Thanks

  • @abdullahsaid181
    @abdullahsaid181 7 месяцев назад

    thanks

  • @uuhju7004
    @uuhju7004 3 года назад +1

    nice video

  • @huntercraft5674
    @huntercraft5674 Год назад

    always surprising me!

  • @MY-pj3gf
    @MY-pj3gf 2 года назад

    thank you good sir

  • @xushnudallaberganov4847
    @xushnudallaberganov4847 Год назад

    quite good

  • @shavindasilva
    @shavindasilva 2 года назад

    thank you sir

  • @Amir_Plays_non_stop
    @Amir_Plays_non_stop 3 года назад +1

    Thank you

  • @EissaAlahdul
    @EissaAlahdul 2 года назад

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

  • @tarikyassa1076
    @tarikyassa1076 3 года назад +1

    Thanks Bro!

  • @EmirKaanOgsarim
    @EmirKaanOgsarim 6 месяцев назад

    ty bro

  • @SuperStarEevee
    @SuperStarEevee 2 года назад

    Thank you!

  • @MrNess2911
    @MrNess2911 Год назад

    Thank you Bro!

  • @lw9954
    @lw9954 2 года назад

    Thanks bro

  • @bekturasanbekov1979
    @bekturasanbekov1979 Год назад

    thx 4 vid bro !

  • @Amangupta-rh7cy
    @Amangupta-rh7cy 3 года назад +2

    Nice video

  • @beingzero7541
    @beingzero7541 2 года назад

    Wow!

  • @abdullahamjad9090
    @abdullahamjad9090 3 года назад

    keep going Bro .....
    great job !

  • @piotrkopcewicz5227
    @piotrkopcewicz5227 Год назад

    thx Bro !!

  • @gamePlays9699
    @gamePlays9699 Год назад +1

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

  • @gustavoaponte1814
    @gustavoaponte1814 Год назад

    meow~!

  • @farhan8834
    @farhan8834 2 года назад

    Here is my random comment for your BRO ❤

  • @BurhanuddinbhaiYusufbhaiPetiwa
    @BurhanuddinbhaiYusufbhaiPetiwa 8 месяцев назад

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

  • @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

  • @avinabani8749
    @avinabani8749 4 месяца назад

    ily bro

  • @BigBrother4Life
    @BigBrother4Life 3 года назад

    comment

  • @ANS-t3m
    @ANS-t3m 9 дней назад +1

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

  • @Rajkumar_Tripat
    @Rajkumar_Tripat Год назад

    thanks bro

  • @gemcyborgd1202
    @gemcyborgd1202 2 года назад

    thanks