I have a list of strings:
mylist = ['room.ax','room-ax1232upper.bin','room-ki9e0lower.bin','window-rm-down.ax','window-rm-up.ax']
I want to iterate through this list and check the string on 2 or more places to make sure I execute the correct function.
for example (pseudocode):
for item in mylist:
if item contains 'room' with extension '.ax':
call_f1()
elif item contains 'room' and also 'upper' and with extension '.bin':
call_f2()
elif item contains 'room' and also 'lower' and with extension '.bin':
call_f3()
elif item contains 'window' and also 'down' and with extension '.ax':
call_f4()
If I was checking for a single substring then it's pretty straight forward:
for item in mylist:
if 'room' in item:
call_f()
But I can't figure out how to do this for multiple substrings.
TRY:
for item in mylist:
if 'room' in item and item.endswith('.ax'):
call_f1()
elif 'room' in item and 'upper' in item and item.endswith('.bin'):
call_f2()
elif 'room' in item and 'lower' in item and item.endswith('.bin'):
call_f3()
elif 'window' in item and 'down' in item and item.endswith('.ax'):
call_f4()
This works.
for item in mylist:
item = item.split('.')
if item[0] == 'room' and item[1] == 'ax':
print('room.ax if')
elif all(i in item[0] for i in ['room','upper']) == True and item[1] == 'bin':
print('roomupper.bin if')
elif all(i in item[0] for i in ['room','lower']) == True and item[1] == 'bin':
print('roomlower.ax if')
elif all(i in item[0] for i in ['window','down']) == True and item[1] == 'ax':
print('windowdown.ax if')
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.