在 Python(特别是 Python 3.0,但我认为这并不重要)中,如何轻松地在具有连续字符代码的字符序列上编写循环?我想做这样的伪代码:
for Ch from 'a' to 'z' inclusive: #
f(Ch)
示例:下面的一个不错的“pythonic”版本怎么样?
def Pangram(Str):
''' Returns True if Str contains the whole alphabet, else False '''
for Ch from 'a' to 'z' inclusive: #
M[Ch] = False
for J in range(len(Str)):
Ch = lower(Str[J])
if 'a' <= Ch <= 'z':
M[Ch] = True
return reduce(and, M['a'] to M['z'] inclusive) #
标有 # 的行是伪代码。当然 reduce() 是真正的 Python!
亲爱的巫师们(特别是年长的白胡子巫师),也许你能看出我最喜欢的语言曾经是 Pascal。