将字符串与 python 中的多个字符串进行比较

Comparing string to multiple strings in python

我正在尝试将数据帧的单元格值与一些字符串进行比较,并在变量中连接匹配的字符串,但出现错误。 有人可以检查一下吗..!

route=''
for b in range(10):
   route+  = ('-'+ rail[0].index[b] +'-') if (rail[x].index[b] == ('WD' | 'POT' | 'NGI'))

我得到的错误是:-

unsupported operand type(s) for |: 'str' and 'str'

编辑:添加一些要处理的索引值

Index(['DW', 'NG', 'NGI', 'DW', 'WD', 'NGI', 'NF', 'FGH', 'NNI',
   'DWD','NGH', 'POT', 'NGI', 'DW', 'POT', 'NNGI', 'GH', 'NH', 'NGI',
   'WD'])

假设您尝试在每次迭代中检查您的 rail[x].index[b] 是否是 ('WD' | 'POT' | 'NGI') 之一,

如果是这样,请尝试下面的代码片段,

route=''
for b in range(10):
   route += ('-'+ rail[0].index[b] +'-') if (rail[x].index[b] in ('WD', 'POT', 'NGI'))

快速检查:

print("WD" == ('WD' | 'POT' | 'NGI'))

TypeError: unsupported operand type(s) for |: 'str' and 'str'

print("WD" in ('WD' , 'POT' , 'NGI'))

True