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
5
votes
1 answer

Numpy - group data into sum values

Say I have an array of values: a = np.array([1,5,4,2,4,3,1,2,4]) and three 'sum' values: b = 10 c = 9 d = 7 Is there a way to group the values in a into groups of sets where the values combine to equal b,c and d? For example: b: [5,2,3] c:…
Lee
  • 29,398
  • 28
  • 117
  • 170
5
votes
6 answers

More efficient way to count intersections?

I have a list of 300000 lists (fiber tracks), where each track is a list of (x,y,z) tuples/coordinates: tracks= [[(1,2,3),(3,2,4),...] [(4,2,1),(5,7,3),...] ... ] I also have a group of masks, where each mask is defined as a list of (x,y,z)…
jbrown
  • 374
  • 2
  • 9
5
votes
1 answer

How can I convert a LinkedHashSet to an EnumSet in Java?

I am trying to make a bean for converting LinkedHashSet into an EnumSet. This is required because Neo4j needs a conversion class for this situation. I have implemented the class so far as: public class LinkedHashSetToEnumSet implements…
Stephen D
  • 2,836
  • 4
  • 27
  • 40
5
votes
6 answers

Python - Remove a set of a list from another list

array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] array2 = [1, 2, 2, 2, 5, 6, 6, 6, 9] temp = set(array2) array1.remove(temp) Traceback (most recent call last): File "Sudoku V2.py", line 6, in array1.remove(temp) ValueError: list.remove(x): x…
DGDD
  • 1,370
  • 7
  • 19
  • 36
5
votes
4 answers

Creating a MySQL SET's from a string

Is there a way to create a set from a string of separated values in MySQL? For example: 'the,quick,brown,fox' => 'the','quick','brown','fox' A sort of inverse EXPORT_SET without the bit fiddeling. Regards
Turin
  • 129
  • 2
  • 10
5
votes
6 answers

Ranking Elements of multiple Lists by their count in Python

I want to rank multiple lists according to their elements how often they appear in each list. Example: list1 = 1,2,3,4 list2 = 4,5,6,7 list3 = 4,1,8,9 result = 4,1,2,3,4,5,6,7,8 (4 is counted three times, 1 two times and the rest once) I've tried…
Tom Siwik
  • 992
  • 9
  • 22
5
votes
2 answers

Why can I not move unique_ptr from a set to a function argument using an iterator?

I have a set of unique_ptr instances and want to pass all of them as an argument to a function. Example demonstrated by below code. #include #include #include using std::set; using std::unique_ptr; using std::vector; void…
Chris
  • 6,914
  • 5
  • 54
  • 80
5
votes
2 answers

What's the time complexity of std::lower_bound with std::set?

I know that there is std::set::lower_bound and the time complexity is O(log), and i see that std::lower_bound is much slower than std::set::lower_bound when operates on std::set. I googled and found…
iloahz
  • 4,491
  • 8
  • 23
  • 31
5
votes
2 answers

Set commands after an If command

Trying to make a quiz show.bat file and having trouble with the If-Set coding: :10 echo @: (Press any key to continue) pause >nul cls echo Bonus Question 1: echo Translate the letters in CAPS. echo @: Copy the sentence and translate the words in…
Batch Man
  • 75
  • 1
  • 6
5
votes
4 answers

Partition a list of sets by shared elements

Here's the jist of the problem: Given a list of sets, such as: [ (1,2,3), (5,2,6), (7,8,9), (6,12,13), (21,8,34), (19,20) ] Return a list of groups of the sets, such that sets that have a shared element are in the same group. [ [ (1,2,3), (5,2,6),…
itsadok
  • 28,822
  • 30
  • 126
  • 171
5
votes
4 answers

Iterating over sets of sets

I am attempting to write a program that iterates over a set of sets. In the example code below, I am getting an error that stating that iter.next() is of type object rather than a set of strings. I am having some other more mysterious issues with…
5
votes
1 answer

AMPL vs. Python - Importing tables (multi-dimensional dictionaries?)

I am an AMPL user trying to write a linear programming optimization model using Python (My first Python code). I am trying to find how to declare indexed parameters over compound sets. For example, in AMPL, i would say: Set A Set B Set…
5
votes
2 answers

Using multiple alternatives of hashCode() and equals() for sets

Suppose I have a simple POJO class Class1 , and it has 2 fields of type int. I've implemented the hashCode() and equals() methods of it to handle exactly those 2 fields, in order to put instances of the class into a set. So far so good. Now, I want…
android developer
  • 114,585
  • 152
  • 739
  • 1,270
5
votes
2 answers

What is the Time Complexity of size() for Sets in Java?

I know, it seems like a stupid question, you would expect that the time complexity of size() on any collection would be O(1) - but I'm finding that an "optimization" in my code which requires a call to size() is actually slowing things down. So,…
Zarjio
  • 1,097
  • 2
  • 12
  • 22
5
votes
3 answers

Select row after refreshing DBGrid

Well, some kind of n00b question from me. I've surfed the net and similar questions here but haven't found any right answers for such simple (as I thought) problem. I have a DBGrid. I select one row and make some actions with another data linked to…
Vlad
  • 337
  • 1
  • 7
  • 12