1

I wrote an if statement like:

if word in vocab: print word 

However, I also would like to know the matched word index in vocab.

Is there any way in Python can do this?

I just come up a solution that use vocab.index(word) to get the index, but this way go through the array twice (one in if statement, one call index()). I wonder there should be more efficiency method.

Thanks.

10
  • @sashkello not exactly the same, I would like to return the index using the 'in' keyword at the same time with if statement executing, and I also state the reason why I didn't choose the index() method. Commented Feb 3, 2014 at 22:35
  • 2
    @AbnerChou That is not possible, in returns only True or False. Also read about: EAFP Commented Feb 3, 2014 at 22:36
  • 1
    Suppose you had a way to do this. What are you going to do next with the information you have received? What are you really trying to do? Commented Feb 3, 2014 at 22:43
  • 1
    @AbnerChou The for-loop/enumerate based solution is going to be very very slow compared to .index and .find methods, and BTW you can easily get the value once you've the index. Commented Feb 3, 2014 at 22:44
  • 1
    @AbnerChou That question has multiple answers and they basically outline all you could possibly do. Commented Feb 3, 2014 at 22:45

4 Answers 4

6

To avoid one loop, you can use a try-except clause (without using if ... in ...:)

try: i = vocab.index(word) except ValueError as e: print e # Optional i = -1 
Sign up to request clarification or add additional context in comments.

Comments

2

This is a case where I wish I could do assignment in an if statement, but I can't so this is the solution:

If vocab is a list:

try: index = vocab.index(word) except ValueError: pass else: print index, word 

if vocab is a str:

index = vocab.find(word) if index >= 0: print index, word 

3 Comments

I assume it was because I used index and not find. One is for list, one is for str. I don't know what OP is using, but downvoter was expecting find (first version of answer only had index)
index works for strings just as well, just might not be as conveniant as find because of the exception. Anyway, +1 from me.
@tobias_k, thanks. I also preferred find for the reason of lack of exception. It's too bad there's no equivalent with a list.
2
def getindex(iterable,target): for idx,value in enumerate(iterable): if value==target: return idx return -1 

Note that this WILL NOT work if you're trying to find a substring in a string (as I'm assuming, since you're doing if word in vocab. In that case you really should do:

try: idx = iterable.index(target) except ValueError as e: # Not found # Handle it 

If you try to use the getindex function above, it will always return -1 for any value len(target) > 1 since it will break your iterable into its smallest iteration, which is by-letter for strings.

2 Comments

This is pretty much identical to the index function.
Sure is, but for some reason OP didn't seem to want to use iterable.index(target) so I thought I'd expose the enumerate function with a bit of an example.
1

You should have a look at pythons enumerate

EDIT: Following with an example:

for i, j in enumerate(vocab): if j == word: print (i, j) 

2 Comments

This doesn't really answer the question. Can you give an example of how enumerate would help? Simply referring to some external documentation isn't what this site is for.
Thanks, I know it work in 'for' statement, but in 'if' I got errors. Christian answer can solve my problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.