Just learning Python as my first coding language. Given a list with many possible sub-lists which have a variable number of elements, is there a way of using regex (or something similar) to identify which lists contain sub-lists with 1) the number of elements specified and 2) a given type of content in a certain order (including other sub-lists)? For example (in pseudocode):
list1 = [1, 4, 7, ["a", 5, "b"], 2, 4,7,"k",9]
list2 = [1, 4, 7, ["a", "h", "b"], 2]
list3 = [1, 4, 7, ["a", ["a", 6, "b"], "b"], 5, 3]
list4 = [1, 4, 7, ["a", "b"], 3, 4]
list5 = [1, 4, 7, ["a", 5, "b", 7], 3, 4]
if ["a", ., "b"] in listx: # where "." stands for anything, even sub-lists
print("yes")
else:
print("no")
list1, list2, and list3 should print "yes", but list4 and list5 should print "no".
As a bonus, is there a way to return 1) the number of times that the specified generic sub-list is found and 2) where? For example, have list3 return "There are 2 ["a", ., "b"] sub-lists, which are list3[3] and list3[3][1]"
I know I could convert the whole thing to a string and parse it, but this doesn't seem like a very elegant or efficient solution. Thank you!