Apr-23-2018, 01:05 AM
For the first part of my homework assignment, I have read in a file full of music keys and chords, and created a list of lists from the file that looks like:
The first element in every inner list is the key, while the rest of the elements in the same list are the chords for that key. For example in first inner list , the first 'C#' is the key, while the rest of the elements in that inner list ('C#', 'D#m', 'E#m', 'F#', 'G#', 'A#m') are the chords.
I need to create a dictionary where the dictionary keys are equal to the music keys and the dictionary values are equal to the chords in that key.
Like so:
Here is the code I have tried:
By the way I did call main() at the end of my code, I forgot to copy that part into the above code.
Output:[['C#', 'C#', 'D#m', 'E#m', 'F#', 'G#', 'A#m'], ['F#', 'F#', 'G#m', 'A#m', 'B', 'C#', 'D#m'], ['B', 'B', 'C#m', 'D#m', 'E', 'F#', 'G#m'], ['E', 'E', 'F#m', 'G#m', 'A', 'B', 'C#m'], ['A', 'A', 'Bm', 'C#m', 'D', 'E', 'F#m'], ['D', 'D', 'Em', 'F#m', 'G', 'A', 'Bm'], ['G', 'G', 'Am', 'Bm', 'C', 'D', 'Em'], ['C', 'C', 'Dm', 'Em', 'F', 'G', 'Am'], ['F', 'F', 'Gm', 'Am', 'Bb', 'C', 'Dm'], ['Bb', 'Bb', 'Cm', 'Dm', 'Eb', 'F', 'Gm'], ['Eb', 'Eb', 'Gm', 'Ab', 'Bb', 'Cm'], ['Ab', 'Ab', 'Bbm', 'Cm', 'Db', 'Eb', 'Fm'], ['Db', 'Db', 'Ebm', 'Fm', 'Gb', 'Ab', 'Bbm'], ['Gb', 'Gb', 'Abm', 'Bbm', 'Cb', 'Db', 'Ebm']]
The first element in every inner list is the key, while the rest of the elements in the same list are the chords for that key. For example in first inner list , the first 'C#' is the key, while the rest of the elements in that inner list ('C#', 'D#m', 'E#m', 'F#', 'G#', 'A#m') are the chords.
I need to create a dictionary where the dictionary keys are equal to the music keys and the dictionary values are equal to the chords in that key.
Like so:
Output:{'C#': ['C#', 'D#m', 'E#m', 'F#', 'G#', 'A#m'], 'F#': ['F#', 'G#m', 'A#m', 'B', 'C#', 'D#m'], ... }
This is where I am having trouble. Every time I run my code i get this output:Output:{'C#': ['A#m'], 'F#': ['D#m'], 'B': ['G#m'], 'E': ['C#m'], 'A': ['F#m'], 'D': ['Bm'], 'G': ['Em'], 'C': ['Am'], 'F': ['Dm'], 'Bb': ['Gm'], 'Eb': ['Cm'], 'Ab': ['Fm'], 'Db': ['Bbm'], 'Gb': ['Ebm']}
I understand that it is outputting the last element in every inner list for each key, but I can't figure out why it is doing it.Here is the code I have tried:
def main(): chord_file = open("chords.txt") chord_lines = chord_file.readlines() chord_dict={} for i in range(len(chord_lines)): chord_lines[i] = chord_lines[i].split() for j in range(len(chord_lines[i])): chord_lines[i][j] = str(chord_lines[i][j]) for i in range(len(chord_lines)): for j in range(1,len(chord_lines[i])): chord_list = [] chord_list.insert(j-1,chord_lines[i][j])#also tried chord_list.append(chord_lines[i][j]) chord_dict[chord_lines[i][0]] = chord_list print(chord_dict)Any help would be greatly appreciated. Thanks.
By the way I did call main() at the end of my code, I forgot to copy that part into the above code.