Questions tagged [set-operations]

Set operations as a branch of set theory which includes operations such as: Union, Intersection, Subset, Set Difference, Symmetrical Set Difference, and Set Equality.

References

88 questions
221
votes
4 answers

Union of dict objects in Python

How do you calculate the union of two dict objects in Python, where a (key, value) pair is present in the result iff key is in either dict (unless there are duplicates)? For example, the union of {'a' : 0, 'b' : 1} and {'c' : 2} is {'a' : 0, 'b' :…
Mechanical snail
  • 29,755
  • 14
  • 88
  • 113
121
votes
5 answers

Set operations (union, intersection) on Swift array?

Are there any standard library calls I can use to either perform set operations on two arrays, or implement such logic myself (ideally as functionally and also efficiently as possible)?
devios1
  • 36,899
  • 45
  • 162
  • 260
66
votes
6 answers

What does `**` mean in the expression `dict(d1, **d2)`?

I am intrigued by the following python expression: d3 = dict(d1, **d2) The task is to merge 2 dictionaries into a third one, and the above expression accomplishes the task just fine. I am interested in the ** operator and what exactly is it doing…
λ Jonas Gorauskas
  • 6,121
  • 6
  • 45
  • 66
51
votes
7 answers

Difference and intersection of two arrays containing objects

I have two arrays list1 and list2 which have objects with some properties; userId is the Id or unique property: list1 = [ { userId: 1234, userName: 'XYZ' }, { userId: 1235, userName: 'ABC' }, { userId: 1236, userName: 'IJKL' }, {…
Shashi
  • 1,112
  • 2
  • 17
  • 32
38
votes
3 answers

Difference between union() and update() in sets, and others?

Python sets have these methods: s.union(t) s | t new set with elements from both s and t s.update(t) s |= t return set s with elements added from t Likewise, there's also these: s.intersection_update(t) s &= t return set s keeping only…
temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
27
votes
3 answers

Quickest way to find the complement of two collections in C#

I have two collections of type ICollection called c1 and c2. I'd like to find the set of items that are in c2 that are not in c1, where the heuristic for equality is the Id property on MyType. What is the quickest way to perform this in C#…
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
20
votes
2 answers

How to perform set subtraction on arrays in C#?

What's the simplest way to perform a set subtraction given two arrays in C#? Apparently this is dead easy in Ruby. Basically I just want to remove the elements from array a that are in array b: string[] a = new string[] { "one", "two", "three",…
devios1
  • 36,899
  • 45
  • 162
  • 260
19
votes
6 answers

Union of intersecting vectors in a list in R

I have a list of vectors as follows. data <- list(v1=c("a", "b", "c"), v2=c("g", "h", "k"), v3=c("c", "d"), v4=c("n", "a"), v5=c("h", "i")) I am trying to achieve the following: Check whether any of the vectors intersect with each…
Crops
  • 5,024
  • 5
  • 38
  • 65
18
votes
9 answers

How to efficiently set subtract a join table in PostgreSQL?

I have the following tables: work_units - self explanatory workers - self explanatory skills - every work unit requires a number of skills if you want to work on it. Every worker is proficient in a number of skills. work_units_skills - join…
ndnenkov
  • 35,425
  • 9
  • 72
  • 104
16
votes
3 answers

Trying to understand "except all" in sql query

I came across this example and I don't understand what it means. (SELECT drinker FROM Frequents) EXCEPT ALL (SELECT drinker FROM Likes); relations: Frequents(drinker, bar), Likes(drinker, beer) What does the ALL do in this case? How is the…
8
votes
2 answers

Data structures for fast intersection operations?

Randomly select two sets ,both set contains distinct keys (one key may belongs to multiple sets ,one set can never contain duplicate keys ). Return a integer which represents for the number of keys belongs to both sets . For example…
6
votes
1 answer

Porting set operations from R's data frames to data tables: How to identify duplicated rows?

[Update 1: As Matthew Dowle noted, I'm using data.table version 1.6.7 on R-Forge, not CRAN. You won't see the same behavior with an earlier version of data.table.] As background: I am porting some little utility functions to do set operations on…
Iterator
  • 20,250
  • 12
  • 75
  • 111
6
votes
3 answers

Extract intersection list from upset object

I'm making some comparisons with UpSetR, and I'd like to save the lists of elements that fall into each intersection. Is this possible? I can't find it anywhere... It would be pretty tedious to do it manually (many lists), and since they're…
Matteo
  • 265
  • 5
  • 16
6
votes
1 answer

What are these set operations, and why do they give different results?

I had seen this test question on Pluralsight: Given these sets: x = {'a', 'b', 'c', 'd'} y = {'c', 'e', 'f'} z = {'a', 'g', 'h', 'i'} What is the value of x | y ^ z? The expected answer is: {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'} Combines…
CodeSpent
  • 1,684
  • 4
  • 23
  • 46
6
votes
3 answers

Numpy: find row-wise common element efficiently

Suppose we are given two 2D numpy arrays a and b with the same number of rows. Assume furthermore that we know that each row i of a and b has at most one element in common, though this element may occur multiple times. How can we find this element…
Samufi
  • 2,465
  • 3
  • 19
  • 43
1
2 3 4 5 6