Questions tagged [set]

A set is a collection in which no element is repeated, which may be able to enumerate its elements according to an ordering criterion (an "ordered set") or retain no order (an "unordered set").

A set is a collection in which no element is repeated. It is often implemented by hashing the objects as they are added to the set, and comparing against those hashes for operations on the set.

In the C++ standard library in particular, the std::set is able to enumerate its elements according to a specific strict weak ordering criterion set on container construction. To achieve this, it is typically implemented by a binary tree. By contrast, the std::unordered_set stores unique elements in no particular order, and allows for fast retrieval of individual elements based on their value.

In Python, there are currently two built-in set types, set and frozenset. set is mutable, i.e. the contents can be changed and it has no hash value and cannot be used as either a dictionary key or as an element of another set. The frozenset type is immutable and hashable.

Common operations on sets:

  • add
  • remove
  • find (check membership)
  • union, intersection, difference

Resources

12019 questions
110
votes
3 answers

How can I add items to an empty set in python

I have the following procedure: def myProc(invIndex, keyWord): D={} for i in range(len(keyWord)): if keyWord[i] in invIndex.keys(): D.update(invIndex[query[i]]) return D But I am getting the following…
user2192774
  • 3,807
  • 17
  • 47
  • 62
109
votes
7 answers

Python: See if one set contains another entirely?

Is there a fast way to check if one set entirely contains another? Something like: >>>[1, 2, 3].containsAll([2, 1]) True >>>[1, 2, 3].containsAll([3, 5, 9]) False
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
107
votes
2 answers

Python, TypeError: unhashable type: 'list'

I'm receiving the following error in my program. The traceback: Traceback (most recent call last): File "C:\Python33\Archive\PythonGrafos\Alpha.py", line 126, in menugrafos() File "C:\Python33\Archive\PythonGrafos\Alpha.py", line 97, in…
Rex
  • 1,201
  • 5
  • 12
  • 11
106
votes
7 answers

What's the difference between HashSet and Set?

Saw the code snippet like Set instances = new HashSet(); I am wondering if Hashset is a special kind of set. Any difference between them?
user496949
  • 83,087
  • 147
  • 309
  • 426
105
votes
9 answers

Get unique values from ArrayList in Java

I have an ArrayList with a number of records and one column contains gas names as CO2 CH4 SO2, etc. Now I want to retrieve different gas names(unique) only without repeatation from the ArrayList. How can it be done?
SDas
  • 1,089
  • 2
  • 8
  • 5
104
votes
5 answers

Python remove set from set

According to my interpretation of Python 2.7.2 documentation for Built-In Types 5.7 Set Types, it should be possible to remove the elements of set A from set B by passing A to set.remove(elem) or set.discard(elem) From the documentation for…
cod3monk3y
  • 9,508
  • 6
  • 39
  • 54
103
votes
2 answers

How does a Python set([]) check if two objects are equal? What methods does an object need to define to customise this?

I need to create a 'container' object or class in Python, which keeps a record of other objects which I also define. One requirement of this container is that if two objects are deemed to be identical, one (either one) is removed. My first thought…
Ada
  • 1,746
  • 4
  • 15
  • 15
102
votes
5 answers

Opposite of set.intersection in python?

In Python you can use a.intersection(b) to find the items common to both sets. Is there a way to do the disjoint opposite version of this? Items that are not common to both a and b; the unique items in a unioned with the unique items in b?
user4847061
  • 1,223
  • 2
  • 9
  • 8
102
votes
1 answer

Possible Locations for Sequence/Picture Parameter Set(s) for H.264 Stream

I'm working on a H.264 Decoder and I'm wondering where to find the SPS and PPS. My reference literature tells me that those are NAL Units encoded in the H.264-Stream, but when I look into an example-MP4-File with IsoViewer, it says that the SPS and…
bananenbär
  • 1,125
  • 3
  • 8
  • 7
101
votes
3 answers

Python Set Comprehension

So I have these two problems for a homework assignment and I'm stuck on the second one. Use a Python Set Comprehension (Python's equivalent of Set Builder notation) to generate a set of all of the prime numbers that are less than 100. Recall that…
user3308790
  • 1,013
  • 2
  • 7
  • 5
100
votes
3 answers

Why is Scala's immutable Set not covariant in its type?

EDIT: Re-written this question based on original answer The scala.collection.immutable.Set class is not covariant in its type parameter. Why is this? import scala.collection.immutable._ def foo(s: Set[CharSequence]): Unit = { println(s) } def…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
98
votes
2 answers

How to convert Set to string with space?

I want to convert JavaScript Set to string with space. For example, if I have a set like: var foo = new Set(); foo.add('hello'); foo.add('world'); foo.add('JavaScript'); And I'd like to print the string from the set: hello world JavaScript (space…
KimchiMan
  • 4,836
  • 6
  • 35
  • 41
98
votes
1 answer

Why does tuple(set([1,"a","b","c","z","f"])) == tuple(set(["a","b","c","z","f",1])) 85% of the time with hash randomization enabled?

Given Zero Piraeus' answer to another question, we have that x = tuple(set([1, "a", "b", "c", "z", "f"])) y = tuple(set(["a", "b", "c", "z", "f", 1])) print(x == y) Prints True about 85% of the time with hash randomization enabled. Why 85%?
Veedrac
  • 58,273
  • 15
  • 112
  • 169
97
votes
8 answers

How to join entries in a set into one string?

Basically, I am trying to join together the entries in a set in order to output one string. I am trying to use syntax similar to the join function for lists. Here is my attempt: list = ["gathi-109","itcg-0932","mx1-35316"] set_1 = set(list) set_2 =…
Spencer
  • 21,348
  • 34
  • 85
  • 121
97
votes
3 answers

Difference between dict and set (python)

So, I know that this, a = {} # dict constructs an empty dictionary. Now, I also picked up that this, b = {1, 2, 3} # set creates a set. This can easily be verified, as, >>>print(type(a)) >>>print(type(b)) While I…
Nelewout
  • 6,281
  • 3
  • 29
  • 39