-1

I want to multiply y_list by a number until all numbers in list are >=1, and then just print the new list (without Numpy if it is possible).

Maybe multiply y_list as a row and numbers in i_list=[range(1,20)] as a column. and then stop when all numbers are bigger than 1.

For example: y_list= [1.0, 0.5669650111879195] to until y_list=[2.0, 1.12.. ]

y_list= [1.0, 0.5669650111879195]
i=0
while all(y <= 1 for y in y_list):
    i+=1
    if all(y<= 1 for y in y_list):
        break
    print(i)
Marcs
  • 3,768
  • 5
  • 33
  • 42

3 Answers3

1

The easiest way to do this is multiply the whole list by the inverse of the smallest term. This will make that term 1 and the rest some factor of it:

inverse_min = min(y_list) ** -1
for int in y_list:
    print int * inverse_min

The example on repl.it

TemporalWolf
  • 7,727
  • 1
  • 30
  • 50
1

If you do need to multiply it by a number (let's assume it to be 1.3) until all the elements are larger than one, the following is the way to go:

y_list = [1.0, 0.1, 0.2, 0.01]
k = 1.3
while any(y<=1 for y in y_list):
    y_list = [y*k for y in y_list]

>>> print(y_list)
[112.45540695195751, 11.245540695195746, 22.491081390391493, 1.1245540695195746]
Vlas Sokolov
  • 3,733
  • 3
  • 26
  • 43
0

I'd recommend using addition instead of multiplication in this case as processors are much faster in addition than multiplications.

Please see What's the relative speed of floating point add vs. floating point multiply

Let's assume our sample data is [-0.4, 1.0, 0.5669650111879195]

y_list= [-0.4, 1.0, 0.5669650111879195]

y_factor = 0
y_min = min(y_list)
if y_min < 1:
    y_factor = 1.0000000000001 - y_min

y_list = [x+y_factor for x in y_list]

print (y_list)

Output:

[1.0000000000001, 2.4000000000001, 1.9669650111880195]

Our resultant array elements are at least greater or equals to 1. See https://repl.it/EDog/0

Community
  • 1
  • 1
Saleem
  • 8,728
  • 2
  • 20
  • 34