Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果字符为大写,如何插入数字?_Python - Fatal编程技术网

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'