Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sets are unhashable
#1
why are sets unhashable?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Probably because sets are mutable. You need immutable objects.

simple solution is to wrap it with a tuple.
a[tuple(set([1,1,4,4,4,4,5,]))] = 1
99 percent of computer problems exists between chair and keyboard.
Reply
#3
The simple solution is to use a frozenset.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
so how can i get any one element from a frozenset? with a mutable set i would pop one and put it back in. but i see no methods to get something out of a frozenset. i guess i just have to make it into a regular set or a tuple.

  some_arbitrary_element = tuple(my_frozen_set)[0]
assuming it isn't empty.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
Why do you want to get a random element of something you're using as a key?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(Aug-08-2018, 01:33 AM)Skaperen Wrote: so how can i get any one element from a frozenset? with a mutable set i would pop one and put it back in.
Just do next(iter(any_iterable)). Works for set, frozen set, tuple, list, etc. Please don't use mutations to achieve a result which doesn't need it, god kills puppies when you do stuff like that.
Reply
#7
i'm not using it as a key per se, but using the set as a non-duplicating collection. get an element out is to check what types the set probably has (to avoid a full test of everything if the random item shows it is not needed)
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  iterating sets and frozensets Skaperen 0 1,236 Nov-22-2019, 01:47 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020