set-operations

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

∥☆過路亽.° 提交于 2019-11-29 16:08:17
问题 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)? 回答1: 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

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

那年仲夏 提交于 2019-11-29 02:27:11
[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 rows of a data frame or pairs of data frames (i.e. each row is an element in a set), e.g. unique - to create a set from a list, union, intersection, set difference, etc. These mimic Matlab's intersect(...,'rows') , setdiff(...,'rows') , etc., which don't appear to have counterparts in R (R's set operations are limited to vectors and lists, but not

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

别来无恙 提交于 2019-11-28 21:04:20
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 to the expression. I thought that ** was the power operator and haven't seen it used in the context above yet. The full snippet of code is this: >>> d1 = {'a': 1, 'b': 2} >>> d2 = {'c': 3, 'd': 4} >>> d3 = dict(d1, **d2) >>> print d3 {'a': 1, 'c': 3, 'b': 2, 'd': 4} ** in argument lists has a special meaning, as covered in section 4.7 of the tutorial

Union of dict objects in Python [duplicate]

∥☆過路亽.° 提交于 2019-11-28 18:30:19
This question already has an answer here: How to merge two dictionaries in a single expression? 41 answers 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' : 1, 'c' : 2} . Preferably you can do this without modifying either input dict . Example of where this is useful: Get a dict of all variables currently in scope and their values Mechanical snail This question provides an idiom. You use one of the

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

给你一囗甜甜゛ 提交于 2019-11-27 16:43:55
问题 [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 rows of a data frame or pairs of data frames (i.e. each row is an element in a set), e.g. unique - to create a set from a list, union, intersection, set difference, etc. These mimic Matlab's intersect(...,'rows') , setdiff(...,'rows') , etc., which

Union of dict objects in Python [duplicate]

三世轮回 提交于 2019-11-27 04:21:33
问题 This question already has an answer here: How do I merge two dictionaries in a single expression? 41 answers 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' : 1, 'c' : 2} . Preferably you can do this without modifying either input dict . Example of where this is useful: Get a dict of all variables

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

戏子无情 提交于 2019-11-27 01:45:09
问题 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 to the expression. I thought that ** was the power operator and haven't seen it used in the context above yet. The full snippet of code is this: >>> d1 = {'a': 1, 'b': 2} >>> d2 = {'c': 3, 'd': 4} >>> d3 = dict(d1, **d2) >>> print d3 {'a': 1, 'c': 3,

Difference and intersection of two arrays containing objects

*爱你&永不变心* 提交于 2019-11-26 22:25:24
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' }, { userId: 1237, userName: 'WXYZ' }, { userId: 1238, userName: 'LMNO' } ] list2 = [ { userId: 1235, userName: 'ABC' }, { userId: 1236, userName: 'IJKL' }, { userId: 1252, userName: 'AAAA' } ] I'm looking for an easy way to execute the following three operations: list1 operation list2 should return the intersection of elements: [ { userId: 1235, userName: 'ABC' }, {

Union of intersecting vectors in a list in R

£可爱£侵袭症+ 提交于 2019-11-26 21:14:31
问题 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 1) Check whether any of the vectors intersect with each other. 2) If intersecting vectors are found, get their union. So the desired output is out <- list(v1=c("a", "b", "c", "d", "n"), v2=c("g", "h", "k", "i")) I can get the union of a group of intersecting sets as follows. Reduce(union, list(data[[1]], data[[3]],

Set operations (union, intersection) on Swift array?

你离开我真会死。 提交于 2019-11-26 11:17:38
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)? Yes, Swift has the Set class. let array1 = ["a", "b", "c"] let array2 = ["a", "b", "d"] let set1:Set<String> = Set(array1) let set2:Set<String> = Set(array2) Swift 3.0+ can do operations on sets as: firstSet.union(secondSet)// Union of two sets firstSet.intersection(secondSet)// Intersection of two sets firstSet.symmetricDifference(secondSet)// exclusiveOr Swift 2.0 can calculate on array arguments: set1.union