-1
n = 136462380542525933949347185849942359177

how to split it into group of 3 digits? the result that i want is

136, 462, 380, ..., 177
brianK
  • 25
  • 7
  • you can treat it like a string and split it. if you want to treat it like a nr, well, some math ops: divide, subtract etc – Petronella Apr 07 '21 at 16:29
  • `v = str(n);size = 3;res = [int(v[i:i + size]) for i in range(0, len(v), size)]` – azro Apr 07 '21 at 16:31

2 Answers2

0

A regex one-liner.

import re

# match 3 digits or 1-3 digits at the end
ls = re.findall(r"(\d{3}|\d{1,3}$)", str(n))

Result

print(ls)
# ['136', '462', '380', '542', '525', '933', '949', '347', '185', '849', '942', '359', '177']

This solution also extracts the last 1 or 2 digits if n has (3m+1) or (3m+2) digits.

Bill Huang
  • 4,491
  • 2
  • 13
  • 31
-1
n = 136462380542525933949347185849942359177
s = str(n)
a = []
for i in range(0, len(s) - 2):
    a.append(s[i: i + 3])
print(a)
MaximumBoy
  • 71
  • 10
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Abdul Aziz Barkat Apr 07 '21 at 17:24