开发者

Add a value to an element in a list of sets

开发者 https://www.devze.com 2022-12-28 15:43 出处:网络
I\'m using python, and I have a list of sets, constructed like this: list = [set([])]*n ...where n is the number of sets I want in the list. I want to add a value to a specific set in the list. Say

I'm using python, and I have a list of sets, constructed like this:

list = [set([])]*n

...where n is the number of sets I want in the list. I want to add a value to a specific set in the list. Say, the second set. I tried

list[1].add(value)

But this instead adds the value to each set in the list. This behaviour is pretty non-intuitive to me. Through further tests, I think I've found the problem: the list apparently contains 10 instances of the same set, or ten pointer开发者_JAVA技巧s to the same set, or something. Constructing the list through repeated calls of

list.append(set([]))

allowed me to use the syntax above to add elements to single sets. So my question is this: what exactly is going on in my first list-construction technique? It is clear I don't understand the syntax so well. Also, is there a better way to intialize an n-element list? I've been using this syntax for a while and this is my first problem with it.


You've pretty much summarized the problem yourself -- the X*n syntax makes one instance of X and includes it n times. It's not a problem for things like 'a'*10 because it doesn't matter if every character in that string happens to point to the same 'a', but it does for mutable constructions like lists and sets. You can make n separate sets using a list comprehension:

list = [set() for x in xrange(n)]


Yes, that is correct. The * syntax is simply copying the reference that many times. Your method works fine, or you can use a list comprehension to construct that many sets as in:

list = [set([]) for x in xrange(n)];
0

精彩评论

暂无评论...
验证码 换一张
取 消