Python Forum
Help with Dictionaries and Lists of Lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Dictionaries and Lists of Lists
#1
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.
Reply
#2
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 
Reply
#3
Thank you for the response. We have not learned about eval or sorted dictionaries, is there another way to do it?
Reply
#4
(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']}
Reply
#5
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.
Reply
#6
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Stuck with using lists to solve task gery576 9 835 Jan-16-2024, 06:29 PM
Last Post: DPaul
  An idiotic problem on Python lists on HackerRank anata2047 4 4,308 Jun-16-2022, 12:12 PM
Last Post: DeaD_EyE
  Lists Leo 3 2,341 Mar-03-2022, 12:41 AM
Last Post: Leo
  function for extracting data from lists Paulman 9 2,805 Nov-09-2021, 04:00 PM
Last Post: Paulman
  Lists Tink 11 4,820 Dec-02-2020, 01:52 PM
Last Post: perfringo
  Intro python using lists Rustypotatoes 2 3,855 Sep-17-2020, 08:30 PM
Last Post: jefsummers
  2 Pairs 2 Lists Harshil 7 2,898 Aug-13-2020, 02:32 PM
Last Post: deanhystad
  simple task with lists... Maxwell123 3 2,379 Jun-27-2020, 01:00 PM
Last Post: GOTO10
  Sorting nested lists in ascending order jszum 2 2,268 May-17-2020, 01:35 PM
Last Post: jefsummers
  loops in combination with lists Juru 4 2,821 May-07-2020, 02:58 PM
Last Post: Marbelous

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020