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


Messages In This Thread
sorted object in list - by trois - Mar-01-2019, 03:14 PM
RE: sorted object in list - by ichabod801 - Mar-01-2019, 04:27 PM
RE: sorted object in list - by trois - Mar-04-2019, 09:12 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  add object and name in list 3lnyn0 4 1,276 Nov-24-2022, 07:33 PM
Last Post: buran
  Failing to print sorted files tester_V 4 1,245 Nov-12-2022, 06:49 PM
Last Post: tester_V
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 4,876 Apr-27-2022, 09:27 AM
Last Post: Anldra12
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 14,953 Jan-19-2022, 08:33 AM
Last Post: menator01
  set and sorted, not working how expected! wtr 2 1,282 Jan-07-2022, 04:53 PM
Last Post: bowlofred
  AttributeError class object has no attribute list object scttfnch 5 3,423 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  'list' object not callable sidra 5 5,524 Nov-29-2020, 04:56 PM
Last Post: bowlofred
  Creating list of lists from generator object t4keheart 1 2,202 Nov-13-2020, 04:59 AM
Last Post: perfringo
  How to make elements return sorted? notsoexperienced 4 3,224 Sep-24-2020, 09:00 AM
Last Post: perfringo
  TypeError: 'list' object is not callable Python_Mey 1 2,471 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