简体   繁体   English

如何在python中将数字(不是字符)分成两部分

[英]how to split number (not character) into two parts in python

How do I split a number into two without any arithmetic in python? 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?在 Python 中执行此操作的最简洁方法是什么? Is there a python module that deals with this directly?有没有直接处理这个的python模块?

This is a possible way:这是一种可能的方式:

N = 10000000
i = 20670005000000

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

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.这会给你一个你可以稍后使用的touple。 You can further optimize this by using list comprehension.您可以使用列表理解进一步优化它。

Assuming the number should be divided exactly at center each time, we can use string and slicing of it to get the two numbers.假设每次都应该在center精确划分数字,我们可以使用stringslicing来获得两个数字。

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

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]]

you could map the int to str, then split it.您可以将 int 映射到 str,然后将其拆分。 remember convert it back to int记得把它转换回 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]

Assuming str.len() does not involve arithmetic and comparing two integers ( 2 > 3 ) is NOT arithmetic:假设str.len()不涉及算术并且比较两个整数 ( 2 > 3 ) 不是算术:

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM