这里有一个和弦列表:
chordList = ["N","C:maj","C:min","C#:maj","C#:min","D:maj","D:min","D#:maj","D#:min","E:maj","E:min","F:maj","F:min","F#:maj","F#:min","G:maj","G:min","G#:maj","G#min","A:maj","A:min","A#:maj", "A#:min", "B:maj","B:min"]
然后,例如,有一些字符串,如" F:maj"," F:maj / 3"," C:maj / 4",& #34; d:min7"我知道我可以使用chordList中的字符串来检查列表中是否有正常的和弦。但是,我想比较像" F:maj / 3"," C:maj / 4"使用列表中的字符串,它们是否具有共同的" x:maj"或者" x:min"部件,然后返回TRUE或FALSE值
答案 0 :(得分:2)
我建议您将chordList
转换为set
,以获得平均O(1)
次查找时间。
选项1
split
上的 /
:
sample = 'C:maj/7'
sample.split('/')[0] in chordList
# True
选项2 (如果我在钢琴演奏时间记得正确,这将永远有效,但如果和弦存在于9日以上则会失败)
的 slice
强>
sample = 'C:maj/7'
sample[:-2] in chordList
# True
答案 1 :(得分:0)
似乎你可以通过前缀匹配字符串:
s="F:maj/3"
if any(s.startswith(x) or x.startswith(s) for x in chordList):
# yes, it is contained