How do you split a number into two parts in python?

How do I split a number into two without any arithmetic in python? For example, the table below:

20670005000000
30003387889032
44555008000004

I want the string to look like the columns below after splitting [and remain numbers that can be used in numerical analysis]:

2067000  5000000
3000338  7889032
4455500  8000004

What's the neatest way to do this in Python? Is there a python module that deals with this directly?

asked Oct 12, 2017 at 14:51

5

This is a possible way:

N = 10000000
i = 20670005000000

# The first part of the number i
i // N
# the second part
i % N

answered Oct 12, 2017 at 14:56

elzellelzell

2,1781 gold badge14 silver badges26 bronze badges

1

I don't know if it is the best way, but you can convert to string, than split it, and convert it to integer again:

number = [int[str[number][:len[number]/2]], int[str[number][len[number]/2:]]]

This will give you a touple which you can use later. You can further optimize this by using list comprehension.

answered Oct 12, 2017 at 14:56

It seems that the existing pattern of operation in this problem is to divide each string value in half.

import re
s = [20670005000000, 30003387889032, 44555008000004]
final_string = [map[int, re.findall['.{'+str[len[str[i]]//2]+'}', str[i]]] for i in s]

Output:

[[2067000, 5000000], [3000338, 7889032], [4455500, 8000004]]

answered Oct 12, 2017 at 14:59

Ajax1234Ajax1234

67.3k7 gold badges58 silver badges98 bronze badges

you could map the int to str, then split it. remember convert it back to int

  data = [20670005000000,
        30003387889032,
        44555008000004,]

  str_data = map[str, data]
  res = map[lambda x: [x[:len[x]//2],x[len[x]//2:]], str_data ]
  res = map[int, reduce[lambda x,y: x+y, res] ]
  print res

output:

[2067000, 5000000, 3000338, 7889032, 4455500, 8000004]

answered Oct 12, 2017 at 14:59

galaxyangalaxyan

5,6802 gold badges18 silver badges41 bronze badges

2

Assuming the number should be divided exactly at center each time, we can use string and slicing of it to get the two numbers.

Doing with single number here :

>>> s = str[20670005000000]
>>> l = int[len[s]/2]
>>> a,b = s[:l],s[l:]
>>> int[a]
=>  2067000   
>>> int[b]
=>  5000000

Here, for the List of numbers :

for ele in map[str,arr]: 
   l = int[ len[ele]/2 ] 
   a,b = ele[:l], ele[l:]

   print[int[a], int[b]] 

# driver values

IN : arr = [20670005000000, 30003387889032, 44555008000004]

OUT : 2067000 5000000
      3000338 7889032
      4455500 8000004

answered Oct 12, 2017 at 14:58

Kaushik NPKaushik NP

6,5348 gold badges30 silver badges57 bronze badges

Assuming str.len[] does not involve arithmetic and comparing two integers [2 > 3] is NOT arithmetic:

import collections
n = 20670005000000

left = collections.deque[str[n]]
right = collections.deque[]
while len[left] > len[right]:
    right.appendleft[left.pop[]]

left = int[''.join[left]]
right = int[''.join[right]]

answered Oct 12, 2017 at 15:30

wwiiwwii

21.9k7 gold badges34 silver badges75 bronze badges

How do you split a number in Python?

To split an integer into digits: Use the str[] class to convert the integer to a string. Use a list comprehension to iterate over the string. On each iteration, use the int[] class to convert each substring to an integer.

How do you split an integer into two parts in Python?

The number num is first converted into a string using str[] in the above code. Then, list comprehension is used, which breaks the string into discrete digits. Finally, the digits are converted back to an integer using the int[] function.

How do you split a number in half in Python?

We use the len[] function which gives us the length of the given string. In string2, we again divide the length of the string by 2, to get to the second half of the string. Here, we '//' operator for dividing the length of the original string, and it returns an integer.

How do you separate a number into two parts?

Divide it into two strings: For string 1, find all the positions of digit 4 in the string change it to 3 we can also change it to another number. For the second string put 1 at all positions of digit 4 and put 0 at all remaining positions from the 1st position of digit 4 to the end of the string.

Chủ Đề