gpt4 book ai didi

python - 从后面的 3 个字符后添加 ", "

转载 作者:行者123 更新时间:2023-11-28 21:51:43 24 4
gpt4 key购买 nike

我只是试图在每 3 个字符之间显示一个“,”!

我试过:

players = ','.join([player[i:i+3] for i in range(0, len(player), 3)])
rankedplayers = ','.join([ranked[i:i+3] for i in range(0, len(ranked), 3)])
points = ','.join([totalpoints[i:i+3] for i in range(0, len(totalpoints), 3)])

但它只是从左到右开始,我怎么能从后如此从右到左呢?

编辑:

例如

player = 9502
ranked = 2040
totalpoints = 12390

输出将是:

player 950,2
ranked 204,0
totalpoints = 123,90

应该是:

player = 9,502
ranked = 2,040
totalpoints = 12,390

最佳答案

你可以从另一边反转范围,像这样:

player = ','.join([player[i:i+3] for i in reversed(range(0, len(player), 3))]) 

但是对于用逗号分组的数字,你真正想要做的是:

player = "{:,}".format(int(player)) 

关于python - 从后面的 3 个字符后添加 ", ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29675242/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com