Haskell可以这样:
['a'..'z']
Run Code Online (Sandbox Code Playgroud)
在Python中:
map(chr, range(97, 123))
Run Code Online (Sandbox Code Playgroud)
我觉得Python很冗长.让Python像Haskell一样简单吗?
在Python 2中,您可以执行以下操作来获取当前区域设置的字符集:
import string
print string.letters
Run Code Online (Sandbox Code Playgroud)
然而,在Python 3字符串模块的区域设置相关的常数(例如string.letters,string.lowercase,string.uppercase等等)除去.
如何使用Python 3获取当前语言环境的字符集?
我需要random.choice(alphabet)为自己的许多游戏创建随机的单词/名称,但是很难输入,并制作大写版本,仅辅音/元音等。
是否有内置或可导入的方式在python中获得预制的方式?
我想A, b, c, d...使用 range()打印Python中包含的英文字母,但出现 TypeError。有没有一种方法可以做到这一点而不必手动定义英文字母列表?
>>> for letter in range('a', 'z'):
... print(letters)
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer
>>>
Run Code Online (Sandbox Code Playgroud) 我需要一个函数来计算每个字母出现在字符串中的次数.它必须将大写字母和小写字母统计为相同的字母.我有点做了,但它并不完美.
def lmao(x):
aye=x.count("a")
Aye=x.count("A")
bye=x.count("b")
Bye=x.count("B")
cye=x.count("c")
Cye=x.count("C")
dye=x.count("d")
Dye=x.count("D")
Eye=x.count("E")
eye=x.count("e")
Fye=x.count("F")
fye=x.count("f")
Gye=x.count("G")
gye=x.count("g")
Hye=x.count("H")
hye=x.count("h")
Iye=x.count("I")
iye=x.count("i")
Jye=x.count("J")
jye=x.count("j")
Kye=x.count("K")
kye=x.count("k")
Lye=x.count("L")
lye=x.count("l")
Mye=x.count("M")
mye=x.count("m")
Nye=x.count("N")
nye=x.count("n")
Oye=x.count("O")
oye=x.count("o")
Pye=x.count("P")
pye=x.count("P")
Qye=x.count("Q")
qye=x.count("q")
rye=x.count("r")
Rye=x.count("R")
sye=x.count("s")
Sye=x.count("S")
tye=x.count("t")
Tye=x.count("T")
uye=x.count("u")
Uye=x.count("U")
Vye=x.count("V")
vye=x.count("v")
Wye=x.count("W")
wye=x.count("w")
Xye=x.count("X")
xye=x.count("x")
Yye=x.count("Y")
yye=x.count("y")
Zye=x.count("Z")
zye=x.count("z")
killme=(aye+Aye,bye+Bye,cye+Cye,Dye+dye,Eye+eye,Fye+fye,Gye+gye,Hye+hye,Iye+iye,jye+Jye,Kye+kye,Lye+lye,Mye+mye,Nye+nye,Oye+oye,Pye+pye,Qye+qye,rye+Rye,sye+Sye,Tye+tye,uye+Uye,Vye+vye,Wye+wye,xye+Xye,Yye+yye,Zye+zye)
return killme
Run Code Online (Sandbox Code Playgroud)
所以,是的,那就是我想出的灾难.有没有办法缩短这个过程?
试图让这个程序将字母翻译成数字,这样就可以输入带有单词的电话号码并输出数字版本.(1800GOTJUNK = 18004685865)不知道我哪里出错但每个输出只给出最后一个字母的数字并重复所有数字的数字(1800adgjmptw = 18009999999).非常感谢任何帮助,谢谢.
def transNum(string):
number = 1
for ch in string:
if ch.lower() in "abc":
number = 2
elif ch.lower() in "def":
number = 3
elif ch.lower() in "ghi":
number = 4
elif ch.lower() in "jkl":
number = 5
elif ch.lower() in "mno":
number = 6
elif ch.lower() in "pqrs":
number = 7
elif ch.lower() in "tuv":
number = 8
elif ch.lower() in "wxyz":
number = 9
return number
def translate(phone):
newNum = ""
for ch in …Run Code Online (Sandbox Code Playgroud) python中是否有内置列表或某些包含字母表列表的包?我想避免像这样的系统
alphabets = ('a','b','c',.....)
Run Code Online (Sandbox Code Playgroud) 我试图在同一行上打印字母表中的所有大写字母,但我不断收到语法错误。
for c in range(0, 26):
print(chr(ord('A', end '')+c))
Run Code Online (Sandbox Code Playgroud)