简体   繁体   English

如何在python中除数

[英]How to divide numbers in python

I have 6 variables where there are unknows numbers in each. 我有6个变量,每个变量中都有未知数。 So as the numbers there are so big, I want to find the major one and next divide it for itself and for the others just to have numbers like 1 or less. 因此,由于数字如此之大,我想找到一个主要的数字,然后再对其进行除法,而对于其他数字,除以等于或小于1的数字即可。

Example: 例:

a1=85550 
b1=-18996 
c1=45500 
d1=-60000 
e1=74666 
f1=-35666

In this case a1 is the major so I must divide it for it self to have 1 and for the rest to have a 0 decimal one.. 在这种情况下,a1是主音,因此我必须将其除以使其自身为1,而其余部分为0小数。

I have tried this but some values are 0 so its wrong. 我已经尝试过了,但是有些值是0,所以它是错误的。

if abs(a1>b1) and abs(a1>c1) and abs(a1>d1) and abs(a1>e1) and abs(a1>f1):
    a11=a1/a1
    b11=b1/a1
    c11=c1/a1
    d11=d1/a1
    e11=e1/a1
    f11=f1/a1
else:
    if abs(b1>a1) and abs(b1>c1) and abs(b1>d1) and abs(b1>e1) and abs(b1>f1):
        a11=a1/b1
        b11=b1/b1
        c11=c1/b1
        d11=d1/b1
        e11=e1/b1
        f11=f1/b1
    else:
        if abs(c1>a1) and abs(c1>b1) and abs(c1>d1) and abs(c1>e1) and abs(c1>f1):
            a11=a1/c1
            b11=b1/c1
            c11=c1/c1
            d11=d1/c1
            e11=e1/c1
            f11=f1/c1
        else:
            if abs(d1>a1) and abs(d1>c1) and abs(d1>b1) and abs(d1>e1) and abs(d1>f1):
                a11=a1/d1
                b11=b1/d1
                c11=c1/d1
                d11=d1/d1
                e11=e1/d1
                f11=f1/d1
            else:
                if abs(e1>a1) and abs(e1>c1) and abs(e1>d1) and abs(e1>b1) and abs(e1>f1):
                    a11=a1/e1
                    b11=b1/e1
                    c11=c1/e1
                    d11=d1/e1
                    e11=e1/e1
                    f11=f1/e1
                else:
                    if abs(f1>a1) and abs(f1>c1) and abs(f1>d1) and abs(f1>e1) and abs(f1>b1):
                        a11=a1/f1
                        b11=b1/f1
                        c11=c1/f1
                        d11=d1/f1
                        e11=e1/f1
                        f11=f1/f1

Keep your values in a list: 将您的值放在列表中:

values = [a1, b1, c1, d1, e1, f1]

Now the task is a simple one: 现在任务很简单:

max_value = max(values, key=abs)
values = [v/max_value for v in values]

For your input, that'd give: 供您输入:

>>> a1=85550 
>>> b1=-18996 
>>> c1=45500 
>>> d1=-60000 
>>> e1=74666 
>>> f1=-35666
>>> values = [a1, b1, c1, d1, e1, f1]
>>> max_value = max(values, key=abs)
>>> [v/max_value for v in values]
[1.0, -0.22204558737580363, 0.5318527177089422, -0.701344243132671, 0.8727761542957335, -0.4169023962594974]

This assumes you are using Python 3; 假设您使用的是Python 3; in Python 2, the / operator on integers gives integer output, so you want to use: 在Python 2中,整数的/运算符给出整数输出,因此您要使用:

max_value = float(max(values, key=abs))

just to make one of the operands a floating point number. 只是为了使操作数之一成为浮点数。 Otherwise you'll end up with [1, -1, 0, -1, 0, -1] instead. 否则,您将最终得到[1, -1, 0, -1, 0, -1]

The rest of your code can just use the numbers in values with indexing: 您的其余代码可以只使用索引中的values的数字:

>>> values[0]
1.0

or you can assign the values back to the original names with sequence assignment: 或者您可以通过序列分配将值重新分配回原始名称:

>>> a1, b1, c1, d1, e1, f1 = values
>>> b1
-0.22204558737580363

but generally you want to keep your data out of your variable names. 但通常您希望数据不包含变量名。

the / in python gives you an integer (whole number) if you are dividing integers. 如果将整数相除,则python中的/为您提供一个整数(整数)。 if the answer isn't exact it rounds down. 如果答案不正确,则四舍五入。 so 1/2 is 0, not 0.5. 因此1/2是0,而不是0.5。

instead of 代替

a / b

try converting one to a float: 尝试将一个转换为浮点数:

a / float(b)

then you will get, say, 0.5 where you expect it. 那么您将在期望值之内得到0.5。

you can also write a number as a float directly. 您也可以直接将数字写为浮点数。 1.0/2 will give 0.5, for example, because 1.0 is a float, not an integer. 例如, 1.0/2将给出0.5,因为1.0是浮点数,而不是整数。

(also, what everyone else is saying about using lists) (此外,其他人都在谈论使用列表)

要将两个数字xy ,请使用/运算符:

z = x / y

Place your values into a list. 将您的值放入列表。

l = [85550, -18996, 45500, -60000, 74666, -35666]

Sort the list in reverse order. 以相反的顺序对列表进行排序。

l.sort(reverse = True)

Then, map a function that divides each value by l[0] , or the largest known value. 然后, map一个将每个值除以l[0]或最大已知值的函数。

result = map(lambda x: x / l[0], l)

The answer will be every value in the list divided as integers by the max value, including the max value. 答案将是列表中的每个值,除以整数除以最大值(包括最大值)。

[1, 0, 0, -1, -1, -1]

If integer division is undesirable, cast x to a float. 如果不希望使用整数除法,请将x强制转换为浮点数。

result = map(lambda x: x / l[0], l)

[1.0, 0.8727761542957335, 0.5318527177089422, -0.22204558737580363, -0.4169023962594974, -0.701344243132671]

Alternative: Instead of sorting the list, only write the mapping function to go across x / max(l) . 替代方法:不用对列表进行排序,只需编写映射函数以遍历x / max(l)

result = map(lambda x: x / max(l), l)

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

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