1

Here I have a chordlist:

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"]

Then for example, there are some strings like "F:maj","F:maj/3","C:maj/4","D:min7" etc. I know i can use if string in chordList to check whether those normal chords in the list. However, I want to compare strings like "F:maj/3","C:maj/4" with the strings in the list, whether they have the common "x:maj" or "x:min" parts, then to return a TRUE or FALSE value

2
  • If you are working with Python and music theory, have a look at this library github.com/fferri/musthe (I'm one of the contributors). Commented May 17, 2018 at 16:36
  • Cool! I will check it out. Thx :D Commented May 17, 2018 at 16:44

2 Answers 2

2

I would recommend converting your chordList to a set for average O(1) lookup times.

Option 1
split on /:

sample = 'C:maj/7'
sample.split('/')[0] in chordList
# True

Option 2 (If I remember correctly from my piano-playing days this will always work, although if chords exist past 9ths this will fail)
slice

sample = 'C:maj/7'
sample[:-2] in chordList
# True
Sign up to request clarification or add additional context in comments.

1 Comment

Happy to help, if your don't have chords past 9ths, the second option will be about twice as fast on average.
0

It seems you can match the strings by prefix:

s="F:maj/3"
if any(s.startswith(x) or x.startswith(s) for x in chordList):
    # yes, it is contained

Comments