Erw*_*nel 2 python count duplicates python-3.x
我有一个问题,我必须在Python(v3.4.1)中计算重复的单词并将它们放在一个句子中.我使用了计数器,但我不知道如何按以下顺序获取输出.输入是:
mysentence = As far as the laws of mathematics refer to reality they are not certain as far as they are certain they do not refer to reality
Run Code Online (Sandbox Code Playgroud)
我把它变成了一个列表并对其进行了排序
输出假设是这样的
"As" is repeated 1 time.
"are" is repeated 2 times.
"as" is repeated 3 times.
"certain" is repeated 2 times.
"do" is repeated 1 time.
"far" is repeated 2 times.
"laws" is repeated 1 time.
"mathematics" is repeated 1 time.
"not" is repeated 2 times.
"of" is repeated 1 time.
"reality" is repeated 2 times.
"refer" is repeated 2 times.
"the" is repeated 1 time.
"they" is repeated 3 times.
"to" is repeated 2 times.
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经到了这一步
x=input ('Enter your sentence :')
y=x.split()
y.sort()
for y in sorted(y):
print (y)
Run Code Online (Sandbox Code Playgroud)
我可以通过排序看到你要去哪里,因为你可以可靠地知道你何时触及一个新单词并跟踪每个独特单词的计数.但是,您真正想要做的是使用哈希(字典)来跟踪计数,因为字典键是唯一的.例如:
words = sentence.split()
counts = {}
for word in words:
if word not in counts:
counts[word] = 0
counts[word] += 1
Run Code Online (Sandbox Code Playgroud)
现在,这将为您提供一个字典,其中键是单词,值是它出现的次数.有些事情你可以做,collections.defaultdict(int)所以你可以添加价值:
counts = collections.defaultdict(int)
for word in words:
counts[word] += 1
Run Code Online (Sandbox Code Playgroud)
但是甚至还有更好的东西...... collections.Counter它将把你的单词列表转换成包含计数的字典(实际上是字典的扩展).
counts = collections.Counter(words)
Run Code Online (Sandbox Code Playgroud)
从那里你需要按排序顺序的单词列表及其计数,以便您可以打印它们. items()将为您提供一个元组列表,并将sorted按每个元组的第一项(本例中的单词)排序(默认情况下)......这正是您想要的.
import collections
sentence = """As far as the laws of mathematics refer to reality they are not certain as far as they are certain they do not refer to reality"""
words = sentence.split()
word_counts = collections.Counter(words)
for word, count in sorted(word_counts.items()):
print('"%s" is repeated %d time%s.' % (word, count, "s" if count > 1 else ""))
Run Code Online (Sandbox Code Playgroud)
OUTPUT
"As" is repeated 1 time.
"are" is repeated 2 times.
"as" is repeated 3 times.
"certain" is repeated 2 times.
"do" is repeated 1 time.
"far" is repeated 2 times.
"laws" is repeated 1 time.
"mathematics" is repeated 1 time.
"not" is repeated 2 times.
"of" is repeated 1 time.
"reality" is repeated 2 times.
"refer" is repeated 2 times.
"the" is repeated 1 time.
"they" is repeated 3 times.
"to" is repeated 2 times.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19548 次 |
| 最近记录: |