导航:首页 > 编程语言 > python统计词

python统计词

发布时间:2022-07-17 06:43:18

python怎么统计一句英语的单词数量并输出

题主你好,

代码及测试截图如下:

说明: 上图红框处的result可不写, 只是为了看一下分隔结果是否正确.

希望可以帮到题主, 欢迎追问.

❷ Python如何统计文本中各个词性的数量

如果是统计文本中某个词出现的数量就用循环遍历读取,匹配到一次,num+=1,最后print

❸ 用Python统计词频

def statistics(astr):
# astr.replace("\n", "")
slist = list(astr.split("\t"))
alist = []
[alist.append(i) for i in slist if i not in alist]
alist[-1] = alist[-1].replace("\n", "")
return alist

if __name__ == "__main__":
code_doc = {}
with open("test_data.txt", "r", encoding='utf-8') as fs:
for ln in fs.readlines():
l = statistics(ln)
for t in l:
if t not in code_doc:
code_doc.setdefault(t, 1)
else:
code_doc[t] += 1

for keys in code_doc.keys():
print(keys + ' ' + str(code_doc[keys]))

❹ python,字符串怎么统计单词个数

如果你是指一串单词,空格隔开的,统计词频,就用列表和字典来。
比如输入的是这样:this one ok this one two three go end at end
dic1={}
n=input().split()
for i in n:
缩进if i in dic1:dic1[i]+=1
缩进else:dic1[i]=1
print(dic1)

❺ python 可以统计出 一个词的出现的次数的代码

先用split()将输入切分成一个列表,获得列表data
然后用列表统计函数data.count('aa') 就能统计出有多少个aa
具体自己写写吧。

❻ python jieba 统计词数问题

看不到前面的代码,但从后面的代码来看,counts不是集合而是字典对象。



如果前面是这样初始化counts处理的,你可以看到counts是一个dict的类型

❼ 如何用python统计单词的频率

代码:

passage="""Editor’s Note: Looking through VOA's listener mail, we came across a letter that asked a simple question. "What do Americans think about China?" We all care about the perceptions of others. It helps us better understand who we are. VOA Reporter Michael Lipin begins a series providing some answers to our listener's question. His assignment: present a clearer picture of what Americans think about their chief world rival, and what drives those perceptions.

Two common American attitudes toward China can be identified from the latest U.S. public opinion surveys published by Gallup and Pew Research Center in the past year.

First, most of the Americans surveyed have unfavorable opinions of China as a whole, but do not view the country as a threat toward the United States at the present time.

Second, most survey respondents expect China to pose an economic and military threat to the United States in the future, with more Americans worried about the perceived economic threat than the military one.

Most Americans view China unfavorably

To understand why most Americans appear to have negative feelings about China, analysts interviewed by VOA say a variety of factors should be considered. Primary among them is a lack of familiarity.

"Most Americans do not have a strong interest in foreign affairs, Chinese or otherwise," says Robert Daly, director of the Kissinger Institute on China and the United States at the Washington-based Wilson Center.

Many of those Americans also have never traveled to China, in part because of the distance and expense. "That means that like most human beings, they take short cuts to understanding China," Daly says.

Rather than make the effort to regularly consume a wide range of U.S. media reports about China, analysts say many Americans base their views on widely-publicized major events in China's recent history."""

passage=passage.replace(","," ").replace("."," ").replace(":"," ").replace("’","'").

replace('"'," ").replace("?"," ").replace("!"," ").replace(" "," ")#把标点改成空格

passagelist=passage.split(" ")#拆分成一个个单词

pc=passagelist.()#复制一份

for i in range(len(pc)):

pi=pc[i]#这一个字符串

if pi.count(" ")==len(pi):#如果全是空格

passagelist.remove(pi)#删除此项

worddict={}

for j in range(len(passagelist)):

pj=passagelist[j]#这一个单词

if pj not in worddict:#如果未被统计到

worddict[pj]=1#增加单词统计,次数设为1

else:#如果统计过了

worddict[pj]+=1#次数增加1

output=""#按照字母表顺序,制表符

worddictlist=list(worddict.keys())#提取所有的单词

worddictlist.sort()#排序(但大小写会出现问题)

worddict2={}

for k in worddictlist:

worddict2[k]=worddict[k]#排序好的字典

print("单次 次数")

for m in worddict2:#遍历输出

tabs=(23-len(m))//8#根据单次长度输入,如果复制到表格,请把此行改为tabs=2

print("%s%s%d"%(m," "*tabs,worddict[m]))

注:加粗部分是您要统计的短文,请修改。我这里的输出效果是:

American 1

Americans 9

Center 2

China 10

China's 1

Chinese 1

Daly 2

Editor's 1

First 1

Gallup 1

His 1

Institute 1

It 1

Kissinger 1

Lipin 1

Looking 1

Many 1

Michael 1

Most 2

Note 1

Pew 1

Primary 1

Rather 1

Reporter 1

Research 1

Robert 1

S 2

Second 1

States 3

That 1

To 1

Two 1

U 2

United 3

VOA 2

VOA's 1

Washington-based1

We 1

What 1

Wilson 1

a 10

about 6

across 1

affairs 1

all 1

also 1

among 1

an 1

analysts 2

and 5

answers 1

appear 1

are 1

as 2

asked 1

assignment 1

at 2

attitudes 1

base 1

be 2

because 1

begins 1

beings 1

better 1

but 1

by 2

came 1

can 1

care 1

chief 1

clearer 1

common 1

considered 1

consume 1

country 1

cuts 1

director 1

distance 1

do 3

drives 1

economic 2

effort 1

events 1

expect 1

expense 1

factors 1

familiarity 1

feelings 1

foreign 1

from 1

future 1

have 4

helps 1

history 1

human 1

identified 1

in 5

interest 1

interviewed 1

is 1

lack 1

latest 1

letter 1

like 1

listener 1

listener's 1

mail 1

major 1

make 1

many 1

means 1

media 1

military 2

more 1

most 4

negative 1

never 1

not 2

of 10

on 2

one 1

opinion 1

opinions 1

or 1

others 1

otherwise 1

our 1

part 1

past 1

perceived 1

perceptions 2

picture 1

pose 1

present 2

providing 1

public 1

published 1

question 2

range 1

recent 1

regularly 1

reports 1

respondents 1

rival 1

say 2

says 2

series 1

short 1

should 1

simple 1

some 1

strong 1

survey 1

surveyed 1

surveys 1

take 1

than 2

that 2

the 16

their 2

them 1

they 1

think 2

those 2

threat 3

through 1

time 1

to 7

toward 2

traveled 1

understand 2

understanding 1

unfavorable 1

unfavorably 1

us 1

variety 1

view 2

views 1

we 2

what 2

who 1

whole 1

why 1

wide 1

widely-publicized1

with 1

world 1

worried 1

year 1

(应该是对齐的,到这就乱了)

注:目前难以解决的漏洞

1、大小写问题,无法分辨哪些必须大写哪些只是首字母大写

2、's问题,目前如果含有只能算为一个单词里的

3、排序问题,很难做到按照出现次数排序

阅读全文

与python统计词相关的资料

热点内容
寿司解压系列全集视频 浏览:911
物体三维重建算法 浏览:982
fuli直播app哪个好 浏览:918
租办公室用什么app 浏览:104
医师定期考核刷题app哪个好 浏览:336
导出dmp文件命令 浏览:286
手机百度网盘怎么解压密码文件 浏览:583
索引重新编译 浏览:604
命令与征服4免cd补丁完美版 浏览:426
kotlin编译为native 浏览:140
家用编译机 浏览:549
电子加密货币最新政策 浏览:379
androidcanvas撤销 浏览:269
安卓手机怎么把图标全部下移 浏览:185
饥荒被服务器踢出怎么进 浏览:171
c编译器哪款好 浏览:732
快手宝哥发明什么app 浏览:822
张艳玲编译 浏览:67
android展开收起动画 浏览:237
linuxxz文件 浏览:160