473,847 Members | 1,753 Online

# split number on their counts in python

1 New Member
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16, 17 and if 11314151617 (11cnt) then should be split like 1,13,1,15,16,17

I am trying to code like

if linecnt == 12 :
numList = [int(digit) for digit in str(strsplit)]
print(numList)

however i m getting 1,2,1,3,1,4,1,5 ,`,6 ..

guyz any help plz..
Feb 6 '24 #1
2 30917
dev7060
645 Recognized Expert Contributor
I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16, 17 and if 11314151617 (11cnt) then should be split like 1,13,1,15,16,17
What you mean by count?
Mar 28 '24 #2
Hichem
2 New Member
To split the number correctly based on your requirement, you should use a different approach that considers the count of digits in the number. Here's a Python function that does that:
Expand|Select|Wrap|Line Numbers
1. def split_number(number, linecnt):
2.     number_str = str(number)
3.     result = []
4.
5.     if linecnt == 12:
6.         for i in range(0, len(number_str), 2):
7.             result.append(int(number_str[i:i+2]))
8.     elif linecnt == 11:
9.         i = 0
10.         while i < len(number_str):
11.             if i == 0 or i == 2:
12.                 result.append(int(number_str[i]))
13.                 i += 1
14.             else:
15.                 result.append(int(number_str[i:i+2]))
16.                 i += 2
17.
18.     return result
19.
20. # Test cases
21. number1 = 121314151617
22. linecnt1 = 12
23. number2 = 11314151617
24. linecnt2 = 11
25.
26. print(split_number(number1, linecnt1))  # Output: [12, 13, 14, 15, 16, 17]
27. print(split_number(number2, linecnt2))  # Output: [1, 13, 1, 14, 15, 16, 17]
28.
2 Weeks Ago #3