Hi assuming I have the following list:
words = ['maritus', 'et', 'quolibet', 'is', 'habitancium', 'dico', 'locum~locus', 'domus', 'totus', 'tempus', 'vitis', 'is', 'de', 'quolibet', 'ipse']
and the following word: locus
and I want to get the index of where it is found in words ie: 6 the following:
ind = words.index('locus')
will only work for whole words
Use next()
on a generator that yields each word in the words
list that locus
is in
. However, since you want the index, you should enumerate
the words
list and only yield the index, i
.
next(i for i, w in enumerate(words) if 'locus' in w)
#6
To get the indexes of all the occurrences, we can use a list-comprehension
:
[i for i, w in enumerate(words) if 'locus' in w]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.