Python tutorials: Sorting (reverse, key, multiple attributes / value)

Поделиться
HTML-код
  • Опубликовано: 10 сен 2024

Комментарии • 39

  • @leonardorezende
    @leonardorezende 5 лет назад +10

    Hi... If we need order the first item of the tuple in ascending order and the second in descending order? Is it possible with sorted?

    • @PyMoondra
      @PyMoondra  5 лет назад +11

      Interesting question! I did a little digging around and playing around and yes you can. You can use nested sorting as follows (I wish I could have included it in my video):
      func = lambda x: (x[0])
      func2 = lambda x: (x[1])
      sorted(sorted(tuple_list, key = func), key = func2, reverse = True)
      You can stack the sort!

    • @momshadalvee
      @momshadalvee 4 года назад +8

      key = lambda x : (x[0], -x[1])

    • @momshadalvee
      @momshadalvee 4 года назад

      PyMoondra don’t do this

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

      @@momshadalvee brilliant, thank you.

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

      @@momshadalvee what if you want to sort x[0] in the reverse order? because you cannot use -x[0] for string

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

    OMG u r a saviour , i have been banging my head to actually get this , you made it so simple , thnks a ton

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

    Wasted a lot of time trying to get this and you explained it in less than 6 minutes.

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

    i like you include both pronunciation of tuple whenever you say it.

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

    Exactly what I was looking for.

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

      Welcome to my channel too, which has both Python and R playlists, ppt and coding windows, with downoadabe source files.

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

    Thanks, Great demonstration!

  • @jihadijesus2346
    @jihadijesus2346 3 года назад +2

    Tuples Tooples :) Nice explanation.

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

      He should have just picked one way to say it and gone with it.

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

    Last one wont work for me i need to sort participants from highest to lowest but if there are lik 56 and 56 i need to put it lexicographically and when i get to 100 it puts that participant as lowest

  • @ProphetPearls
    @ProphetPearls 4 года назад +1

    glist=sorted(glist,key=lambda x:abs(x),reverse=True)

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

    awesome bruh!! love from india 😍

  • @az1k643
    @az1k643 4 года назад +1

    you are the best u just saved me with the multiple attributes idea!! THANKS MANN (subscribed + liked)

    • @PyMoondra
      @PyMoondra  4 года назад +2

      Thank you some much for the support.

    • @az1k643
      @az1k643 4 года назад

      @@PyMoondra i'm wondering if you can do a video about function complexity with big O and stuff?

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

    Which is it, a tuple or a tuple?? ;)
    Thanks for the content!

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

    What are tuples-tooples? Can't find anything about them in the python documentation.

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

      tuples. Type of data contained/data structure in Python

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

      @@PyMoondra not tuples. Tuples-tooples.

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

      @@onlinecomedyclub5725 there's not such thing

    • @WilliamLee-zf6tb
      @WilliamLee-zf6tb 2 года назад +1

      @@onlinecomedyclub5725 the joke somehow flew over both of their heads

  • @waterdamagerestoration4490
    @waterdamagerestoration4490 4 года назад

    How do I manage to have the key and the value in a list beneath each other making just 1 list??
    Thanks

    • @PyMoondra
      @PyMoondra  4 года назад

      I am not quite sure exactly what you mean.

    • @waterdamagerestoration4490
      @waterdamagerestoration4490 4 года назад

      @@PyMoondra I know how to get the output of the value, and the key seperately. But my question is, how I get them under each other.... like the value under the key (or the other way around) ?? Like in this case I want to have the a,b,c, etc first (the whole alphabet). And then the Alpha, beta, etc. under all the letters. www.screencast.com/t/h1kXtHzkUKX

  • @sharifmansuri607
    @sharifmansuri607 4 года назад

    Thanks a lot, very useful video

    • @PyMoondra
      @PyMoondra  4 года назад +1

      Thank you for such a nice comment!

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

    Wtf I just spent 30 min trying to figure this ish out.

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

    what is it key = len

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

    Its bad practice to assign a lambda function to a variable. They are supposed to be anonymous and used in-line.

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

    mafhemt chay ...