-1

In the code:

    import numpy as np
    array_3_sets = np.repeat(set(), 3)
    print(array_3_sets)
    array_3_sets[0].add(0)
    array_3_sets[1].add(1)
    print(array_3_sets)

I get this output:

    [set() set() set()]
    [{0, 1} {0, 1} {0, 1}]

Instead of getting:

    [set() set() set()]
    [{0} {1} set()]

as I would expect. When I add an element to one of the three sets, it is added to all of them, as if the three sets were one same set. I can't understand why this happens.

  • 1
    Because you are creating exactly ONE `set` object, and storing three references to that set. The same thing happens if you say `[set()]*3`. – Tim Roberts Jun 14 '22 at 23:16
  • It repeats the reference to the object you created. There are many dupes to that in SO – rafaelc Jun 14 '22 at 23:16
  • " as if the three sets were one same set. I can't understand why this happens." I don't understand why there is confusion. It happens because the three sets *actually are* the same set, listed three times. If the question is "why do I get this result from the `np.repeat` code?", then the answer is "because that is how `np.repeat` is defined to work; please try to use documentation to learn this kind of thing. If the question is "why is `np.repeat` defined to work that way?", then this is not an answerable question in this format; we don't speculate about design decisions. – Karl Knechtel Jun 14 '22 at 23:23
  • What's the purpose of storing sets inside a numpy array? You lose out on all the benefits of numpy by storing your data this way... – ddejohn Jun 14 '22 at 23:42

1 Answers1

2

The repeat() function creates multiple references to the same object.

What is needed your case is to create an array of distinct sets:

>>> array_3_sets = np.array([set() for i in range(3)])
>>> array_3_sets[0].add(0)
>>> array_3_sets[1].add(1)
>>> print(array_3_sets)
[{0} {1} set()]
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485