Home > OS >  sort list by frequency of an Attribute of objects
sort list by frequency of an Attribute of objects

Time:10-02

I have a list of people. Every element of the list is an object of a class Person and has 3 attributes: name, age, height.
I need to sort this list by frequency of a name in list and if frequency is the same in alphabetical order using only sorted, no loops.
Example (objects):

[<name=Ellie, age=20, height=1.74>,
 <name=Sebastian, age=15, height=1.7>,
 <name=Lukas, age=19, height=1.82>,
 <name=Lukas, age=19, height=1.81>,
 <name=Alex, age=19, height=1.8>
]
 -> 
[<name=Lukas, age=19, height=1.82>,
 <name=Lukas, age=19, height=1.81>,
 <name=Alex, age=19, height=1.8>,
 <name=Ellie, age=20, height=1.74>,
 <name=Sebastian, age=15, height=1.7>
]

CodePudding user response:

As long as the list contains objects whose name is asserted in the object property called name this will work:

lst.sort(key= lambda x: (lst.count(x), x.name))

CodePudding user response:

Try this using counter & sort

lst = ['ellie', 'sebastian', 'lukas', 'lukas', 'alex']
lst = sorted(lst)
from collections import Counter
result = [item for items, c in Counter(lst).most_common()
          for item in [items] * c]
# Output of result


['lukas', 'lukas', 'alex', 'ellie', 'sebastian']

CodePudding user response:

class People:
    def __init__(self, name, age,height):
        self.name = name
        self.age=age
        self.height=height
p1=People("John",36,5.8)
p2=People("Mary",24,5.6)
p3 = People("John", 42, 6.1)
p4=People("Mel",18,5.9)
p5=People("John",26,5.7)
p6=People("Mary",32,5.11)
baselist=[p1,p2,p3,p4,p5,p6]

basename=[]
for obj in baselist:
    basename.append((obj.name,obj.height))
basename.sort()
finalobjlist=[]
for i in basename:

    for j in baselist:
        if i[0]==j.name and i[1]==j.height:
            finalobjlist.append(j)
print("")
for i in finalobjlist:
    print(i.name,i.age,i.height)

NOTE: ASSUMING Everyone's height is unique, I've sorted the initial names, and then compared it to the original obj's height and name finally appended it to a new array.

Output:

John 26 5.7
John 36 5.8
John 42 6.1
Mary 32 5.11
Mary 24 5.6
Mel 18 5.9

The final ppl objects are stored in a list (finalobjlist).

CodePudding user response:

so I made the class as u mentioned like

class Person:
    def __init__(self,age,height,name) -> None:
        self.age = age
        self.name = name
        self.height = height

and a list of people like

a = [Person(1,1,"ellie"),Person(1,1,"sebastian"),Person(1,1,"lukas"),Person(1,1,"lukas"),Person(1,1,"alex")]

next I counted the frequency

counts = {}
for i in a:
    if i.name not in counts:
        counts[i.name]=0
    counts[i.name] =1
counts

and using this key u can sort in your required manner mind the negative count to give output in manner u requested

key = lambda element:(-counts[element.name],element.name)
k = sorted(a,key=key)

output will be like:

for i in k:
    print(i.name)
lukas
lukas
alex
ellie
sebastian

CodePudding user response:

I think you should try something with 'key' parameter of sorted() function of python.

sorted(lst, key = lst.count(lambda y: y.name), reverse = 1)

Try playing with these parameters you will achieve what you are willing to, I believe. Have a Nice Day..!

  • Related