I have a list of strings, containing chords represented as Latin Numerals like this:
['ii7', 'vi7', 'V', 'IVadd9', 'Iadd9', 'V', 'IVmaj7', 'ii7', 'vi7', 'V', 'IVadd9', 'Iadd9', 'V', 'IVmaj7']
and I want to split these strings into 3 sublists, like this:
numerals = ['ii', 'vi', 'V', 'IV', 'I', 'V', 'IV', 'ii', 'vi', 'V', 'IV', 'I', 'V', 'IV']
chord_type=['min', 'min', 'maj', 'maj', 'maj', 'maj','maj', 'min', 'min', 'maj', 'maj', 'maj', 'maj','maj']
extentions=['7','7','', 'add9','add9','','7','7','7','','add9','add9','','7']
(As you can see, the roman numerals in capital letters correspond to 'maj' in chord type and in non-capital letters to 'min'. )
All the possible roman numerals in my case:
i, ii, iii, iv, v, vi, vii, I, II, III, IV, V, VI, VII
Note that we don't need M
, C
, L
, X
.
I know I can extract or split numbers from letters in a string in Python, as described here, but how can I extract roman numerals?
I thought about using something like match regex, but I'm confused on how to define those 7 roman numerals as these characters might occur again in the string.
If roman numeral is always first then you might do
import re
chords = ['ii7', 'vi7', 'V', 'IVadd9', 'Iadd9', 'V', 'IVmaj7', 'ii7', 'vi7', 'V', 'IVadd9', 'Iadd9', 'V', 'IVmaj7']
numerals = [re.match('[IiVv]+', i).group(0) for i in chords]
print(numerals)
output
['ii', 'vi', 'V', 'IV', 'I', 'V', 'IV', 'ii', 'vi', 'V', 'IV', 'I', 'V', 'IV']
Note that I used re.match as it does Try to apply the pattern at the start of the string and limited digits to existing in your example (rather than using all known i.e. IiVvXxLlCcDdMm
).