Python replace values with others in combination

Asked

Viewed 141 times

-4

Opa my beasts, I am with a difficulty, I made a combination list of 0 and 1, where 1 will be replaced by positive values of a list, and 0 will be replaced by negative values of another list and added with another list of numbers. (all lists have 25 values/columns)

I’ve been able to replace the positive values for when I have 1, but not the negative values for 0, I’ve tried a lot and this below was my last and still nothing. If you can help me I’d be very grateful, hugs.

  combina =  product([0, 1], repeat=25)

  for item in combina:
    res = sum([v + i*p + (not i)*n for i,v,p,n in zip(item, inicial, positivos, negativos)])
    print(res)

DETAILED UPDATE

Leaving the script unmounted at the part I’m having problems:

positivos = [0.0195, 0.0194, 0.0193, 0.0193, 0.0196, 0.0205, 0.0204, 0.0212, 0.0202, 0.0189, 0.0190, 0.0200, 0.0188, 0.0195, 0.0199, 0.0209, 0.0199, 0.0199, 0.0197, 0.019, 0.0201, 0.0197, 0.0196, 0.0189, 0.0194]

negativos = [-0.0298, -0.0298, -0.0300, -0.0300, -0.0297, -0.0288, -0.0289, -0.0281, -0.0291, -0.0304, -0.0303, -0.0293, -0.0305, -0.0298, -0.0294, -0.0283, -0.0294, -0.0294, -0.0296, -0.0302, -0.0292, -0.0296, -0.0297, -0.0304, -0.0298]


  combina =  product([0, 1], repeat=25)
  for item in combina:
    for i,p,n in zip(item, positivos, negativos):
      teste = i*p
      print(teste) 

Printing what only the combination will show: (I will put only one part)

0
1    # os valores que são 1, eu já consigo substituir pelo da lista positivos
0
0
0
0
0

In list order, note that the positive number you show is the second value of the positive list, because the first is 0 (which would have to be the first negative)

And I’d have to leave like this:

-0.0298
0.0194
-0.0300
-0.0300
-0.0297
-0.0288
-0.0289

But logically it would have to be based on that first code that I showed, where everything is on one line.

  • 1

    Junior, I’m not sure what your goal is. You can edit your post in more detail about this problem you’re trying to solve?

  • @Allanjuan In which part did not understand friend?

  • 1

    I’m not sure what it is you’re trying to do. If you give an input/output example demonstrating what the program should do, I think it may be clearer

  • @Allanjuan updated

  • I drafted an answer and, after I saw its details, decided to delete it. Something else. even with the details you added I still don’t understand the focus of the issue.

  • @Solkarped the focus is that the 0 is not coming out with the negative values, in the last two examples, compare them, of to understand well.

  • Junior, every numeric set has value NULO for the addition operation. If you are working with integers the value NULO will be 0. If you are working with real numbers the value NULO will be 0.0. Note that 0 is a value NULO, that is to say, 0 nor is positivo and neither negativo.

  • @Solkarped understood in what you still had doubts, I updated the penultimate example, a check, where the value is 0.0194, is pq printing only the "i", in place of it would be 1

  • Junior, the value "0" HAS NO SIGN, neither positive nor negative. ``0é um valorNULL`.

  • @Solkarped, I know, but where zero is I want to replace with a negative value, the list of 0 and 1 that comes out, is just for combination reference

  • Then you should create a constraint so that when the value is <= 0 the result is 0 and, if not (value > 0) the result is 1.

  • Why you have to logically solve this problem in a row?

  • @Lucasmaraal because I have other functions below, which can influence the result, if this line is divided into several "for" and "if", but if you have any suggestion tell me that I try without problems, will not influence

  • I would first create two lists p and n, one with the positives and the other with the negatives and then concatenate the p + n. Finally, I would sum up the elements of this new list created with the other list you quote.

  • @Lucasmaraal good I’ll try, put as response in the post and with the script, so moves the topic and is easier to see.

Show 10 more comments

1 answer

0

I do not know if I understood the question very well but it follows a possible solution.

Code:

positivos = [0.0195, 0.0194, 0.0193, 0.0193, 0.0196, 0.0205, 0.0204, 0.0212, 0.0202, 0.0189, 0.0190, 0.0200, 0.0188, 0.0195, 0.0199, 0.0209, 0.0199, 0.0199, 0.0197, 0.019, 0.0201, 0.0197, 0.0196, 0.0189, 0.0194]

negativos = [-0.0298, -0.0298, -0.0300, -0.0300, -0.0297, -0.0288, -0.0289, -0.0281, -0.0291, -0.0304, -0.0303, -0.0293, -0.0305, -0.0298, -0.0294, -0.0283, -0.0294, -0.0294, -0.0296, -0.0302, -0.0292, -0.0296, -0.0297, -0.0304, -0.0298]

combina = [0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0]

combinada = [p if c > 0 else n for p, n, c in zip(positivos, negativos, combina)]
print(f'{combinada}')

Exit:

[-0.0298, 0.0194, -0.03, -0.03, -0.0297, -0.0288, -0.0289, 0.0212, 0.0202, -0.0304, -0.0303, 0.02, 0.0188, -0.0298, 0.0199, -0.0283, 0.0199, 0.0199, -0.0296, -0.0302, 0.0201, -0.0296, 0.0196, 0.0189, -0.0298]

Inside the zip we put the 3 initial lists, then we check inside the list combines which values are greater than zero and smaller than zero, so it allows us to fill the new list with the correct values.


Edit

from itertools import product 

positivos = [0.0195, 0.0194, 0.0193, 0.0193, 0.0196, 0.0205, 0.0204, 0.0212, 0.0202, 0.0189]

negativos = [-0.0298, -0.0298, -0.0300, -0.0300, -0.0297, -0.0288, -0.0289, -0.0281, -0.0291, -0.0304]

combina = [ list(item) for item in  product([0, 1], repeat=10)]

combinada = [[p if i > 0 else n for p, n, i in zip(positivos, negativos, item)] for item in combina]

I put only 10 elements for testing (because it is a long time computing if it is a larger number), I think it works for 25.

  • It’s kind of what I need, except that the combination would have to be all possible combinations of 0 and 1, so the combination = product([0, 1], repeat=25), if you run, you will see that there are millions of possible combinations, what I need

  • I added an update to the answer, see if that’s it. Hug!

  • Guy with 25 of the error "Exception has occurred: Memoryerror", up to 15 of the right wheel, but with 25 of the ;/

  • But this is due to the level of complexity of the operation. This permutation is gigantic possibly the pc is running out of memory space.

  • What must be done then?

  • The memory of the pc is limited, usually for very large calculations the solution is to go saving it in a database, in a txt file.

  • It is because this value 25, also corresponds to excel columns that it will write from A:Y

  • @Juniorlopes With 25 vc will have a total of 33,554,432 combinations (over 33 million lists, with 25 elements each). Taking into account that one float takes about 8 bytes (varies depending on the implementation, but is more or less that), the program will need to more than 6 gigabytes memory (and I am not even considering the additional memory for the list structure itself, which will also take up a considerable space, since there are 33 million lists). There is no way to leave all this in memory, you have to go saving the results somewhere and not keep them in memory.

  • @Juniorlopes With 15 it works because there are only 32,768 combinations, which takes about 6 megabytes (a little more because I didn’t count the additional structure of the lists themselves, but anyway, it is much quieter to keep in memory)

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.