string.punctuation 删除一个值
string.punctuation deleting a value
所以我正在编写一个代码来计算#ed 的单词数量,如果一个单词没有#ed,它会忽略它。
当我运行代码:
import string
all = []
count = {}
word = []
line = input("Tweet: ").lower().strip(string.punctuation)
while line != '':
word.extend(line.split())
line = input("Tweet: ").lower().strip(string.punctuation)
for w in word:
if w.startswith('#'):
count[w] = count.get(w, 0) + 1
for word in sorted(count):
print(word, count[word])
我输入#Python是#AWESOME!
它输出#awesome 1 但没有输出#python 1
我需要它来输出所有#ed 单词并计算使用了多少。我认为问题是由 string.punctuation.
引起的
你说对了一部分!
.strip(string.punctuation)
是罪魁祸首。
根据 the Python docs,# 是 string.punctuation 集的一部分。
同样来自 the Python docs,在 strip
-函数上:
string.strip(s[, chars])
Return a copy of the string with leading and trailing characters
removed. If chars is omitted or None, whitespace characters are
removed. If given and not None, chars must be a string; the characters
in the string will be stripped from the both ends of the string this
method is called on.
因此,您要删除前导(例如第一个)#,并且保存在变量 line
中的字符串是 "python is #awesome"
。
您的 while 循环也永远不会退出,如 ".".strip(string.punctuation) == ""
。
看来您真的根本不想要 .strip
-方法。如果只在标点符号时删除最后一个字符,请使用 "your string".rstrip(string.punctuation)
代替
这可能对你有用
import string
all = []
count = {}
word = []
line = input("Tweet: ").lower().rstrip(string.punctuation)
while line != '.':
word.extend(line.split())
line = input("Tweet: ").lower().rstrip(string.punctuation)
for w in word:
if w.startswith('#'):
count[w] = count.get(w, 0) + 1
for word in sorted(count):
print(word, count[word])
或
import string
all = []
count = {}
word = []
line = input("Tweet: ").lower()
while line != '.':
word.extend(line.split())
line = input("Tweet: ").lower()
for w in word:
if w.startswith('#'):
w = w.rstrip(string.punctuation)
count[w] = count.get(w, 0) + 1
for word in sorted(count):
print(word, count[word])
所以我正在编写一个代码来计算#ed 的单词数量,如果一个单词没有#ed,它会忽略它。
当我运行代码:
import string
all = []
count = {}
word = []
line = input("Tweet: ").lower().strip(string.punctuation)
while line != '':
word.extend(line.split())
line = input("Tweet: ").lower().strip(string.punctuation)
for w in word:
if w.startswith('#'):
count[w] = count.get(w, 0) + 1
for word in sorted(count):
print(word, count[word])
我输入#Python是#AWESOME!
它输出#awesome 1 但没有输出#python 1
我需要它来输出所有#ed 单词并计算使用了多少。我认为问题是由 string.punctuation.
引起的你说对了一部分!
.strip(string.punctuation)
是罪魁祸首。
根据 the Python docs,# 是 string.punctuation 集的一部分。
同样来自 the Python docs,在 strip
-函数上:
string.strip(s[, chars])
Return a copy of the string with leading and trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the both ends of the string this method is called on.
因此,您要删除前导(例如第一个)#,并且保存在变量 line
中的字符串是 "python is #awesome"
。
您的 while 循环也永远不会退出,如 ".".strip(string.punctuation) == ""
。
看来您真的根本不想要 .strip
-方法。如果只在标点符号时删除最后一个字符,请使用 "your string".rstrip(string.punctuation)
代替
这可能对你有用
import string
all = []
count = {}
word = []
line = input("Tweet: ").lower().rstrip(string.punctuation)
while line != '.':
word.extend(line.split())
line = input("Tweet: ").lower().rstrip(string.punctuation)
for w in word:
if w.startswith('#'):
count[w] = count.get(w, 0) + 1
for word in sorted(count):
print(word, count[word])
或
import string
all = []
count = {}
word = []
line = input("Tweet: ").lower()
while line != '.':
word.extend(line.split())
line = input("Tweet: ").lower()
for w in word:
if w.startswith('#'):
w = w.rstrip(string.punctuation)
count[w] = count.get(w, 0) + 1
for word in sorted(count):
print(word, count[word])