Skip to content Skip to sidebar Skip to footer

Python How To Create List Of Interchangeable Values?

I'm working with midi note, midi number, and frequency. What kind of list in python should I use to refer to any one attribute and get the other attributes? for example: input: 'C

Solution 1:

Like I said in the comments, a dictionary of tuples is what you're probably looking for. Example:

data = {'C3': ('frequency', 261.6255653006), 
    261.6255653006: ('midinumber', 60), 
    60: ('midinote', 'C3'),
}

To validate your input you can do:

input = raw_input()
try:
    key = float(input)
except ValueError:
    key = inputtry:
    value = data[key]
except KeyError:
    print"Invalid input. Valid keys are: " + ', '.join(data.keys())
else:
    #input was valid, so value == data[key]

Tuples are indexed just like lists are. However, they are immutable which means you can't change them or append new items to them. And I believe that's desired in your case.

Dictionaries are indexed by keys, for example data['C3'] returns ('frequency', 261.6255653006) and data['C3'][0] returns 'frequency'.

Post a Comment for "Python How To Create List Of Interchangeable Values?"