Question

x = 115

freq_chart = {
'C1':'65.4063913251',
'C#1':'69.2956577442',
'D1':'73.4161919794',
'D#1':'77.7817459305',
'E1':'82.4068892282',
'F1':'87.3070578583',
'F#1':'92.4986056779',
'G1':'97.9988589954',
'G#1':'103.826174395',
'A1':'110',
'A#1':'116.5409403795',
'B1':'123.470825314'
}

I need to return the closest note name that x is closest to in freq_chart. In this example, x is closest to A#1. What is the syntax? Thank you!

Was it helpful?

Solution

You can use min:

>>> min(freq_chart, key=lambda y:abs(float(freq_chart[y])-x))
'A#1'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top