当前位置:网站首页>Word frequency statistics (string, list)
Word frequency statistics (string, list)
2022-06-30 05:21:00 【Clever little lion】
Count the number of occurrences of each letter in the string
1. utilize Counter function
from collections import Counter
c = Counter("loonbalxballpoon")
print(c)
- Building an empty dictionary , Traverse
str1="loonbalxballpoon"
gg={
}
for i in str1:
gg[i]=0
for i in str1:
gg[i]+=1
print(gg)
result :
Counter({
'l': 4, 'o': 4, 'n': 2, 'b': 2, 'a': 2, 'x': 1, 'p': 1})
leetcode242:
Given two strings s and t , Write a function to determine t Whether it is s Letter heteronym of .
Be careful : if s and t Each character in the has the same number of occurrences , said s and t They are mutually alphabetic words .
Example 1:
Input : s = “anagram”, t = “nagaram”
Output : true
def isAnagram(s: str, t: str) -> bool:
letter_frequcy_ransomNote = {
}
for i in range(len(s)):
letter_frequcy_ransomNote[s[i]] = 0
for i in range(len(s)):
letter_frequcy_ransomNote[s[i]] += 1
letter_frequcy_magazine = {
}
for i in range(len(t)):
letter_frequcy_magazine[t[i]] = 0
for i in range(len(t)):
letter_frequcy_magazine[t[i]] += 1
if letter_frequcy_magazine==letter_frequcy_ransomNote:
return True
else:
return False
leetcode1189:
Give you a string text, You need to use text To piece together as many words as possible “balloon”( balloon ).
character string text Each letter in can only be used once at most . Please return the maximum number of words you can piece together “balloon”.
def maxNumberOfBalloons(text: str) -> int:
# Method 1 :
# dict_text = {'b':0,'a':0,'l':0,'o':0,'n':0}
# for x in text:
# if x in dict_text:
# dict_text[x] += 1
# dict_text['l'] //= 2
# dict_text['o'] //= 2
# return min(dict_text.values())
# Method 2 :
c = Counter(text)
c['l']//=2
c['o']//=2
return min(c['l'],c['o'],c['b'],c['n'],c['a'])
3. utilize jieba
list1=jieba.lcut(" I love Tsinghua University and Peking University ")
#jieba Of lcut Function to segment a string into words and phrases , It's a list back
print(list1)
print(Counter(list1))
result :
[' I ', ' Love ', ' Tsinghua University ', ' and ', ' Peking University, ']
Counter({
' I ': 1, ' Love ': 1, ' Tsinghua University ': 1, ' and ': 1, ' Peking University, ': 1})
Count the number of times each letter appears in the list
The same way of thinking
d=Counter(['aa','bb','cc','aa'])
print(d)
Counter({
'aa': 2, 'bb': 1, 'cc': 1})
边栏推荐
- Virtual and pure virtual destructions
- Unity + hololens2 performance test
- Another download address for typro
- 使用码云PublicHoliday项目判断某天是否为工作日
- 【VCS+Verdi聯合仿真】~ 以計數器為例
- 【LeetCode】Easy | 232. Using stack to realize queue (pure C manual tearing stack)
- Writing unityshader with sublimetext
- Revit secondary development - use panel function without opening the project
- Pit of smoothstep node in shadergraph
- Unity screenshot method
猜你喜欢

PWN Introduction (2) stack overflow Foundation

Intellj idea generates jar packages for projects containing external lib to other projects. The method refers to the jar package written by itself

Responding with flow layout

Procedural animation -- inverse kinematics of tentacles

Force buckle 27 Removing Elements

企事业单位源代码防泄露工作该如何进行

Writing unityshader with sublimetext

遥感图像/UDA:Curriculum-Style Local-to-Global Adaptation for Cross-Domain Remote Sensing Image Segmentat

【LeetCode】Easy | 225. Using queue to realize stack (pure C manual tearing queue)

Intellj idea jars projects containing external lib to other project reference methods - jars
随机推荐
Configuration and use of controllers and routes in nestjs
Unity call Exe program
Introduction to mmcv common APIs
[typescript] experimentaldecorators of vscode stepping pit
Unity3d learning notes-1 (C # learning)
Remote sensing image /uda:curriculum style local to global adaptation for cross domain remote sensing image segmentation
很紧张,第一天做软件测试,需要做什么?
Network communication problem locating steps
C # uses monopinvokecallback to directly call back C # function
Unity mobile end sliding screen rotation
Chapter 8 primitive processing of OpenGL super classic (version 7)
How to use js to control the scroll bar of moving div
Intellj idea jars projects containing external lib to other project reference methods - jars
Unity supports the platform # define instruction of script
Chapter 12 pipeline monitoring of OpenGL super classic (version 7)
网络变压器怎么判断好坏?网络滤波变压器坏了一般是什么症状?
Operation of JSON file
Revit二次开发---未打开项目使用面板功能
Unity dotween plug-in description
[typescript] cannot redeclare block range variables