Python 如果字符为大写,如何插入数字?
我有一个功能:Python 如果字符为大写,如何插入数字?,python,Python,我有一个功能: def func(s): dict = { 'a': 1, 'b': 2, 'c': 3 } #Split input into list split = list(s) #gather output list output = [] for x in split: output.append(dict.get(x)) print(output
def func(s):
dict = {
'a': 1,
'b': 2,
'c': 3 }
#Split input into list
split = list(s)
#gather output list
output = []
for x in split:
output.append(dict.get(x))
print(output)
func("abc")
输出为:
1,2,3
目标:
如果输入包含大写字母,如何将值“00”放在大写字母之前
例如,如果输入为“Abc”,则输出为“00、1、2、3”
一种方法:
我知道的解决方案之一就是将大写字母放入字典中,并将其值设为“00”。但是有人知道一种更简单的方法吗?您可以添加一个if来检查给定字符串是否为上限:
好吧,如果我的绳子在上面。。。我突然想到。
def func(s):
d = {
'a': 1,
'b': 2,
'c': 3 }
#Split input into list
split = list(s)
#gather output list
output = []
for x in split:
if x.isupper():
output.append('00')
output.append(str(d.get(x.lower())))
elif x in d:
output.append(str(d.get(x)))
return ', '.join(output)
func("abc")
# '1, 2, 3'
func("Abc")
#' 00, 1, 2, 3'