我试图从字典中提取大写字母和数字,并使用python打印其格式
例如,输入字典= {'姓名':['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*(*#']
您不需要为此使用正则表达式,只需遍历字符串中的所有字符。
然后我们使用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*(*#']}