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.

Josan
  • 692
  • 1
  • 5
  • 27
  • @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. – Josan Feb 03 '14 at 22:35
  • 2
    @AbnerChou That is not possible, `in` returns only True or False. Also read about: [EAFP](http://docs.python.org/2/glossary.html#term-eafp) – Ashwini Chaudhary Feb 03 '14 at 22:36
  • @Ashwini Chaudhary I saw use enumerate() in for statement could return both index and value, so I just would like to give it a try in if statement. – Josan Feb 03 '14 at 22:39
  • 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? – Karl Knechtel Feb 03 '14 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. – Ashwini Chaudhary Feb 03 '14 at 22:44
  • 1
    @AbnerChou That question has multiple answers and they basically outline all you could possibly do. – sashkello Feb 03 '14 at 22:45
  • @KarlKnechtel I just want to make it more efficiency. Suppose I have thousand words and I need to mapping them to another system, I need their index in both systems. However, if use 'in' and then call index() will cast too much time to do so. Why not there is a way that go through the list one time and return the indexes and matched values? The Christian answer is exactly what I want. – Josan Feb 03 '14 at 22:50
  • @sashkello thanks, I only look at the top answer. :) – Josan Feb 03 '14 at 22:52
  • "if use 'in' and then call index() will cast too much time to do so" you are over-thinking it. Unless you have a problem with performance, this is most likely not the place you would want to optimize. Have you profiled it? I'm sure the win is minuscule. – sashkello Feb 03 '14 at 22:59
  • @sashkello not affect the performance when my dataset is small. but now my dataset is about 60000 words and 3180000 tokens, already take hours to perform. I'm just try to increase the program speed. otherwise, I have to use C++ or Java. – Josan Feb 03 '14 at 23:09

4 Answers4

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
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
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
mhlester
  • 22,781
  • 10
  • 52
  • 75
  • 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`) – mhlester Feb 03 '14 at 22:29
  • `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 Feb 03 '14 at 22:32
  • @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. – mhlester Feb 03 '14 at 22:50
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.

Adam Smith
  • 52,157
  • 12
  • 73
  • 112
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)
shaktimaan
  • 11,962
  • 2
  • 29
  • 33
  • 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. – Bryan Oakley Feb 03 '14 at 22:29
  • Thanks, I know it work in 'for' statement, but in 'if' I got errors. Christian answer can solve my problem. – Josan Feb 03 '14 at 22:53