I'm learning how to optimize for loops in python 3.7, and I keep getting an "invalid syntax error". I've already checked for missing parenthesis. Here's the code:
best = probabilities[0]
best = (probabilities[i] for i in range(1, len(probabilities)) if probabilities[i] > best)
print(best)
prob, im = (prob, im for prob, im in zip(probabilities, image_names) if prob == best)
return (prob, im)
gives me the error
File "path", line 27
prob, im = (prob, im for prob, im in zip(probabilities, image_names) if prob == best)
^
SyntaxError: invalid syntax
I'm not sure what went wrong, as the following code I used in another exercise uses pretty much the same syntax:
im = [im for im, prob in zip(image_names, probabilities) if prob >= threshold]
return (im)
Edit: I've also tried
return (prob, im for prob, im in zip(probabilities, image_names) if prob == best)
and it says that the variable type is a Generator[tuple[float, str], Any, None] but I'm not sure what it means.
Any help would be very appreciated
Tuples need to be explicitly wrapped in ()
when being produced by a comprehension/generator expression:
((prob, im) for prob, im in zip(probabilities, image_names) if prob == best)