-1

Literally need correction in only one line of the code below. Everything in the code works fine except for line: dwarfs.sort(key=lambda x:-x.physics) which is incomplete. After creating the objects and appending them in the list dwarfs and keeping track of the colors in the colors dictionary I want to sort the objects by: self.physics and then, if multiple with the same value, by appearance of the color in colors dictionary. With my current code when entering

Grumpy <:> Red <:> 5000
Grumpy <:> Blue <:> 10000
Grumpy <:> Red <:> 10000
Happy <:> Blue <:> 10000
Once upon a time

I would like to receive:

(Blue) Grumpy <-> 10000
(Blue) Happy <-> 10000
(Red) Grumpy <-> 10000

Instead I have:

(Blue) Grumpy <-> 10000
(Red) Grumpy <-> 10000
(Blue) Happy <-> 10000

Code:

class Dwarf:
    def __init__(self,name,hat_color,physics:int):
        self.name=name
        self.hat_color=hat_color
        self.physics=physics

    def __repr__(self):
        return f"({self.hat_color}) {self.name} <-> {self.physics}"

dwarfs=[]
colors={}

while True:
    entry=input()
    if entry=="Once upon a time":
        break
    name,hat_color,physics=entry.split(" <:> ")
    physics=int(physics)
    obj=Dwarf(name,hat_color,physics)
    tracker=None
    if len(dwarfs)==0:
        dwarfs.append(obj)
    else:
        for i in dwarfs:
            if name==i.name and hat_color==i.hat_color:
                tracker=i
                break
        if tracker!= None:
            dwarfs.remove(tracker)
        dwarfs.append(obj)
    if hat_color not in colors:
        colors[hat_color]=1
    else:
        if tracker is None:
            colors[hat_color]+=1

colors={k:v for k,v in sorted(colors.items(),key=lambda x:-x[1])}
dwarfs.sort(key=lambda x:-x.physics)

for dwarf in dwarfs:
    print(dwarf)
ddoch003
  • 17
  • 3
  • 1
    Please make your [mre] minimal. Unless you have a problem with the code that *creates* the dicts, hardcode an example instead. – Kelly Bundy Mar 04 '23 at 17:40

1 Answers1

1

Everything in the code works fine except for line: dwarfs.sort(key=lambda x:-x.physics) which is incomplete.....I want to sort the objects by: self.physics and then, if multiple with the same value, by appearance of the color

Change the key argument in dwarfs.sort(key=lambda x:-x.physics) to

  • dwarfs.sort(key=lambda x:(-x.physics, -colors[x.hat_color])) to have the color with the maximum value in colors at the top (which is probably what you want, judging by how you sorted colors first)
  • dwarfs.sort(key=lambda x:(-x.physics, x.hat_color)) if you just want to sort by the name of the color
  • dwarfs.sort(key=lambda x:(-x.physics, x.hat_color, x.name))if you want to sort by the name as well....

For more details see Sorting HOW TO — Python 3 documentation.


Btw, you can shorten your code to

dwarfs, colors = [], {}
while (entry := input())!='Once upon a time':
    name,hat_color,physics = entry.split(" <:> ")
    obj = Dwarf(name, hat_color, int(physics))

    for i, d in enumerate(dwarfs):
        if d.name==name and d.hat_color==hat_color:
            dwarfs.pop(i)
            break
    else: colors[hat_color] = colors.get(hat_color,0)+1
    dwarfs.append(obj) 

# colors = dict(sorted(colors.items(),key=lambda x:-x[1])) # ? unnecessary ?
dwarfs.sort(key=lambda x:(-x.physics, -colors[x.hat_color]))

print(*dwarfs, sep='\n')
Driftr95
  • 4,572
  • 2
  • 9
  • 21
  • 1
    @KellyBundy what does that mean? something like `list(colors.keys()).index(x.hat_color)`? The rgb value? `colors[x.hat_color]`? Until they specify, I'll assume they just meant the color itself ¯\_(ツ)_/¯ – Driftr95 Mar 04 '23 at 20:28
  • thank you so very much! `dwarfs.sort(key=lambda x:(-x.physics,-colors[x.hat_color]))` is exactly what I wanted. I was just not sure about the correct syntaxis. It is working fine now. – ddoch003 Mar 05 '23 at 07:37