-1
n = 136462380542525933949347185849942359177

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

136, 462, 380, ..., 177
brianK
  • 25
  • 5
  • Welcome to Stack Overflow! Please take the [tour], read [what's on-topic here](/help/on-topic), [ask], and the [question checklist](//meta.stackoverflow.com/q/260648/843953), and provide a [mre]. "Implement this feature for me" is off-topic for this site. You have to _make an honest attempt_, and then ask a _specific question_ about your algorithm or technique. – Pranav Hosangadi Apr 07 '21 at 16:28
  • 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

-1
n = 136462380542525933949347185849942359177
s = str(n)
a = []
for i in range(0, len(s) - 2):
    a.append(s[i: i + 3])
print(a)
MaximumBoy
  • 46
  • 9
  • 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
-1

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,292
  • 1
  • 12
  • 29