0

Possible Duplicate:
Python two lists finding index value

listEx = ['cat *(Select: "Standard")*', 'dog', 'cow', 'turtle *(Select: "Standard")*', 'onion', 'carrot *(select: "Standard")*']
listEx2 = ['house', 'farm', 'cottage', 'cat', 'turtle', 'computer', 'carrot']

for i in listEx2:
    for j in ListEx:
        if i in j:
            print listEx.index(listEx2[i] in listEx[j])

Here is what I am attempting to do. For the items in listEx2, I want to find the index of the item in listEx, where the item in listEx2 was found in listEx. I would like the script to print out the index of the item in listEx with the name.

Thanks!

0

2 Answers 2

1

I might need some further clarification on this!

I think you're mixing things up a bit here.

When you do for i in listEx2:, in each iteration, i is the value of the position in the list, NOT the index!

If you just need the index where elements occur, then why not something like:

for i in listEx2:
    if i in listEx:
        print listEx.index(i)

This will print the index of matching ocurrences, you can concatenate the value itself, of course (i).

Sign up to request clarification or add additional context in comments.

Comments

0
>>> listEx = ['cat *(Select: "Standard")*', 'dog', 'cow', 'turtle *(Select: "Standard")*', 'onion', 'carrot *(select: "Standard")*']
>>> listEx2 = ['house', 'farm', 'cottage', 'cat', 'turtle', 'computer', 'carrot']
>>> for elem in listEx2:
        for i, tgt in enumerate(listEx):
                if elem in tgt:
                        print elem, 'in', tgt, 'at', i


cat in cat *(Select: "Standard")* at 0
turtle in turtle *(Select: "Standard")* at 3
carrot in carrot *(select: "Standard")* at 5

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.