strSpecialChars=['%', 'dBu', 'dB', 'kHz', 'Hz']
str = "-20.0dB"
我需要到True这里,因为它检查列表中的每个项目 -strSpecialChars在字符串中str。
strSpecialChars=['%', 'dBu', 'dB', 'kHz', 'Hz']
str = "-20.0dB"
我需要到True这里,因为它检查列表中的每个项目 -strSpecialChars在字符串中str。
使用any()函数测试每个值:
>>> strSpecialChars=['%', 'dBu', 'dB', 'kHz', 'Hz']
>>> yourstr = "-20.0dB"
>>> any(s in yourstr for s in strSpecialChars)
True
我重命名str为 yourstr以避免掩盖内置类型。
any()只会推进传递给它的生成器表达式,直到True返回一个值;这意味着您的示例仅测试前 3 个选项。
你可以str.endswith()在这里使用:
any(yourstr.endswith(s) for s in strSpecialChars)
将匹配限制为仅以任何特殊字符结尾的匹配。
map(lambda s: s in "-20.0dB", strSpecialChars)
您可能需要将输出转换list为实际看到它。