[英]matching more than one substring in a list of strings
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.我想遍历此列表并检查 2 个或更多位置的字符串,以确保执行正确的 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:如果我正在检查单个 substring 那么它非常简单:
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.