473,322 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Sets Module Bug?

>>> from sets import *
Set((1,2,3)) Set([1, 2, 3]) Set([1,2,3]) Set([1, 2, 3]) Set(([1,2,3],[4,5,6]))

Traceback (most recent call last):
File "<input>", line 1, in ?
File "sets.py", line 399, in __init__
self._update(iterable)
File "sets.py", line 344, in _update
data[element] = value
TypeError: list objects are unhashable

How can a Set using a tuple or list be ok, but not a tuple of lists?
Jul 18 '05 #1
3 1551
Chris S. wrote:
>>> from sets import *
>>> Set((1,2,3)) Set([1, 2, 3]) >>> Set([1,2,3]) Set([1, 2, 3]) >>> Set(([1,2,3],[4,5,6]))

Traceback (most recent call last):
File "<input>", line 1, in ?
File "sets.py", line 399, in __init__
self._update(iterable)
File "sets.py", line 344, in _update
data[element] = value
TypeError: list objects are unhashable

How can a Set using a tuple or list be ok, but not a tuple of lists?


Because "list objects are unhashable", as Python told you :-)
AFAIK the sets module is built upon the dict type, it uses (requires)
the elements of the set to be hashable for efficient containment tests.
The library reference of the sets module also says:
"All of the elements in iterable should be immutable or be transformable to an immutable
using the protocol described in section 5.13.3. "

--Irmen
Jul 18 '05 #2
Dnia Mon, 02 Aug 2004 05:25:50 -0400, Chris S. napisał(a):
How can a Set using a tuple or list be ok, but not a tuple of lists?


Because tuples can't be changed once they're constructed (= immutable.)
You'd get the same error if you tried to do
d = {[1,2]: 'foo'}


Traceback (most recent call last):
File "<pyshell#1>", line 1, in -toplevel-
d = {[1,2]: 'foo'}
TypeError: list objects are unhashable

--
Marek Baczyński :: UIN 57114871 :: GG 161671 :: JID im******@jabber.gda.pl
He who knows best best knows how little he knows. -- Thomas Jefferson
Jul 18 '05 #3
> >>> Set((1,2,3))
[...]
How can a Set using a tuple or list be ok, but not a tuple of lists?


I think you are confused by the syntax above. This expression constructs
a Set object, and fills it with the members of the tuple. In no way does
the Set "use" the tuple - once the Set is created, the tuple goes away.

So you should have asked "How can a Set containing numbers be ok, but
not a Set containing lists?", to which the other two posters gave the
right answer.

Regards,
Martin
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Raymond Hettinger | last post by:
I've gotten lots of feedback on the itertools module but have not heard a peep about the new sets module. * Are you overjoyed/outraged by the choice of | and & as set operators (instead of + and...
5
by: Raymond Hettinger | last post by:
For Py2.4, I'm working on a C implementation of Sets.py with the possibility of having a set() type as a builtin. There is a question as whether the current design for set of sets has been useful....
1
by: Gerrit Holl | last post by:
Brett C. wrote: > Sets now at blazing C speeds! > ----------------------------- > Raymond Hettinger implemented the sets API in C! The new built-ins are > set (which replaces sets.Set) and...
0
by: Dave Benjamin | last post by:
Hola, I made a backport of sets.py that will run on Jython 2.1. Here is a diff against the Python 2.3 version of sets.py. The changes were simple, but I may have made a mistake here or there,...
7
by: Steve | last post by:
This post has two parts. First is my feedback on sets. (Hello? Last summer called, they want their discussion thread back...) Second is some questions about my implementation of a partition...
6
by: kpp9c | last post by:
I have a question... and ... whew ... i am gonna be honest, i haven't the slightest clue how to even start ... i am not sure if i used up all my good will here or can take a mulligan.. i love to...
2
by: mkppk | last post by:
I have kind of strange change I'd like to make to the sets.Set() intersection() method.. Normally, intersection would return items in both s1 and s2 like with something like this: ...
11
by: Prateek | last post by:
I have 3 variable length lists of sets. I need to find the common elements in each list (across sets) really really quickly. Here is some sample code: # Doesn't make sense to union the sets -...
17
by: Larry Bates | last post by:
You can do the following: a = del a and a = {1:'1', 2: '2', 3: '3', 4:'4', 5:'5'} del a
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.