Python wrapping around on a list when list index is out of range

I'm looking for some code improvement, or a pre-built version of what I've implemented myself as I think there might be, or should be, a cleaner way to achieve what I want.

I'm writing a piece of software to convert guitar tabs into classical notation, I need to convert the number on a tab to it's corresponding note and it would be useful for building a list of each strings note from the starting string.

I have a list of notes, (a - g#) and a list of frets (0, 21).

notes[fret] works fine for the first eleven notes but after that I obviously get an out of index error.

The code I have to get around this is here:

notes = ["a", "a#", "b", "c", "c#", "d", "e", "f", "f#", "g", "g#"]
note = 21
while note >= len(notes):
    note -= 11
        print notes[note]

It works but it seems a little... long, is there a better way to do this?


Use the % operator to produce a modulus:

notes[note % len(notes)]


>>> notes = ["a", "a#", "b", "c", "c#", "d", "e", "f", "f#", "g", "g#"]
>>> note = 21
>>> notes[note % len(notes)]

or in a loop:

>>> for note in range(22):
...     print notes[note % len(notes)],
a a# b c c# d e f f# g g# a a# b c c# d e f f# g g#

Need Your Help

How can I fix an issue in IE where borders don't show up when the mouse isn't hovered over an image

css internet-explorer image

I am trying to create a rather simple effect on a set of images. When an image doesn't have the mouse over it, I'd like it to have a simple, gray border. When it does have an image over it, I'd l...

Why won't the div slide up under the above div?

css html5 css3 html

I'm working on this dashboard and can't seem to get container 3 to slide up under container 1, can any one spot why?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.