Somewhat simple question with many similar ones out there, but I cannot seem to find what I am looking for. I am trying to filter a list of images in a dbc.Carousel via an int list of location numbers. Any image title that starts with a given number will correlate to a location. So, initially, I would want all images that start with 1, 2, 3, 4, OR 5. I have:
JavaScript
x
12
12
1
def imageFilter(locFilter):
2
3
locFilter = list(map(int, locFilter))
4
#locFilter = [1, 2, 3, 4, 5]
5
itemFilter = [
6
{'key': k ,'src':'data:image/jpg;base64,{}'.format(v.decode())}
7
for k,v in imageEncodeDict.items()
8
# Everything works above here. .format with single numbers works too
9
if k.startswith("Spot CAM {}".format(locFilter))
10
]
11
return [dbc.Carousel(controls=True, indicators=True, interval=None, style=img_style, items = itemFilter)]
12
Unpacking the list with *locFilter
DOES NOT work. How can I pass a list to work as a filter for str.format?
Advertisement
Answer
You can try using any
with a generator expression:
JavaScript
1
11
11
1
def imageFilter(locFilter):
2
3
locFilter = list(map(int, locFilter))
4
#locFilter = [1, 2, 3, 4, 5]
5
itemFilter = [
6
{'key': k ,'src':'data:image/jpg;base64,{}'.format(v.decode())}
7
for k,v in imageEncodeDict.items()
8
if any(k.startswith("Spot CAM {}".format(f)) for f in locFilter)
9
]
10
return [dbc.Carousel(controls=True, indicators=True, interval=None, style=img_style, items = itemFilter)]
11