# List rotation

David Vaughan David.Vaughan at Gifford.UK.com
Fri Oct 1 09:39:50 CEST 2004

```Here's a quick way to transpose, keeping all the sharps and flats
correct.
You could dress it up a bit by writing a Tune subclass of list with a
transpose method, particularly if there are other things you want to
do with tunes :)

David

# Define a long list of notes, each a fifth higher than its predecessor:
fifths = []
for suffix in ['bb','b','','#','##']:
for note in ['F','C','G','D','A','E','B']:
fifths.append(note+suffix)
fifths.append('F###')  # So len(fifths)==36 and we can wrap round
correctly
print 'fifths ==',fifths
print

# Translate between note names and indices in this list of fifths:
def m_ord(note_name):
return fifths.index(note_name)
def m_str(note_index):
"""Note: index can be any value, and wraps round appropriately."""
return fifths[note_index % len(fifths)]

# Do the main work:
def offset(sourcekey, destkey):
"""Returns offset in fifths which represents key-change."""
return m_ord(destkey) - m_ord(sourcekey)
def transpose(tune, offset):
"""Transposes a tune (=list of notes) by given key-change offset."""
return [m_str(m_ord(note)+offset) for note in tune]

# And show it off:
tune = ['C','E','B','F']
print 'Tune is',tune
for key in ['G', 'F', 'Dbb']:
t = offset('C', key)
print 'Transposition of tune from C to',key,'is',transpose(tune,t)

David Vaughan
Structural Analyst
GIFFORD AND PARTNERS LTD
Southampton, SO40 7HT,  UNITED KINGDOM

Tel: +44 (0)23 8081 7500      Fax: +44 (0)23 8081 7600
Email: David.Vaughan at gifford.uk.com
Web:  http://www.gifford.uk.com
_____

This e-mail and any files transmitted with it are intended solely for
the use of the individual or entity to whom they are addressed and may
be confidential.  If you are not the intended recipient or the person
responsible for delivering the e-mail to the intended recipient, be
advised that you have received this e-mail in error and that any use,
dissemination, forwarding, printing or copying of this e-mail is
strictly prohibited.

```