python - 如何按接收顺序获取 Counter 对象的值?
问题描述
任务:第一行包含整数 N。接下来的 N 行每行包含一个单词。输出应该是: 1) 在第一行,输出输入中不同单词的数量。2) 在第二行,根据输入中的出现输出每个不同单词的出现次数。我对#1没有任何困难。对于第 2 点,我使用 Counter 来获取单词的出现次数。但是,我很难按照收到的顺序打印它们。下面是我的代码。
from collections import Counter
from collections import OrderedDict
all_words=[]
for _ in range(int(raw_input())):
name=raw_input()
all_words.append(name)
uniqlst=list(set(all_words))
print len(uniqlst)##On the first line, output the number of distinct words from the input.
x=OrderedDict(Counter(all_words)) #This is where I am having trouble to get values of x in the order it was received.
print " ".join(map(str,x.values()))
输入:
4
bcdef
abcdef
bcde
bcdef
我的代码的输出:
3
1 1 2
预期输出:
3
2 1 1
解决方案
这是行不通的:
x=OrderedDict(Counter(all_words))
首先,您Counter
通过迭代创建 a all_words
。由于 aCounter
只是底层的 a dict
,根据您的 Python 版本,这可能是插入顺序、一致但任意顺序或显式随机顺序。
OrderedDict
然后你通过迭代它来创建一个Counter
。这将保留- 如果是任意顺序Counter
,这将不是很有用。Counter
您要做的是创建一个可以做所有事情Counter
但也可以做所有事情的类OrderedDict
。这是微不足道的:
class OrderedCounter(Counter, OrderedDict):
'Counter that remembers the order elements are first encountered'
这不是很完美,因为它repr
会给你错误的类名,而且它不会正确腌制。但修复它几乎一样简单。事实上,它在文档中作为示例给出:
class OrderedCounter(Counter, OrderedDict):
'Counter that remembers the order elements are first encountered'
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))
def __reduce__(self):
return self.__class__, (OrderedDict(self),)
推荐阅读
- excel - 使用带负数的 IFS
- r - 如何使用 cbind 将 300 列数据集中的 2 行从一个 df 传输到另一个
- r - 如何编辑此函数以便不需要指定迭代次数?
- python-3.x - 雪花无法识别数值
- docker - 在 debian:9-slim 容器下的 PHP 项目中启用 Xdebug
- get - 按 ID 获取 DynamoDB 返回空而不是 404
- python - 如何使用变量结果进行另一个计算python
- flutter - 如何使用 GetX 包管理 Flutter Web URL 路由?
- java - 为什么打印行中的 ahadot+asarot 相加而 ahadot1 和 asarot1 不相加?
- ios - 通过调用层次结构从下到上发出错误