作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个用户输入文本的小程序,我想检查给定输入中给定单词出现了多少次。
# Read user input
print("Input your code: \n")
user_input = sys.stdin.read()
print(user_input)
比如我在程序中输入的文字是:
a=1
b=3
if (a == 1):
print("A is a number 1")
elif(b == 3):
print ("B is 3")
else:
print("A isn't 1 and B isn't 3")
要查找的单词在数组中指定。
wordsToFind = ["if", "elif", "else", "for", "while"]
基本上我想打印输入中出现了多少个“if”、“elif”和“else”。
如何统计用户输入的给定字符串中“if”、“elif”、“else”、“for”、“while”等词的出现次数?
最佳答案
我认为最好的选择是使用 python 的内置模块 tokenize
:
# Let's say this is tokens.py
import sys
from collections import Counter
from io import BytesIO
from tokenize import tokenize
# Get input from stdin
code_text = sys.stdin.read()
# Tokenize the input as python code
tokens = tokenize(BytesIO(code_text.encode("utf-8")).readline)
# Filter the ones in wordsToFind
wordsToFind = ["if", "elif", "else", "for", "while"]
words = [token.string for token in tokens if token.string in wordsToFind]
# Count the occurrences
counter = Counter(words)
print(counter)
假设您有一个 test.py
:
a=1
b=3
if (a == 1):
print("A is a number 1")
elif(b == 3):
print ("B is 3")
else:
print("A isn't 1 and B isn't 3")
然后你运行:
cat test.py | python tokens.py
输出:
Counter({'if': 1, 'elif': 1, 'else': 1})
只有正确的 python(语法上)才会被解析
您只会计算 python 关键字(不是每次 if 出现在代码文本中,例如,您可以有这样一行
a = "if inside str"
我认为if不应该被计算在内
关于python - 如何计算 Python 数组中指定单词的出现次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74278889/
我是一名优秀的程序员,十分优秀!