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
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
© 2024 OneMinuteCode. All rights reserved.