我只是试图展示一个","每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
答案 0 :(得分:3)
你可以从另一边反转范围,如下:
player = ','.join([player[i:i+3] for i in reversed(range(0, len(player), 3))])
但是为了用逗号分组数字,你真正想做的是:
player = "{:,}".format(int(player))
答案 1 :(得分:2)
此regex
完全符合您的要求:
(/(\d)(?=(?:\d{3})+$)/g, '$1,')
最适合使用replace
方法。