关于PYTHON字符串处理,怎么实现统计字符串中出现了2次的英文字母(区分大小写)

问题相关代码,请勿粘贴截图

s=input()
dic={}
for i in s:
if i >='a' and i<='z' or i>='A' and i<='Z' :
dic[i]=s.count(i)
def getKey(dic,2):
if 2 not in dic:
return '出现2次的字母:无'
result=set()
for key in dic:
result.add(key)
return '出现2次的字母:',result

运行结果及报错内容

Exited with error status 1
File "/data/0d5b1f086b293b442a33aa0f857a4235", line 6
def getKey(dic,2):
^
SyntaxError: invalid syntax

我想要达到的结果

img


s = input()
a = []
for i in s:
    if s.count(i) == 2 and i not in a:
        a.append(i)
a = ",".join([str(i) for i in a])
print("出现2次的字母:无" if len(a) == 0 else "出现2次的字母:{}".format(a))

这样改试试:

s=input()
dic={}
for i in s:
    if i >='a' and i<='z' or i>='A' and i<='Z' :
        dic[i]=s.count(i)

def getKey(dic,n):
    if n not in dic.values():
        return '出现2次的字母:无'
    result=set()
    for key in dic:
        if dic.get(key) == n: 
            result.add(key)
    return '出现2次的字母:',result

print(getKey(dic,2))

# -*- coding: utf-8 -*-

search="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

def find2times(str):
    '''
    初始化矩阵
    :param m:
    :param n:
    :return:
    '''
    result = []
    for i in range(0,len(search)):
        first = str.count(search[i])
        if first == 2:
            result.append(search[i])
    if len(result) > 0:
        return ",".join(result)
    else:
        return u"无"


if __name__ == "__main__":
    teststr="hello world, my name is bob"
    teststr2="alice"
    result = find2times(teststr)
    print(u"输入:{}, 出现2次的字母:{}".format(teststr, result))
    result = find2times(teststr2)
    print(u"输入:{}, 出现2次的字母:{}".format(teststr2, result))

看看是否合适,合适请采纳,谢谢