一道python练习题,求解答,谢谢

 

第一空:[]

2、not in ls

3、ls.append(c)

4、len(ls)

str1 = "hello world"
# 方法一: 字典推导式
result = {x:str1.count(x) for x in str1}
print(result)

# 方法二:自己写逻辑
result = {}
for x in str1:
    result[x] = result.get(x, 0) + 1
print(result)

如果对你有帮助,可以点击我这个回答右上方的【采纳】按钮,给我个采纳吗,谢谢
 

s="abcdddca"
ls={}
for c in s:
    if c in ls:
        ls[c]=ls.get(c,0)+1
    else:
        ls[c]=1
print("只出现一次的字符有{}个".format(len([x for k,x in ls.items() if x==1])))
ls=sorted(ls.items(),key=lambda x:x[-1],reverse=True)
print(ls)