当前位置:网站首页>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})
边栏推荐
- Unity 3D model operation and UI conflict Scrollview
- Unityshader learning notes - Basic Attributes
- Super comprehensive summary | related improvement codes of orb-slam2 / orb-slam3!
- Unity project hosting platform plasticscm (learn to use 2)
- Does the tester need to analyze the cause of the bug?
- Unity publishing /build settings
- Unity Catmull ROM curve
- 【LeetCode】Easy | 225. Using queue to realize stack (pure C manual tearing queue)
- Untiy3d controls scene screenshots through external JSON files
- 炒美原油的国际交易平台如何能保障资金安全呢?
猜你喜欢

Responding with flow layout

【 VCS + Verdi joint simulation】 ~ Taking Counter as an Example

Remote sensing image /uda:curriculum style local to global adaptation for cross domain remote sensing image segmentation

Generate a slice of mesh Foundation

9. naive Bayes

Xctf--Web--Challenge--area Wp

Unity C trigonometric function, right triangle corner calculation

East Tower attack and defense world - XSS bypasses the safety dog

Bev instance prediction based on monocular camera (iccv 2021)

The minecraft server address cannot be refreshed.
随机推荐
Terminal convenient SSH connection
Bessel curve with n control points
如何写论文
Force buckle 977 Square of ordered array
Very nervous. What should I do on the first day of software testing?
Pytorchcnn image recognition and classification model training framework
Unity shortcut key
Unity scroll view element drag and drop to automatically adsorb centering and card effect
Rotation, translation and scaling of unity VR objects
The fourth day of learning C language for Asian people
虚析构和纯虚析构
[notes] unity Scrollview button page turning
Nestjs入门和环境搭建
【VCS+Verdi聯合仿真】~ 以計數器為例
Visualization of 3D geological model based on borehole data by map flapping software
003-JS-DOM-Attr-innerText
Generate a slice of mesh Foundation
Unity + hololens common basic functions
E: Topic focus
Force buckle 209 Minimum length subarray