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
  add object and name in list 3lnyn0 4 1,226 Nov-24-2022, 07:33 PM
Last Post: buran
  Failing to print sorted files tester_V 4 1,188 Nov-12-2022, 06:49 PM
Last Post: tester_V
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 4,721 Apr-27-2022, 09:27 AM
Last Post: Anldra12
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 14,773 Jan-19-2022, 08:33 AM
Last Post: menator01
  set and sorted, not working how expected! wtr 2 1,254 Jan-07-2022, 04:53 PM
Last Post: bowlofred
  AttributeError class object has no attribute list object scttfnch 5 3,341 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  'list' object not callable sidra 5 5,439 Nov-29-2020, 04:56 PM
Last Post: bowlofred
  Creating list of lists from generator object t4keheart 1 2,161 Nov-13-2020, 04:59 AM
Last Post: perfringo
  How to make elements return sorted? notsoexperienced 4 3,166 Sep-24-2020, 09:00 AM
Last Post: perfringo
  TypeError: 'list' object is not callable Python_Mey 1 2,447 Aug-25-2020, 03:56 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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