020作业:统计下边这个长字符串中各个字符出现的次数
统计下边这个长字符串中各个字符出现的次数def search(*params):
a = list(params)
b = []
for each in params:
if each not in b:
b.append(each)
c = {}
for each in b:
c = a.count(each)
print(c)
search(' sdfdsf')
哪里不对呢...{:5_96:} def search(params): def search(string):
c = {}
for e in string:
c.setdefault(e,0)
c += 1
return c
print(search(' sdfdsf')) def search(*params):
a = []
for each in params:
a.append(each)
b = []
for each in params:
if each not in b:
b.append(each)
c = {}
for each in b:
c = a.count(each)
print(c)
search(' sdfdsf')
或者这样
def search(params):
a = []
for each in params:
a.append(each)
b = []
for each in params:
if each not in b:
b.append(each)
c = {}
for each in b:
c = a.count(each)
print(c)
search(' sdfdsf')
以修改 希望采纳
*params就是考虑不定长参数,那就需要多一个循环
然后下面计数也要放进这个循环里
def search(*params):
a = list(params)
b = []
c = {}
for params_temp in a:
for each in params_temp:
if each not in b:
b.append(each)
for each in b:
c = params_temp.count(each)
print(c)
search(' sdfdsf','sdaaa') {:10_256:}{:10_256:}{:10_256:} f = open("string1.txt")
lines = f.read()
result = {x:lines.count(x) for x in lines}
print(result)
f.close()
页:
[1]