How to move the offset of the 'index' method to 'list'

I know that the index function works as follows:

list = ['dog','cat','pizza','trump', 'computer', 'trump'] print list.index('trump') 

the output will be 3. but now I want it to print another line of 'trump' that appears after two objects. but if I make the same command:

 print list.index('trump') 

he will print 3 again - the first trump card he sees. since I can move the "offset" of the index function, so it will detect another trump card in index 5? thank you guys!

+5
source share
6 answers

list.index takes a second start argument:

 >>> lst = ['dog','cat','pizza','trump', 'computer', 'trump'] >>> lst.index('trump') # 1st index 3 >>> lst.index('trump',4) # 2nd index 5 >>> lst.index('trump',lst.index('trump')+1) # 2nd index 5 

Or, if you need all the indexes, use a list comprehension:

 >>> [idx for idx,item in enumerate(lst) if item == 'trump'] [3, 5] 
+5
source

Notice the first index where the row is displayed, and the second optional index parameter:

 l = ['dog','cat','pizza','trump', 'computer', 'trump'] i = l.index("trump") print(i) print(l.index("trump",i+1)) 

I get:

 3 5 

from an embedded document, you can pass an optional start and stop value:

index (...)

L.index (value, [start, [stop]]) โ†’ integer - returns the first index of the value. Raises the value of ValueError if there is no value.

(in general, you need to protect your call to index with a try/except ValueError block if the value is not in the list and acts accordingly)

Note that start may be negative. If the value is negative, the index is calculated from the end of the list.

+5
source

If you need a list of all the indexes to write to your list, you can do something like this:

 indices = [i for i, x in enumerate(list) if x == "key"] 
+2
source

Create a shortened version of the list.

 list=['dog','cat','pizza','trump', 'computer', 'trump'] print list.index('trump') list = list[list.index('trump')+1:] print list.index('trump') 
+1
source
 l = ['dog','cat','pizza','trump', 'computer', 'trump'] i = [n for n, item in enumerate(l) if item == 'trump'] #find all indices # print the result for n in i: print n, l[n] 3 trump 5 trump 
+1
source

Try it ... it works like a charm.

Actually in your problem iteration is performed only once, even in for loop, it does not work (it does not work for me even after iter and next() ), so I tried with while , then I understand that this is an iteration game so put while in try using while 1: to start with iteration 1 , and the index method performs a linear search and stops at the first matching element. If no matching element is found, it throws a ValueError exception.

CODE

 list = ['dog','cat','pizza','trump', 'computer', 'trump'] value = "trump" i = -1 try: while 1: # Start with Iteration 1 never 0 i = list.index(value, i+1) print(value,"at", i) except ValueError: # No matching item is found. pass 

OUTPUT

 trump at 3 trump at 5 
0
source

Source: https://habr.com/ru/post/1268137/


All Articles