最 Python 的正确分成三组的方法是什么?我看过这个答案 https://stackoverflow.com/a/2801117/1461607 但我需要它右对齐。最好是没有进口的简单高效的单线。
- '123456789' = ['123','456','789']
- '12345678' = ['12','345','678']
- '1234567' = ['1','234','567']
最佳答案
另一种方式,不确定效率(如果它们已经是数字而不是字符串会更好),但在 2.7+ 中是另一种方式。
for i in map(int, ['123456789', '12345678', '1234567']):
print i, '->', format(i, ',').split(',')
#123456789 -> ['123', '456', '789']
#12345678 -> ['12', '345', '678']
#1234567 -> ['1', '234', '567']
关于python - 将字符串右分成 3 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18121416/