简体   繁体   中英

Python List Iteration Using Dynamic Length

I'm wanting to iterate over a loop in python but am not sure how to go about it.

channels = [1, 2]
for channel in channels:
    channel_filters = filters.channel(channel)

I need to end up like this:

channel_filters = filters.channel(1) | filters.channel(2) | filters.channel(3)

But need it to be dynamic with the length of the list.

Is anyone able to point me in the right direction on how best to achieve this?

I managed to resolve the issue with the answer shared by toppk.

channel_filters = 0;
channels = [-1, -2]
for channel in channels:
    temp_channel=abs(channel)
    channel_filters |= -abs(temp_channel)
    print(channel_filters)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM