0

I am trying to print a list of indices for elements that are common in both x and y. So in the example below [5,6,8,9,11] are common in each, so I am wanting my output to be a list of indices for common elements, so in this case, [0,1,3,4,6]. I tried the below code, but I just get an empty list [] as my output and I dont know where to go from here. Any help will be appreciated.

x= [5,6,7,8,9,10,11,12]
y=[5,6,8,9,11]

y=[]
for i in range(0,len(x)):
    if x[i] in y:
        y.append(i)
print(y)
Jacob
  • 103
  • 6
  • 2
    You're overriding `y`!! – ShlomiF Jun 24 '21 at 04:26
  • You can easily fix it by modifying with these changes; ```y = []``` to ```res = []``` and ```y.append(i)``` to ```res.append(i)```. – Rippyae Jun 24 '21 at 04:35
  • Does this answer your question? [Python: Finding corresponding indices for an intersection of two lists](https://stackoverflow.com/questions/12570417/python-finding-corresponding-indices-for-an-intersection-of-two-lists) – mpx Jun 24 '21 at 04:39
  • @balandongiv ya I saw that, but I didn't to create a dictionary, as there are more elegant solutions like the ones mentioned in the answers. – Jacob Jun 24 '21 at 04:44

3 Answers3

3

First off - you're overriding y. So you're comparing x to [].
Also, you're updating y as you go along, while comparing to it in your loop. That's error prone if I've ever seen it...

Next, why not use a simple list-comprehension?

indices_of_stuff_in_x_thats_also_in_y = [i for i, x_ in enumerate(x) if x_ in y]
print(indices_of_stuff_in_x_thats_also_in_y)
# [0, 1, 3, 4, 6]

One last comment - please note that your original definition kinda-sounds symmetric ("a list of indices for elements that are common in both x and y"). But it isn't and can't be, because you're talking about indices...

ShlomiF
  • 2,686
  • 1
  • 14
  • 19
1

You can use enumerate function to get the indices of the common elements.

You can use .index() but that can cause a problem because it fetches the index of the first appearance of the element provided.

Here is a working code.

list1 = [5,6,7,8,9,10,11,12]
list2 = [5,6,8,9,11]
list3=[j for j,i in enumerate(list1) if  i in list2]
print(list3)
-2

You must use decorators. This is Pythonic and you must write Pythonic code whenever possible:

import math
import operator
import functools

def filter(func):
    def _(f):
        @functools.wraps(f)
        def _(args):
            return f.__call__(list.__call__(__builtins__.filter.__call__(func, args)))
        return _
    return _

def transform(func):
    def _(f):
        @functools.wraps(f)
        def _(args):
            return f.__call__(func.__call__(args))
        return _
    return _

def map(func):
    def _(f):
        @functools.wraps(f)
        def _(args):
            return f.__call__(list.__call__(__builtins__.map.__call__(func, args)))
        return _
    return _

y = [5, 6, 8, 9, 11]

@transform(enumerate)
@filter(lambda v: y.__contains__(operator.itemgetter(int(round(math.pi.__mul__(pow(int(math.e).__mul__(int(math.pi + math.e)), int(math.pi.__add__(math.e)))), pow(hash(math.inf), next(v for k, v in __builtins__.__dict__.items.__call__() if k.__eq__(y.__class__.__name__)).__call__().__len__.__call__()).__neg__())).__sub__(hash(math.inf)))(v)))
@map(operator.itemgetter(hash(math.nan)))
def solve(v):
    return v
    
print(solve([5, 6, 7, 8, 9, 10, 11, 12]))
# Outputs [0, 1, 3, 4, 6]
enzo
  • 9,861
  • 3
  • 15
  • 38
  • I have updated my answer to be more readable. Maybe it's time to remove the downvotes now? – enzo Jun 24 '21 at 05:08