set-operations

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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 12:30:43
问题 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 elements also found in t s.intersection(t) s & t new set with elements common to s and t And so on, for all the standard relational algebra operations. So...question is, what exactly is the difference here? I see that it says that the update() versions returns

Trying to understand “except all” in sql query

别等时光非礼了梦想. 提交于 2019-12-10 03:45:03
问题 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 result different from the query below? (SELECT drinker FROM Frequents) EXCEPT (SELECT drinker FROM Likes); 回答1: The SQL EXCEPT operator takes the distinct rows of one query and returns the rows that do not appear in a second result set. The EXCEPT ALL

How to perform set subtraction on arrays in C#?

本秂侑毒 提交于 2019-12-10 01:11:43
问题 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", "four" }; string[] b = new string[] { "two", "four", "six" }; string[] c = a - b; // not valid c should equal { "one", "three" } . b - a would yield { "six" } . 回答1: If you're using Linq, you can use the Except operator like this: string [] c = a

Set operations in Appengine datastore

二次信任 提交于 2019-12-06 00:21:51
I assume there's no good way to do so, but if you had to, how would you implement set operations on Appengine's datastore? For example given two collections of integers, how would you store them in the datastore to get a good performance for intersect and except (all those items in A that are not in B) operations? There aren't any built in set operations in the datastore API. I see you having two options: For smallish sets (hundreds of items) You might get away with doing keys-only queries for both set A and set B, and doing the intersection in your application code. The precise definition of

Trying to understand “except all” in sql query

房东的猫 提交于 2019-12-05 04:26:19
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 result different from the query below? (SELECT drinker FROM Frequents) EXCEPT (SELECT drinker FROM Likes); The SQL EXCEPT operator takes the distinct rows of one query and returns the rows that do not appear in a second result set. The EXCEPT ALL operator does not remove duplicates. For purposes of row elimination and duplicate removal, the EXCEPT operator

How to perform set subtraction on arrays in C#?

眉间皱痕 提交于 2019-12-05 01:22:30
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", "four" }; string[] b = new string[] { "two", "four", "six" }; string[] c = a - b; // not valid c should equal { "one", "three" } . b - a would yield { "six" } . If you're using Linq, you can use the Except operator like this: string [] c = a.Except(b).ToArray(); Edit: CodeInChaos makes a good point. If a contains duplicates, it will remove any

How to efficiently set subtract a join table in PostgreSQL?

无人久伴 提交于 2019-12-04 22:36:37
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 table workers_skills - join table A worker can request the next appropriate free highest priority (whatever that means) unit of work to be assigned to her. Currently I have: SELECT work_units.* FROM work_units -- some joins WHERE NOT EXISTS ( SELECT skill_id FROM work_units_skills WHERE work_unit_id = work_units.id EXCEPT SELECT skill_id FROM workers_skills

How to find elements common in at least 2 vectors?

白昼怎懂夜的黑 提交于 2019-12-02 04:47:37
问题 Say I have 5 vectors: a <- c(1,2,3) b <- c(2,3,4) c <- c(1,2,5,8) d <- c(2,3,4,6) e <- c(2,7,8,9) I know I can calculate the intersection between all of them by using Reduce() together with intersect() , like this: Reduce(intersect, list(a, b, c, d, e)) [1] 2 But how can I find elements that are common in, say, at least 2 vectors? i.e.: [1] 1 2 3 4 8 回答1: It is much simpler than a lot of people are making it look. This should be very efficient. Put everything into a vector: x <- unlist(list(a

How to find elements common in at least 2 vectors?

最后都变了- 提交于 2019-12-02 01:31:51
Say I have 5 vectors: a <- c(1,2,3) b <- c(2,3,4) c <- c(1,2,5,8) d <- c(2,3,4,6) e <- c(2,7,8,9) I know I can calculate the intersection between all of them by using Reduce() together with intersect() , like this: Reduce(intersect, list(a, b, c, d, e)) [1] 2 But how can I find elements that are common in, say, at least 2 vectors? i.e.: [1] 1 2 3 4 8 It is much simpler than a lot of people are making it look. This should be very efficient. Put everything into a vector: x <- unlist(list(a, b, c, d, e)) Look for duplicates unique(x[duplicated(x)]) # [1] 2 3 1 4 8 and sort if needed. Note: In

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

元气小坏坏 提交于 2019-11-30 10:53:33
I have two collections of type ICollection<MyType> 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# (3.0)? Use Enumerable.Except and specifically the overload that accepts an IEqualityComparer<MyType> : var complement = c2.Except(c1, new MyTypeEqualityComparer()); Note that this produces the set difference and thus duplicates in c2 will only appear in the resulting IEnumerable<MyType> once. Here you need to implement IEqualityComparer<MyType> as