如何打印输入的份数?

时间:2017-01-19 01:26:49

标签: python-3.x

这是我的代码:

import re

t = int(input())
nentry = int(input())
def nitems():
    for number in range(nentry):
        pt = []
        items = input()[:10] 
        pt.append(items)
        if re.match("^[a-z]*$",items):
          print(pt)
        else:
          None
n = nitems()

让我们说一个例子,我会输入:

1
6
same
same 
same
different
different
different

我希望它输出复制字符串的次数(基数是1.不介意打印和列表)所以输出应该是:

2

每个字符串的基数为1,输入为三个“相同”字符串,因此副本为两个。 “不同”字符串也是如此。那我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

from collections import Counter, defaultdict

t = int(input())
nentry = int(input())
cnt = Counter()

def nitems():
    for number in range(nentry):
        item = input()
        cnt[item] += 1

    v = defaultdict(list)

    for key, value in sorted(cnt.items()):
        v[value].append(key)

    for key in v.keys():
        print(key)

n = nitems()

演示:

1
4
alpha
alpha
omega
omega

输出:

2