想要从单词列表中提取大写和小写字母

时间:2019-04-20 04:14:21

标签: python-3.x

我正在尝试从字典中提取大写小写字母和数字,并使用python打印其格式

Ex。,输入字典= {'名称':['AgbAA21','sdsd21S'],'地址':['AGDB323andnd','sbfsj @ 2342'],'电话':['909898986',' 23423 *(*#']}

预期输出= {'名称':['AaaAA99','aaaa99A'],'地址':'AAAA999aaaaa','aaaaa @ 9999'],'电话':'999999999','99999 *(*# ']}


for key,value in outputDict.items(): 

    for wiki in value:
        fmt = ''
        for c in wiki:
            if c.islower():
                fmt += 'a'
            elif c.isupper():
                fmt += 'A'
            elif c.isdigit():
                fmt += '9'
            else:
                fmt+=c
        output.append(fmt)
    print(key,output)

Expected result :{'Name': ['AaaAA99', 'aaaa99A'], 'address': 'AAAA999aaaaa', 'aaaaa@9999'], 'phone': '999999999', '99999*(*#']}


Actual result:

Name ['AaaAA99', 'aaaa99A']
address ['AaaAA99', 'aaaa99A', 'AAAA999aaaaa', 'aaaaa@9999']
phone ['AaaAA99', 'aaaa99A', 'AAAA999aaaaa', 'aaaaa@9999', '999999999', '99999*(*#']

1 个答案:

答案 0 :(得分:0)

您不需要为此使用正则表达式,只需遍历字符串中的所有字符即可。

然后我们使用isupper()检查字符是否为大写字母,使用islower()检查小写字符还是使用isdigit()使用数字,对于其他情况,我们直接附加字符,然后创建我们的输出字符串相应地,最后我们将其附加到输出列表中


inputDict =  {'Name': ['AgbAA21', 'sdsd21S'], 'address': ['AGDB323andnd', 'sbfsj@2342'], 'phone': ['909898986', '23423*(*#']}
outputDict = {}

for key,value in inputDict.items():

    output = []
    for wiki in value:
        fmt = ''
        for c in wiki:
            if c.islower():
                fmt += 'a'
            elif c.isupper():
                fmt += 'A'
            elif c.isdigit():
                fmt += '9'
            else:
                fmt+=c
        output.append(fmt)
    outputDict[key] = output

print(outputDict)

输出看起来像

{'Name': ['AaaAA99', 'aaaa99A'], 
'address': ['AAAA999aaaaa', 'aaaaa@9999'], 
'phone': ['999999999', '99999*(*#']}