这个题可以有好几种解题方法。

一、索引值获取

s="hello alex alex hello haiyan cc  haiyan  com"
l=s.split()
dic={}
for item in l:
    if item in dic:
           dic[item]+=1
   else :
           dic[item]=1
print(dic)

二、count方法

s="hello alex alex hello haiyan cc  haiyan  com"
l=s.split()
dic={}
for item in l:
       dic[item]=l.count(item)
print(dic)

三、setdefault方法

s="hello alex alex hello haiyan cc  haiyan  com"
l=s.split()
dic={}
for item in l:
      dic.setdefault(item,l.count(item))
print(dic)

四、利用集合先去重,后利用count

s="hello alex alex hello haiyan cc  haiyan  com"
l=s.split()
dic={}
set_s=set(l)
for item in l:
        dic[item]=l.count(item)
print(dic)

  

posted on 2017-07-21 15:20  海燕。  阅读(510)  评论(2编辑  收藏  举报