I have 6 variables where there are unknows numbers in each. 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.
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..
I have tried this but some values are 0 so its wrong.
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; in Python 2, the /
operator on integers gives integer output, so you want to use:
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.
The rest of your code can just use the numbers in values
with indexing:
>>> 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. if the answer isn't exact it rounds down. so 1/2
is 0, not 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.
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.
(also, what everyone else is saying about using lists)
要将两个数字x
和y
,请使用/
运算符:
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.
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.
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)
.
result = map(lambda x: x / max(l), l)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.