Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sorted object in list
#1
[python 3.7.2]

Hello so I already read how to do it and even try the exemple with student_objects.
So my problem is I want to sort a list by one of their attribut.
class Vents:
       def __init__(self, colonne,ligne): # Notre méthode constructeur
           self.col=colonne
           self.li=ligne
           self.direction =feuille1.cell_value(ligne,0)
           self.durée=feuille1.cell_value(self.li,self.col)
           self.nom=feuille1.cell_value(11,colonne)
I did a list of object Vents, 16 list for 16 differents directions.(each one of those contain 5 objects)
so I want to sort them by "durée" which is a int.
direction1=[5 object Vents] which their "durée" value is [5, 7, 0, 0, 0]
I type
sorted(direction1, key=lambda direction= direction.durée)
and when I print it it give me [5, 7, 0, 0, 0]

[the exemple]
class Student:
        def __init__(self, name, grade, age):
                self.name = name
                self.grade = grade
                self.age = age
        def __repr__(self):
                return repr((self.name, self.grade, self.age))
        def weighted_grade(self):
                return 'CBA'.index(self.grade) / float(self.age)

student_objects = [
        Student('john', 'A', 15),
        Student('jane', 'B', 12),
        Student('dave', 'B', 10),]

sorted(student_objects, key=lambda student: student.age)
gives me [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10),]
So it didn't work too.
Reply
#2
Your student example works for me. It gave me [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]. Note that sorted creates a new list, it does not modify the original list. To sort the original list you would want the sort method of the list: student_objects.sort(key=lambda student: student.age).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
thanks a lot that was only that. once again thank you. this topic is closed (I'm searching how to do that ahahh)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError class object has no attribute list object scttfnch 5 667 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  'list' object not callable sidra 5 1,161 Nov-29-2020, 04:56 PM
Last Post: bowlofred
  Creating list of lists from generator object t4keheart 1 619 Nov-13-2020, 04:59 AM
Last Post: perfringo
  How to make elements return sorted? notsoexperienced 4 764 Sep-24-2020, 09:00 AM
Last Post: perfringo
  TypeError: 'list' object is not callable Python_Mey 1 872 Aug-25-2020, 03:56 PM
Last Post: Yoriz
  Why is my original list also sorted? Pedroski55 1 561 Jul-15-2020, 09:25 PM
Last Post: Yoriz
  Outputting Sorted Text files Help charlieroberrts 1 562 Jul-05-2020, 08:37 PM
Last Post: menator01
  converting string object inside a list into an intiger bwdu 4 867 Mar-31-2020, 10:36 AM
Last Post: buran
  set a new object node in a linked list via reference oloap 2 818 Mar-13-2020, 09:45 PM
Last Post: oloap
  create a list of object with a list of character studenthch 0 563 Feb-12-2020, 08:43 AM
Last Post: studenthch

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020