Python Ranking Features

Asked 2 years ago, Updated 2 years ago, 15 views

I'm creating a ranking function with Python.

I want to put a number in front of the ranking, what should I do?

with open("d:\ZeroBOT\Json_File\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 k, v in v1_sorted_by_lvl:
        write.append(f'{k} | {v["money"]}')
    print("\n".join(write))

The operation result of the above code is


> ID | 20001
> ID | 11001
> ID | 3001
> ID | 1001

It's like that I'd like to put a number in front of my ID.

python

2022-09-20 17:59

1 Answers

for i, (k, v) in enumerate(v1_sorted_by_lvl, start=1):
    write.append(f'{i} | {k} | | {v["money"]}')

https://docs.python.org/ko/3/library/functions.html#enumerate


2022-09-20 17:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.