Posts: 3
Threads: 1
Joined: Apr 2018
Apr-23-2018, 01:05 AM
(This post was last modified: Apr-23-2018, 01:12 AM by Nate5800.)
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:
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.
Posts: 817
Threads: 1
Joined: Mar 2018
import ast
data = '''[['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']]'''
#with open("chords.txt", 'r') as a_source_file:
# data = a_source_file.read() # read entire txt-file
# unsafe method
result = {item[0]: item[1:] for item in eval(data)} # if data is trusted
# more reliable
#result = {item[0]: item[1:] for item in ast.literal_eval(data)} # if data isn't trusted
print(result)
# Note: Python dictionary is unordered structure, so you probably need to use OrderedDict instead
Posts: 3
Threads: 1
Joined: Apr 2018
Thank you for the response. We have not learned about eval or sorted dictionaries, is there another way to do it?
Posts: 3,458
Threads: 101
Joined: Sep 2016
(Apr-23-2018, 03:05 PM)Nate5800 Wrote: We have not learned about eval That's good. Eval is a very bad suggestion, and should never actually be used.
Quote: for i in range(len(chord_lines)):
for j in range(1,len(chord_lines[i])):
I think this is actually a little more confusing than it needs to be. If we just iterate over the items directly, the code becomes much clearer, and easier to see what's happening.
To make it easier for me to work with, I created a variable data to hold whatever you're getting from a file, and I used the pprint module to display the results in a nice manner. The rest should be fairly easy to understand: >>> data = [['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']]
>>> import pprint
>>> pprint.pprint(data)
[['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']]
>>> chords = {}
>>> for group in data:
... key = group[0]
... chord = group[1:]
... chords[key] = chord
...
>>> pprint.pprint(chords)
{'A': ['A', 'Bm', 'C#m', 'D', 'E', 'F#m'],
'Ab': ['Ab', 'Bbm', 'Cm', 'Db', 'Eb', 'Fm'],
'B': ['B', 'C#m', 'D#m', 'E', 'F#', 'G#m'],
'Bb': ['Bb', 'Cm', 'Dm', 'Eb', 'F', 'Gm'],
'C': ['C', 'Dm', 'Em', 'F', 'G', 'Am'],
'C#': ['C#', 'D#m', 'E#m', 'F#', 'G#', 'A#m'],
'D': ['D', 'Em', 'F#m', 'G', 'A', 'Bm'],
'Db': ['Db', 'Ebm', 'Fm', 'Gb', 'Ab', 'Bbm'],
'E': ['E', 'F#m', 'G#m', 'A', 'B', 'C#m'],
'Eb': ['Eb', 'Gm', 'Ab', 'Bb', 'Cm'],
'F': ['F', 'Gm', 'Am', 'Bb', 'C', 'Dm'],
'F#': ['F#', 'G#m', 'A#m', 'B', 'C#', 'D#m'],
'G': ['G', 'Am', 'Bm', 'C', 'D', 'Em'],
'Gb': ['Gb', 'Abm', 'Bbm', 'Cb', 'Db', 'Ebm']}
Posts: 12,031
Threads: 485
Joined: Sep 2016
The chords can also be built using intervals and the circle of fifths.
Here's an article on this: https://www.musical-u.com/learn/how-to-u...le-fifths/
using such a method, any chord for any key can be built
I have an unfinished project here: https://github.com/Larz60p/MusicScales
look at the file: sdata.py, there are a bunch of dictionaries for chord patterns, plus one for
over 600 scales from around the world.
Posts: 3
Threads: 1
Joined: Apr 2018
(Apr-23-2018, 04:42 PM)nilamo Wrote: (Apr-23-2018, 03:05 PM)Nate5800 Wrote: We have not learned about eval That's good. Eval is a very bad suggestion, and should never actually be used. Quote: for i in range(len(chord_lines)): for j in range(1,len(chord_lines[i])):
I think this is actually a little more confusing than it needs to be. If we just iterate over the items directly, the code becomes much clearer, and easier to see what's happening. To make it easier for me to work with, I created a variable data to hold whatever you're getting from a file, and I used the pprint module to display the results in a nice manner. The rest should be fairly easy to understand:>>> data = [['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']] >>> import pprint >>> pprint.pprint(data) [['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']] >>> chords = {} >>> for group in data: ... key = group[0] ... chord = group[1:] ... chords[key] = chord ... >>> pprint.pprint(chords) {'A': ['A', 'Bm', 'C#m', 'D', 'E', 'F#m'], 'Ab': ['Ab', 'Bbm', 'Cm', 'Db', 'Eb', 'Fm'], 'B': ['B', 'C#m', 'D#m', 'E', 'F#', 'G#m'], 'Bb': ['Bb', 'Cm', 'Dm', 'Eb', 'F', 'Gm'], 'C': ['C', 'Dm', 'Em', 'F', 'G', 'Am'], 'C#': ['C#', 'D#m', 'E#m', 'F#', 'G#', 'A#m'], 'D': ['D', 'Em', 'F#m', 'G', 'A', 'Bm'], 'Db': ['Db', 'Ebm', 'Fm', 'Gb', 'Ab', 'Bbm'], 'E': ['E', 'F#m', 'G#m', 'A', 'B', 'C#m'], 'Eb': ['Eb', 'Gm', 'Ab', 'Bb', 'Cm'], 'F': ['F', 'Gm', 'Am', 'Bb', 'C', 'Dm'], 'F#': ['F#', 'G#m', 'A#m', 'B', 'C#', 'D#m'], 'G': ['G', 'Am', 'Bm', 'C', 'D', 'Em'], 'Gb': ['Gb', 'Abm', 'Bbm', 'Cb', 'Db', 'Ebm']}
Thank you so much!
|