<pre><code>>>> import string
>>> def letterList (start, end):
# add a character at the beginning so str.index won't return 0 for `A`
a = ' ' + string.ascii_uppercase
# if start > end, then start from the back
direction = 1 if start < end else -1
# Get the substring of the alphabet:
# The `+ direction` makes sure that the end character is inclusive; we
# always need to go one *further*, so when starting from the back, we
# need to substract one. Here comes also the effect from the modified
# alphabet. For `A` the normal alphabet would return `0` so we would
# have `-1` making the range fail. So we add a blank character to make
# sure that `A` yields `1-1=0` instead. As we use the indexes dynamically
# it does not matter that we have changed the alphabet before.
return a[a.index(start):a.index(end) + direction:direction]
>>> letterList('A', 'G')
'ABCDEFG'
>>> letterList('G', 'A')
'GFEDCBA'
>>> letterList('A', 'A')
'A'
</code></pre>
<p>请注意,此解决方案允许使用任何类型的字母表。我们可以设置<code>a = ' ' + string.ascii_uppercase + string.ascii_lowercase</code>并得到这样的结果:</p>
<pre><code>>>> letterList('m', 'N')
'mlkjihgfedcbaZYXWVUTSRQPON'
</code></pre>
<p>当您完全支持unicode时,谁需要ASCII?</p>
<pre><code>>>> a = ' あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわを'
>>> letterList('し', 'ろ')
'しすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろ'
</code></pre>