array = ['3', 'pnwites', 'it', 'we', 'sy', 'nwjccoor,', '_', 'invoice', '°—', 'page:', '1', 'of', '1', 'nucor', 'steel', 'tuscaloosa,', 'inc.', 'please', 'make', 'checks', 'payable', 'and', 'mail', 'to:', 'invoice', 'date:', '12/29/2021', 'seenivoee]
variable = ['invoice', 'date:']
Is there any solution to get True whether the words 'invoice' and 'date:' closest to each other
CodePudding user response:
If you want to find whether the two values in variables
occur as sublist in array
(I'm not entirely sure whether that's your question), you could do
pairs = [
[array[i], array[i 1]]
for i in range(len(array)-1)
]
is_contained_as_sublist = variable in pairs
In fact, you don't even have to build the list of pairs
, but can use a generator expression:
is_contained_as_sublist = any(
[array[i], array[i 1]] == variable
for i in range(len(array)-1)
)