fastest method to get indices of first and last common elements between lists

python find common elements in multiple lists
find common elements in one list python
python check if two lists have common elements
find common elements in two lists python
remove common elements from two lists python
find common values in two lists python
find common elements in two arrays
python select list elements by index

I have two sorted lists

x = [-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10]
y = [3,4,5,6]

Between these x and y I would like to return imin = (6,0) and imax = (9,3). If these lists do not share any elements, I would like to return imin = None and imax = None.

A solution is

def inds(x,y):
    arr = [(i,j) for i,xx in enumerate(x) for j,yy in enumerate(y) if xx==yy ]
    if arr!=[]: # to obtain proper None output
        imin = (min(i for i,_ in arr), min(j for _,j in arr))
        imax = (max(i for i,_ in arr), max(j for _,j in arr))
    else: 
        imin = None
        imax = None 
    return (imin,imax)

This does a lot of unnecessary computation (O(n**2)) and is a bottleneck of one of my programs. Can anyone suggest something faster?

ADDITIONAL (NON-MINIMAL EXAMPLE) INFO

If it helps, I actually have a list of objects.

objects = [(A1,B1),(A2,B2)] 

x and y would be attributes of each element of this objects list like so:

x = objects[0][0].attrib
y = objects[0][1].attrib

and I actually want to generate

[(imin1,imax1),(imin2,imax2)]

Which could come, for example, from

def attribs(A,B):
    return (A.attrib,B.attrib)

[inds(*attribs(*v)) for v in objects]

note: I added a numpy tag just because I'm open to using numpy for this if it's faster.

This should be what you are after

c = set(x).intersection(y)  # O(n) time
def get_first(l):
    return next((idx for idx, elm in enumerate(l) if elm in c), None)  # O(n) time
imin = (get_first(x), get_first(y))
imax = (len(x) - get_first(x[::-1]) - 1, len(y) - get_first(y[::-1]) - 1)

From here onwards you can do a few tweaks but it will still run O(n)

Fast algorithm for finding common elements of two sorted lists , I have two sorted lists x = [-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10] y = [3,4,5,6]. Between these x and y I would like to return imin = (6,0) and imax = (9,3) . Python | Get first and last elements of a list. Sometimes, there might be a need to get the range between which a number lies in the list, for such applications we require to get the first and last element of the list.

Using np.intersect1d and returning the indices, you can do the following

idxes = np.stack(np.intersect1d(x,y, return_indices=True)[1:])
ix = tuple(idxes[:,0])
iy = tuple(idxes[:,-1])

>>> ix
(6, 0)
>>> iy
(9, 3)

Explanation

idxes is a 2d array of the indices where there are intersections between your two arrays:

>>> idxes
array([[6, 7, 8, 9],
       [0, 1, 2, 3]])

So you can just take the first and last using

ix = tuple(idxes[:,0])
iy = tuple(idxes[:,-1])

Python, The naive way requires O(N^2) time, correct. Sorting the lists first takes O(N log N​) time, assuming you use a good algorithm (bubblesort is O(N^2)), and the rest  Copy to clipboard. #N#Pick out elements 5 through 8 of the list: Copy to clipboard. #N#Pick the last 7 elements (negative indices count from the end): Copy to clipboard. #N#A matrix in the Wolfram Language is a list of lists of the same length. You can pick out elements from a matrix just like you would from a list.

You can also sort the intersection list and use .index() to find out the indices.

z = list(set(x).intersection(set(y))) # O(n)
z.sort() # O(nlogn)

imin = (x.index(z[0]), y.index(z[0])) # O(n)
imax = (x.index(z[-1]), y.index(z[-1])) # O(n)

Programming in Python 3: A Complete Introduction to the Python , Given two lists, print all the common element of two lists. 3, 4, 5] list2 = [6, 7, 8, 9] Output : No common elements Explanation: They do not have any elements in common in between them. Recommended: Please try your approach on {IDE} first, before moving on to the solution. Method 1:Using Set's intersection property. Approach 2: Binary Search. Intuition. Because the array is sorted, we can use binary search to locate the left and rightmost indices. Algorithm. The overall algorithm works fairly similarly to the linear scan approach, except for the subroutine used to find the left and rightmost indices themselves.

Python, OrderedDicts have the same API as normal dicts, although when iterated the items are from first to last inserted), and the popitem() method always returns the most The Counter class is a dict subclass used to provide a fast and easy way of elements or a list of the most common elements as (element, count) 2-​tuples. The resulting indices I need in this case will give array(a)[indices] = b. I've edited the question. maybe the description there is clearer. – user1342516 Apr 30 '12 at 14:25 You should open your second case up as a new question as nobody will see it here. – jamylak Apr 30 '12 at 14:27

11. Lists, Accessing an element from its index is easier task in python, just using the [] operator This task is easy to perform with a loop, and hence shorthand for it is the first method to start with this task. Iterating over the index list to get the corresponding elements from list into new list is This is quite quick way to perform this task. The previous answers all work to find the unique common elements, but will fail to account for repeated items in the lists. If you want the common elements to appear in the same number as they are found in common on the lists, you can use the following one-liner: l2, common = l2[:], [ e for e in l1 if e in l2 and (l2.pop(l2.index(e)) or True)]

Nine Algorithms That Changed the Future: The Ingenious Ideas That , The values that make up a list are called its elements, or its items. It is common to use a loop variable as a list index. the list, so the first element of fruit has been changed from "banana" to "pear", and the last from "quince" to "orange". There is some evidence that programs that use pure functions are faster to develop  Method 1:Using Set’s & property. Convert the lists to sets and then print set1&set2. set1&set2 returns the common elements set, where set1 is the list1 and set2 is the list2. Below is the Python3 implementation of the above approach: # Python program to find the common elements.

Comments
  • Are those two lists guaranteed to be sorted?
  • yep, they are always sorted
  • How is iy = (9,3) ? there are not 9 items in y Shouldn't iy = (0, 3) Also shouldn't ix = (6, 9) not (6, 0) ?
  • sorry, naming is a little weird. iy is actually the indices of the max elements in x and y respectively. I will edit
  • ah ok I'll delete my answer and adjust
  • 106 times speedup on a system size of interest :)
  • Note that this is a very new function in numpy and only in versions 1.15.0 and higher
  • @sacul my np.intersect1d() doesn't have a return_indices kwarg. Any idea about this? I have numpy 1.13.3
  • I'd be very surprised if it wasn't. Sometimes you can't really be sure with numpy though. Best way is to run for array lengths of 10, 100, 1000, 10000 and plot the log gradient
  • Yeah, I'm not sure how to check the time complexity of numpy functions, but @AlexanderMcFarlane's solution of plotting should give a good hint
  • Tbh I think they might be nlog(n) because I saw in the source there is a sort algorithm here github.com/numpy/numpy/blob/v1.15.0/numpy/lib/…