Bottom Page

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.
buran wrote Mar-01-2019, 04:15 PM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Quote
#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

Quote
#3
thanks a lot that was only that. once again thank you. this topic is closed (I'm searching how to do that ahahh)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Byte array is sorted when sending via USB daviddlc68 1 74 Aug-16-2019, 10:11 AM
Last Post: wavic
  Iterating trough Unique Object List yelyah 1 363 Dec-12-2018, 11:46 AM
Last Post: Gribouillis
  [Help] sorted() in while loop with user's input() {Screenshot attached} vanicci 5 747 Aug-04-2018, 08:59 PM
Last Post: vanicci
  understanding sorted key parameter amirt 2 461 Jul-30-2018, 06:27 PM
Last Post: amirt
  AttributeError: 'NoneType' object has no attribute 'n' in list of class objects jdrp 4 1,338 Jun-19-2018, 02:44 PM
Last Post: jdrp
  sorted dictionary? HenryJ 2 660 Feb-05-2018, 08:36 AM
Last Post: wavic
  TypeError: 'list' object is not callable rajeev1729 4 9,413 Sep-10-2017, 05:31 PM
Last Post: rajeev1729
  List data-member of object not updating inside loop. Sagar 2 1,039 Aug-30-2017, 01:07 PM
Last Post: Sagar
  want to know the kind of object whether its a python or json object johnkennykumar 5 1,868 Jan-21-2017, 08:47 AM
Last Post: snippsat
  How to create def for sorted() from list of versioning files (filename+datetime) DrLove73 10 2,932 Jan-16-2017, 11:43 AM
Last Post: DrLove73

Forum Jump:


Users browsing this thread: 1 Guest(s)