Say I have a list:
D = [str1, str2, str3, str4, str5, str6, ... str100]
And I want to search the list using [str4 str5]
as my key word and return the index of the last element of my keyword. In this case, I want to return 4
since str5
is the last element in my keyword and its index in the searched list, i.e. D
, is 4.
Is there any efficient way of doing this? I thought of using a for loop but that takes too much time, since I have a very big list. Is list comprehension a solution?
EDIT:
To answer Ben's question, I need to search [str4 str5]
as a set because I want to make sure these two strings are together. It is possibly that [str5]
can appear by itself in the future without str4
preceding it.
str4
is right beforestr5
. I want to search these two strings as a set. – qiaop Jul 18 '14 at 20:39