python代碼統計
❶ python函數統計字元串中字母數學其他字元的個數
代碼如下:
text = "Hello Python,Hello 2021."
letter = 0
digital = 0
other = 0
for i in text:
if i.isalpha():
letter += 1
elif i.isdigit():
digital += 1
else:
other += 1
print('字母:{} 數字:{} 其他:{}'.format(letter,digital,other))
輸出:
字母:16 數字:4 其他:4
下面是Python內置關於判斷字元串類型的方法介紹:
str.isalnum()
如果字元串中的所有字元都是字母或數字且至少有一個字元,則返回True, 否則返回False。 如果c.isalpha(),c.isdecimal(),c.isdigit(),或c.isnumeric()之中有一個返回True,則字元``c``是字母或數字。
str.isalpha()
如果字元串中的所有字元都是字母,並且至少有一個字元,返回True,否則返回False。字母字元是指那些在 Unicode 字元資料庫中定義為 "Letter" 的字元,即那些具有 "Lm"、"Lt"、"Lu"、"Ll" 或 "Lo" 之一的通用類別屬性的字元。 注意,這與 Unicode 標准中定義的"字母"屬性不同。
str.isascii()
如果字元串為空或字元串中的所有字元都是 ASCII ,返回True,否則返回False。ASCII 字元的碼點范圍是 U+0000-U+007F 。
3.7 新版功能.
str.isdecimal()
如果字元串中的所有字元都是十進制字元且該字元串至少有一個字元,則返回True, 否則返回False。十進制字元指那些可以用來組成10進制數字的字元,例如 U+0660 ,即阿拉伯字母數字0 。 嚴格地講,十進制字元是 Unicode 通用類別 "Nd" 中的一個字元。
str.isdigit()
如果字元串中的所有字元都是數字,並且至少有一個字元,返回True,否則返回False。 數字包括十進制字元和需要特殊處理的數字,如兼容性上標數字。這包括了不能用來組成 10 進制數的數字,如 Kharosthi 數。 嚴格地講,數字是指屬性值為 Numeric_Type=Digit 或 Numeric_Type=Decimal 的字元。
str.isidentifier()
如果字元串是有效的標識符,返回True,依據語言定義,標識符和關鍵位元組。
調用keyword.iskeyword()來檢測字元串s是否為保留標識符,例如def和class。
❷ 如何用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接收輸入一個字元串,統計其中小寫字母的個數
可以這樣編寫程序:
1、定義一個含有所有小寫字母的列表變數w及一個待測字元串變數s。
2、對s字元串中的每一個字元進行循環迭代檢測其是否位於變數w中,若為真,則對計數變數c進行加一操作。
3、輸出c變數,即為所求。
具體代碼及運行示例如下圖所示:
程序代碼及示例運行結果
❹ python統計單詞中字母個數
代碼:
string = "T-shirt"
letters = []
for i in range(65, 91):
letters.append(chr(i))#A-Z
for i in range(97, 123):
letters.append(chr(i))#a-z
num = 0
for i in string:
if i in letters:#如果是字母
num += 1
print(num)#輸出num
運行效果:
6
備註:加粗部分的單詞可以自己改。
解析:
通過ASCII碼和chr函數查找到所有大小寫字母,再逐一遍歷單詞,如果是字母就+1個,這樣就可以得到統計數據了。
❺ Python 統計列表裡面有多少個元素
Python 統計列表裡面有多少個元素步驟如下:
1、打開python語言命令窗口,定義一個列表變數Z並列印對應的列表值。
❻ python統計excel數據分析
咨詢記錄 · 回答於2021-12-08
❼ Python如何統計一行數據同時滿足A和B的次數
可以用pandas
也可以直接用csv庫讀入然後for循環計算