我写了一个简单的函数程序。该程序本身工作,但转换为函数时,它给出的字符数为1.我正在使用Python版本3.5。Python函数错误
def sink(x,y):
x = ['a','a','b','c']
y = 'c'
count = {}
for char in x:
count.setdefault(char,0)
count[char] = count[char]+1
print (count.get(y,0))
sink (['a','a','c','d','e','e'],'e')
不是一个伟大的使用setdefault'的',你可以使用' collections.defaultdict(int)'为'count'或者只是'count [char] = count.get(char,0)+ 1' – AChampion