intersection

Python list intersection with non unique items

↘锁芯ラ 提交于 2019-11-28 09:54:10
I have two strings and I would like to have the intersection on them including duplicate items: str_a = "aabbcc" str_b = "aabd" list(set(str_a) & set(str_b)) >> "ab" I would like to have it return: >> "aab" Any ideas? ninjagecko Multisets are implemented in python 2.7 or later as (mutable) Counter objects. You can perform many of the same operations as you can for sets, such as union, intersection, difference (though counts can become negative), etc.: from collections import Counter as mset Solution: (mset("aabbcc") & mset("aabd")).elements() More details: >>> intersection = mset("aabbcc") &

Intersection of two string array (ignore case)

☆樱花仙子☆ 提交于 2019-11-28 09:35:33
I have two arrays: string[] array1 = { "Red", "blue", "green", "black" }; string[] array2 = { "BlUe", "yellow", "black" }; I need only the matching strings in one array (ignoring case). Result should be: string[] result = { "blue", "black" } or { "BlUe", "black" }; How about an Enumerable.Intersect and StringComparer combo: // other options include StringComparer.CurrentCultureIgnoreCase // or StringComparer.InvariantCultureIgnoreCase var results = array1.Intersect(array2, StringComparer.OrdinalIgnoreCase); 来源: https://stackoverflow.com/questions/10323071/intersection-of-two-string-array

Common elements of vectors with multiple elements

馋奶兔 提交于 2019-11-28 09:22:52
问题 How to efficiently find common elements of two vectors with duplicate elements? Example: v1 <- c(1, 1, 2, 3, 3, 4) v2 <- c(1, 1, 1, 3, 4, 5) commonElements <- c(1, 1, 3, 4) intersect doesn't handle duplicate elements well. 回答1: I like intersect and table s, so... tv1 <- table(v1) tv2 <- table(v2) comvals <- intersect(names(tv1),names(tv2)) comtab <- apply(rbind(tv1[comvals],tv2[comvals]),2,min) The information is still there, but in (what I view as) a nicer format: > comtab 1 3 4 2 1 1 EDIT:

Java: Is there an easy, quick way to AND, OR, or XOR together sets?

谁说胖子不能爱 提交于 2019-11-28 09:02:42
That is, if I had two or more sets, and I wanted to return a new set containing either: All of the elements each set has in common (AND). All of the elements total of each set (OR). All of the elements unique to each set. (XOR). Is there an easy, pre-existing way to do that? Edit: That's the wrong terminology, isn't it? Assuming 2 Set objects a and b AND(intersection of two sets) a.retainAll(b); OR(union of two sets) a.addAll(b); XOR either roll your own loop: foreach item if(a.contains(item) and !b.contains(item) || (!a.contains(item) and b.contains(item))) c.add(item) or do this: c.addAll(a)

Trying to optimize line vs cylinder intersection

我的未来我决定 提交于 2019-11-28 08:50:41
My brain has been melting over a line segment-vs-cylinder intersection routine I've been working on. /// Line segment VS <cylinder> // - cylinder (A, B, r) (start point, end point, radius) // - line has starting point (x0, y0, z0) and ending point (x0+ux, y0+uy, z0+uz) ((ux, uy, uz) is "direction") // => start = (x0, y0, z0) // dir = (ux, uy, uz) // A // B // r // optimize? (= don't care for t > 1) // <= t = "time" of intersection // norm = surface normal of intersection point void CollisionExecuter::cylinderVSline(const Ogre::Vector3& start, const Ogre::Vector3& dir, const Ogre::Vector3& A,

Distance from point to line on Earth

自古美人都是妖i 提交于 2019-11-28 08:50:23
I need something as simple as "Subject 1.02: How do I find the distance from a point to a line?" But that works with Lon/Lat. We'll start with some assumptions: We'll model the figure of the earth as an oblate spheroid . By "line" we mean the minor arc of a great circle . We are looking for a highly accurate solution (for the suggested model) So, rephrasing your question: Given three earth-surface points - p0, p1 and p2, find an earth-surface point on the minor arc of the great circle defined by p1 and p2, which is the closest to p0. As an infrastructure for the solution, We'll need: An

Detect if line segment intersects square

别等时光非礼了梦想. 提交于 2019-11-28 08:28:53
问题 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 回答1: 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,

Take the intersection of an arbitrary number of lists in python

左心房为你撑大大i 提交于 2019-11-28 08:17:32
问题 Suppose I have a list of lists of elements which are all the same (i'll use int s in this example) [range(100)[::4], range(100)[::3], range(100)[::2], range(100)[::1]] What would be a nice and/or efficient way to take the intersection of these lists (so you would get every element that is in each of the lists)? For the example that would be: [0, 12, 24, 36, 48, 60, 72, 84, 96] 回答1: Use sets, which have an intersection method. >>> s = set() >>> s.add(4) >>> s.add(5) >>> s set([4, 5]) >>> t =

Objective-C check if subviews of rotated UIViews intersect?

你说的曾经没有我的故事 提交于 2019-11-28 06:35:28
I don't know where to start with this one. Obviously CGRectIntersectsRect will not work in this case, and you'll see why. I have a subclass of UIView that has a UIImageView inside it that is placed in the exact center of the UIView: I then rotate the custom UIView to maintain the frame of the inner UIImageView while still being able to perform a CGAffineRotation. The resulting frame looks something like this: I need to prevent users from making these UIImageViews intersect, but I have no idea how to check intersection between the two UIImageViews, since not only do their frames not apply to

Java method to find the rectangle that is the intersection of two rectangles using only left bottom point, width and height?

我是研究僧i 提交于 2019-11-28 05:06:41
I have found the solution but wanted to ensure my logic is the most efficient. I feel that there is a better way. I have the (x,y) coordinate of the bottom left corner, height and width of 2 rectangles, and i need to return a third rectangle that is their intersection. I do not want to post the code as i feel it is cheating. I figure out which is furthest left and highest on the graph. I check if one completely overlaps the other, and reverse to see if the other completely overlaps the first on the X axis. I check for partial intersection on the X axis. I basically repeat steps 2 and 3 for the