1

Here's the code:

ValuesDict = {}
PathwayTemplate = {}
GenerationDictionary = {0:[]}
StartNumbers = [2,3,5,7]
for Number in StartNumbers:
    PathwayTemplate.update({Number:0})
for Number in StartNumbers:
    ValuesDict.update({Number:[PathwayTemplate.copy()]})
    ValuesDict[Number][0][Number] += 1
    GenerationDictionary[0].append(Number)

Fin = 15

Gen = 0

while min(GenerationDictionary[Gen])<=Fin:
    Gen += 1
    GenerationDictionary.update({Gen:[]})
    for Number in GenerationDictionary[Gen-1]:
        for StartNumber in StartNumbers:
            if (Number+StartNumber) in ValuesDict.keys():
                pass
            else:
                ValuesDict.update({(Number+StartNumber):list(ValuesDict[Number])})
                for subpath in ValuesDict[(Number+StartNumber)]:
                    subpath[StartNumber]+=1
                GenerationDictionary[Gen].append((Number+StartNumber))
                print((Number+StartNumber),ValuesDict[(Number+StartNumber)])
    print()

And under first iteration it outputs:

4 [{2: 2, 3: 0, 5: 0, 7: 0}]
9 [{2: 2, 3: 0, 5: 0, 7: 1}]
6 [{2: 0, 3: 2, 5: 0, 7: 0}]
8 [{2: 0, 3: 2, 5: 1, 7: 0}]
10 [{2: 0, 3: 2, 5: 1, 7: 1}]
12 [{2: 0, 3: 0, 5: 1, 7: 1}]
14 [{2: 0, 3: 0, 5: 0, 7: 2}]

Where I expected it to give me one 2 and one 7 under 9.

It's meant to add numbers together and store number added as a result.

I tried reading about Python 3 saving memory and making a pointer type link when a=bso when a changes, so does b, but it'd appear that they are linked here despite being re-cast as a list. I've also tried checking if a is b and it gave me false, however it clearly seems to auto-update my original ValuesDict[Number].

Matt
  • 43
  • 4
  • 2
    Python doesn't have pointers nor casts. Python does not work like C/C++. Now, if you are coming from languages like that, it might help you to think of *all python variables* acting like PyObject pointers, which will get automagically dereferenced for you when you need it to, but that is really an implementation detail. You should read the following to understand Python's semantics in this regard: https://nedbatchelder.com/text/names.html – juanpa.arrivillaga Dec 14 '18 at 19:22
  • 2
    Now, you really should be providing a [mcve]. In any event, I am almost certain that the problem is that you require a *deep copy* somewhere, and instead, you are simply making a shallow copy when you do `list(some_list)`. Note again `list` is *not a cast*. There are *no pointers*. `list` is a type-constructer that takes an iterable and returns a list populated by that iterable. It will not copy the items in that iterable. So if you have a `a = [[1,2], [3,4]]; b = list(a)`, the `a[0] is b[0]` and modifying `a[0]` will be visible in `b[0]`, since the are the *same objects* – juanpa.arrivillaga Dec 14 '18 at 19:25
  • So, `import copy` and if I were a betting man, I'd say your issue would be fixed with replacing `list(ValuesDict[Number])` with `copy.deepcopy(ValuesDict[Number])`. So almost certainly a duplicate of [this question](https://stackoverflow.com/questions/8913026/list-copy-not-working). As an aside, *please* use standard Python styling, e.g. `snake_case` instead of `CapWords` (which in Python is reserved for class names). – juanpa.arrivillaga Dec 14 '18 at 19:27
  • Note, also, `some_dict.update({key:value})` is a *annoyingly* verbose and *needlessly inefficient* way of writing `some_dicy[key] = value`, note, using `update` makes you create and throw away a completely useless `dict` object. Don't do it. – juanpa.arrivillaga Dec 14 '18 at 19:30
  • @juanpa.arrivillaga this might be better as an answer than 4 continuous comments – G. Anderson Dec 14 '18 at 19:46
  • @G.Anderson I'm voting to close this question. – juanpa.arrivillaga Dec 14 '18 at 19:48
  • Deep copy was the way to go, thank you @juanpa.arrivillaga for pointing me in the direction of the older question. For any future references, I used deepcopy as advised. It turns out that when python makes a copy of a list through `some_list[:]`, it copies the list, but if it holds lists inside it, they aren't copied. Python creates pointers to those lists inside so they're affected. Long story short, read the second comment very carefully. Thanks again! – Matt Dec 17 '18 at 16:30
  • Mat, **python doesn't have pointers**. The distinction you are looking for is deep vs shallow copy. – juanpa.arrivillaga Dec 17 '18 at 17:42

0 Answers0