Python Ranking Features

Asked 2 years ago, Updated 2 years ago, 46 views

with open("main.json") as f:
    lvs = json.load(f)
v1_sorted_by_lvl = sorted(lvs.items(), key=lambda e: e[1]["money"], reverse=True)
write = []
for i, (k, v) in enumerate(v1_sorted_by_lvl, start=1):
    write.append(f'{i} `{k}` | {v["money"]}')
print("\n".join(write))

How can I mark only the 10th place in the ranking code above?

python json

2022-09-20 17:44

1 Answers

i is the rank. We can just spin the for loop until i reaches 10, and then we can go out of the loop when i reaches the loop.

For, when you go out of the loop, you can use break.

It's simple, right?


2022-09-20 17:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.