词频统计

# count 推导思路    词频统计
#统计 元素出现的次数
l1 = [1,2,3,5,4,1,2,3,6,4,5,88,99]
l2 = list()
#  [元素,次数,元素,次数,..........................]
for i in l1:                                       #  i  在  l1 循环
    if i not in l2:                                #  条件 :  如果i 不在l2 里边 开始循环:
        l2.append(i)                               #   l2 添加    i
        l2.append(str(1))                          #   l2 添加  字符串  '1'                [i , 'i+1']
    else:                                                           # 如果不存在,把次数加一      先找需要改值得位置
        l2[l2.index(i) + 1] = str(int(l2[l2.index(i) + 1]) + 1)     # 找到位置 赋值次数  l2后一位+1
print(l2)
# [1, '2', 2, '2', 3, '2', 5, '2', 4, '2', 6, '1', 88, '1', 99, '1']
i = 0
while i < len(l2):
    print(l2[i],l2[i+1],sep='  ------')             # 迭代打印 格式  l2[i] ------  l2[i+1]    sep  默认格式
    i += 2                                          # 隔一个元素 进行一次迭代
# 1  ------2
# 2  ------2
# 3  ------2
# 5  ------2
# 4  ------2
# 6  ------1
# 88  ------1
# 99  ------1
for i in range(0,len(l2),2):                  #  循环范围   0 开始 尾结束 隔两个 循环一次
    print(l2[i],l2[i+1],sep='  ------')       #  迭代打印 格式  l2[i] ------  l2[i+1]
# 1  ------2
# 2  ------2
# 3  ------2
# 5  ------2
# 4  ------2
# 6  ------1
# 88  ------1
# 99  ------1

#字符串  取元素取次数
s1 = 'nihao,suibian,ni,nizai,zaime,zaina,nihao'
s1 = s1.split(',')
print(s1)
s2 = list()
for i in s1:
    if i not in s2:
        s2.append(i)
        s2.append(1)
    else:
        s2[s2.index(i)+1] = int(s2[s2.index(i)+1])+1  #/   s2[s2.index(i)+1] += 1    如果是整数 可以用
print(s2)
#['nihao', 2, 'suibian', 1, 'ni', 1, 'nizai', 1, 'zaime', 1, 'zaina', 1]
for i in range(0,len(s2),2):
    print(s2[i],s2[i+1],sep=' ------')
# nihao ------2
# suibian ------1
# ni ------1
# nizai ------1
# zaime ------1
# zaina ------1
j = 0
while j < len(s2):
    print(s2[j],s2[j+1],sep=' -----')
    j += 2
# nihao ------2
# suibian ------1
# ni ------1
# nizai ------1
# zaime ------1
# zaina ------1


# # 输入一段字符串,打印出这个字符串中出现过的字符及出现过的次数:
# # 如:
# # 输入:abcdabcaba
# # 打印如下:
l = 'abcdabcaba'
le = dict()
for i in l:
    if i not in le:
        le[i] = 1
    else:
        le[i] = le[i]+1
print(le)
for k,v in le.items() :
    print(f'{k}:{v} 次' )


import  collections
s = 'python,java,pjp,go,c,python,java,pjp,go,c,python,java,pjp,go,c'
re = s.split(',')
print(collections.Counter(re))
for word,count in collections.Counter(re).items():
    print(word,count,sep=':')
# collections.Counter  统计次数

s = 'python,java,pjp,go,c,python,java,pjp,go,c,python,java,pjp,go,c'
re = s.split(',')
di = {}
for i in re:
    if i not in di:
        di[i] = 1
    else: 
        di[i] = di[i]+1
print(di)
for k,v in di.items():
    print(f'{k} : {v} 次')
便捷方法

s = 'python,java,pjp,go,c,python,java,pjp,go,c,python,java,pjp,go,c'
re = s.split(',')
le = {}
for i in re:
    if i not in le:
        le[i] = re.count(i)
print(le)
for k,v in le.items():
    print(f'{k} : {v} 次 ')

 

 

 

posted @ 2020-07-17 15:27  XuanchenLi  阅读(203)  评论(0编辑  收藏  举报