简体   繁体   English

从Python的指数中提取数字

[英]extract number from exponent in Python

Hi everyone / Python Gurus 大家好/ Python Gurus

I would like to know how to accomplish the following task, which so far I've been unable to do so. 我想知道如何完成以下任务,到目前为止我还无法做到。

Here's what I have: 这是我所拥有的:

Q1 = 20e-6 Q1 = 20e-6

Now this is an exponential number that if you print(Q1) as is it will show: 2e-5 which is fine. 现在,这是一个指数数字,如果按原样打印(Q1),它将显示:2e-5很好。 Mathematically speaking. 从数学上讲。

However, here's what I want to do with it: 但是,这是我要使用的方法:

I want Q1 to print only the number 20. And based on the whether this is e-6 then print uC or if this e-9 the print nC. 我希望Q1仅打印数字20,然后基于,这是e-6,然后打印uC,或者如果此e-9,则打印nC。

Here's an example for better understanding: 这是一个更好理解的示例:

Q1=20e-6 Q1 = 20e-6

When I run print(Q1) show: 20uC. 当我运行print(Q1)时,显示:20uC。

Q2=20e-9 Q2 = 20e-9

When I run print(Q2) show: 20nC. 当我运行print(Q2)时显示:20nC。

Can you please help me figure this out? 你能帮我解决这个问题吗?

just replace the exponent using str.replace: 只需使用str.replace替换指数:

q1 = 'XXXXXX'
q1 = q1.replace('e-9', 'nC').replace('e-6', 'uC')
print(q1)

You can try by splitting the string: 您可以尝试通过拆分字符串:

'20e-9'.split('e')

gives

['20', '-9']

From there on, you can insert whatever you want in between those values: 从那里开始,您可以在这些值之间插入所需内容:

('u' if int(a[1]) > 0 else 'n').join(a)

(with a = '20e-9'.split('e') ) (带有a = '20e-9'.split('e')

You can not. 你不能。 The behaviour you are looking for is called "monkey patching". 您正在寻找的行为称为“猴子修补”。 And this is not allowed for int and float. 不允许使用int和float。

You can refer to this stackoverflow question 您可以参考这个stackoverflow问题

The only way I can think of is to create a class that extends float and then implement a __str__ method that shows as per your requirement. 我能想到的唯一方法是创建一个扩展float的类,然后实现一个按您的要求显示的__str__方法。

------- More explanation ----- -------更多说明-----

if you type 如果您输入

Q1 = 20e-6

in python shell and then 在python shell中,然后

type(Q1) your will get a type(Q1)您将得到一个

float

So basically your Q1 is considered as float by python type system 所以基本上您的Q1被python类型系统视为float

when you type print(Q1) the _str__ method of float is called 当您键入print(Q1)时,float的_str__方法被调用

The process of extending core class is one example of "monkey patch" and that is what I was refereing to. 扩展核心类的过程就是“猴子补丁”的一个例子,这就是我所指的。

Now the problem is that you can not "monkey patch" (or extend if you prefer that) core classes in python (which you can in some languages like in Ruby). 现在的问题是,您不能在python中使用“猴子补丁”(如果愿意,也可以扩展)(在Ruby等某些语言中可以)。

[int, float etc are core classes and written in C for your most common python distribution.] [int,float等是核心类,并用C编写,用于最常见的python发行版。]

So how do you solve it? 那么您如何解决呢?

you need to create a new class like this 您需要创建一个这样的新类

class Exponent(float): def init (self, value): self.value = value def __str__ (self): return "ok"

x = Exponent(10.0)
print(x) ==> "ok"

hope this helps 希望这可以帮助

I recommend you using si-prefix . 我建议您使用si-prefix

You can install it using pip : 您可以使用pip安装它:

sudo pip install si-prefix

Then you can use something like this: 然后,您可以使用以下内容:

from si_prefix import si_format

# precision after the point
# char is the unity's char to be used
def get_format(a, char='C', precision=2):
    temp = si_format(a, precision)

    try:
        num, prefix = temp.split()
    except ValueError:
        num, prefix = temp , ''

    if '.' in num:
        aa, bb = num.split('.')
        if int(bb) == 0:
            num = aa

    if prefix:
        return num + ' ' + prefix + char
    else:
        return num


tests = [20e-6, 21.46e05, 33.32e-10, 0.5e03, 0.33e-2, 112.044e-6]
for k in tests:
    print get_format(k)

Output: 输出:

20 uC
2.15 MC
3.33 nC
500
3.30 mC
112.04 uC

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

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