intersection

Is there a way to get the difference and intersection of tuples or lists in Python? [duplicate]

喜欢而已 提交于 2019-11-30 05:59:58
问题 This question already has answers here : Find intersection of two nested lists? (19 answers) Closed 5 years ago . If I have lists: a = [1, 2, 3, 4, 5] b = [4, 5, 6, 7, 8] c = a * b should give me: c = [4, 5] and c = a - b should give me: c = [1, 2, 3] Is this available for Python or do I have to write it myself? Would the same work for tuples? I will likely use lists as I will be adding them, but just wondering. 回答1: If the order doesn't matter, you can use set for this. It has intersection

How can I tell if two polygons intersect?

大兔子大兔子 提交于 2019-11-30 03:43:50
问题 Imagine I have the coordinate of 4 points that form a polygon. These points are represented using PointF in C#. If I have 2 polygons (using 8 points), how can I tell if they intersect? Rectangle class has a method called IntersectsWith but I couldn't find something similar for GraphicsPath or Region. Any advice would be greatly appreciated. Mosh 回答1: As Charlie already pointed out you can use the Separating Axis theorem. Check out this article for a C# implementation and example of polygon

Best way to get intersection of keys of two objects?

巧了我就是萌 提交于 2019-11-30 03:01:04
I have two object literals like so: var firstObject = { x: 0, y: 1, z: 2, a: 10, b: 20, e: 30 } var secondObject = { x: 0, y: 1, z: 2, a: 10, c: 20, d: 30 } I want to get the intersection of the keys these two object literals have like so: var intersectionKeys = ['x', 'y', 'z', 'a'] I can obviously do a loop and see if a key with the same name exists in the other object, but I am wondering if this would be a good case for some functional programming and map / filter / reduce usage? I myself have not done that much functional programming, but I have a feeling, that there could exist a clean and

Fast intersection of sets: C++ vs C#

余生长醉 提交于 2019-11-30 02:26:11
On my machine (Quad core, 8gb ram), running Vista x64 Business, with Visual Studio 2008 SP1, I am trying to intersect two sets of numbers very quickly. I've implemented two approaches in C++, and one in C#. The C# approach is faster so far, I'd like to improve the C++ approach so its faster than C#, which I expect C++ can do. Here is the C# output: (Release build) Found the intersection 1000 times, in 4741.407 ms Here is the initial C++ output, for two different approaches (Release x64 build): Found the intersection (using unordered_map) 1000 times, in 21580.7ms Found the intersection (using

C# fastest intersection of 2 sets of sorted numbers

会有一股神秘感。 提交于 2019-11-30 02:12:45
I'm calculating intersection of 2 sets of sorted numbers in a time-critical part of my application. This calculation is the biggest bottleneck of the whole application so I need to speed it up. I've tried a bunch of simple options and am currently using this: foreach (var index in firstSet) { if (secondSet.BinarySearch(index) < 0) continue; //do stuff } Both firstSet and secondSet are of type List. I've also tried using LINQ: var intersection = firstSet.Where(t => secondSet.BinarySearch(t) >= 0).ToList(); and then looping through intersection . But as both of these sets are sorted I feel there

How can I get the intersection, union, and subset of arrays in Ruby?

这一生的挚爱 提交于 2019-11-29 19:38:18
I want to create different methods for a class called Multiset . I have all the required methods, but I'm unsure of how to write intersection, union, and subset methods. For intersection and union, my code starts like this: def intersect(var) x = Multiset.new end Here is an example: X = [1, 1, 2, 4] Y = [1, 2, 2, 2] then the intersection of X and Y is [1, 2] . Mike Lewis Utilizing the fact that you can do set operations on arrays by doing & (intersection), - (difference), and | (union). Obviously I didn't implement the MultiSet to spec, but this should get you started: class MultiSet attr

Edge crossing reduction in graph

我的未来我决定 提交于 2019-11-29 19:05:28
问题 I'd like to ask you if there are any algorithms how to minimize edge crossings in graph, for example if I have a transition matrix of the graph. I found methods like trying to place the nodes around the other node, but I'd like to know some other ideas. Thanks. 回答1: There's a range of well established algorithms/libraries that have been developed for graph drawing applications, you can get a bit of background here. To draw undirected graphs a popular choice is the force-based layout algorithm

Find cells in array that are crossed by a given line segment

别说谁变了你拦得住时间么 提交于 2019-11-29 18:02:19
I have got a 2D array of cells with size 10x10, and many points that are pairs of floating point values, like: (1.6, 1.54), (4.53, 3.23). The pairs (x,y) are such that x<10 and y<10 Each cell takes points whose coordinates have the same integer part as the cell coordinates. So arr[3][7] will take points with x={3...3.99(9)} and y={7... 7.99(9)}, for example (3.5, 7.1) or (3.2, 7.6). Similarly (1.6, 1.54) is in arr[1][1] , (4.53, 3.23) is in arr[4][3], etc. Each point has got a designated place in the array that is easy to find, because I just have to cast x and y to int to get rid of the

Python intersection and difference sum doesn't give me the actual number of the original set

亡梦爱人 提交于 2019-11-29 16:34:22
I have two lists, one with old IDs and one with new IDs. I want to get items in common and items not common. The new_Items list has all new ones. The old_Items has the old ones. I suppose that when I calculate the ones in common plus the in new items list but not in old items list, I get the actual number of new items. Here is the code and the output. print(old_Items) print(new_Items) common = set(new_Items) & set(old_Items) not_common = set(new_Items) - set(old_Items) print(len(old_Items)) print(len(new_Items)) print(len(common)) print(len(not_common)) output ['312064913440', '312062038159',

Detect if line segment intersects square

本小妞迷上赌 提交于 2019-11-29 14:48:58
Anyone have a simple algorithm for this? No need for rotation or anything. Just finding if a line segment made from two points intersects a square Eric This code should do the trick. It checks where the line intersects the sides, then checks if that is within the width of the square. The number of intesections is returned. float CalcY(float xval, float x0, float y0, float x1, float y1) { if(x1 == x0) return NaN; return y0 + (xval - x0)*(y1 - y0)/(x1 - x0); } float CalcX(float yval, float x0, float y0, float x1, float y1) { if(x1 == x0) return NaN; return x0 + (yval - y0)*(y1 - y0)/(x1 - x0); }