Du bygger en streng i din for-løkke, ikke en liste over re.compile
genstande. Du ønsker:
collected_x = [] # Initialize an empty list
for x in input: # Iterate over input
collected_x.append(re.compile(x)) # Append re.compile object to list
collected_x_cut = collected_x[:-2] # Slice the list outside the loop
cursor = db.collection.find({"key": {"$all": collected_x_cut}})
En simpel tilgang ville være at bruge map
for at bygge listen:
collected = map(re.compile, input)[:-2]
db.collection.find({"key": {"$all": collected}})
Eller en list comprehension
:
collected = [re.compile(x) for x in input][:-2]
db.collection.find({"key": {"$all": collected}})