-1

Essentially I would like my while loop to take the dvals input list, identify the minimum element, add 0.05 to it and append it to the end of the doptions list of lists, but instead it seems to be overwriting every element of the list each iteration so only the last iteration is represented repeatedly, once for each iteration.

Here is sample code:

doptions = [[]]
dvals = [0.2997, 0.29969999999999997, 0.29969999999999997, 0.27372881355932194, 0.0225, 0.29969999999999997, 0.2997, 0.06457182320441988, 0.13706896551724135, 0.29969999999999997, 0.006211180124223601]
doptions[0] = dvals

indicator = 0
while indicator < 0.9:
    dminix = dvals.index(min(dvals))
    dvals[dminix] += 0.05
    doptions.append(dvals)
    indicator = min(dvals)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • you are actively changing the value in dvals when you do dvals[dminix] += .05. You need to store it as a different value and append that value to doptions. – Sam Jul 25 '22 at 20:38
  • TL;DR: Every element in `doptions` references a single list, `dvals`. | After doing `doptions[0] = dvals`, `dvals` and `doptions[0]` point to the same list in memory, and modifying `dvals` modifies `doptions[0]` as well, also, in the loop when you do `doptions.append(dvals)`, the new element in `doptions` is just another reference to the same list `dvals` and `doptions[0]` (and any element in `doptions` prior to that iteration) points to, so you get `doptions` containing simply multiple references to the same list, rather than multiple lists. – DjaouadNM Jul 25 '22 at 20:45
  • DjaouadNm: I can see how the link relates now, however I am still having trouble figuring out what I should do in my circumstance instead. Do you have any suggestions? – alaskanbullworm Jul 25 '22 at 20:53

2 Answers2

0

Try this:

doptions = [[]]
dvals = [0.2997, 0.29969999999999997, 0.29969999999999997, 0.27372881355932194, 0.0225, 0.29969999999999997, 0.2997, 0.06457182320441988, 0.13706896551724135, 0.29969999999999997, 0.006211180124223601]

# sorted lowest to highest
sorted_dvals = sorted(dvals)
# smallest value is index 0. Add 0.05 to smallest value
min_val = sorted_dvals[0] + 0.05
# append to end of dvals list
dvals.append(min_val)

doptions[0] = dvals
Jordan
  • 540
  • 4
  • 10
  • The order of the values is actually important as it pertains to the order of other lists so that I can use indexes to reference between the lists. – alaskanbullworm Jul 25 '22 at 20:48
  • That should be fine. dvals order is never changed. Only the copy of dvals (aka `sorted_dvals`) gets changed, so we can easily find the minimum value and then append the updated minimum value, to the original dvals list. – Jordan Jul 25 '22 at 20:51
  • Oh, I also see a misunderstanding: I need the minimum value updated in its current position in dval, (so if the 5th value is 0.1 i need it replaced with 0.15) and then append the entire list dval to a large list of lists doptions. apologies I missed that. – alaskanbullworm Jul 25 '22 at 20:55
0

I have explored the explanation presented by DJaouadNM and decided to employ the following fix, appending copies of the dvals variable instead of additional references to it.

import copy

doptions = [[]]
dvals = [0.2997, 0.29969999999999997, 0.29969999999999997, 0.27372881355932194, 0.0225, 0.29969999999999997, 0.2997, 0.06457182320441988, 0.13706896551724135, 0.29969999999999997, 0.006211180124223601]
doptions[0] = dvals

indicator = 0
while indicator < 0.9:
    dminix = dvals.index(min(dvals))
    dvals[dminix] += 0.05
    doptions.append(copy.copy(dvals))
    indicator = min(dvals)