简体   繁体   English

如何在python中并排打印两个计数器?

[英]How can I print two counter side by side in python?

I have a function like this.I need to print two dictionaries side by side in python.我有一个这样的函数。我需要在 python 中并排打印两个字典。

def kelime_sayma(metin):
    kelimeler = metin.split()
    kelime_sayi = Counter(kelimeler)

    for i,value in kelime_sayi.most_common():
        print('{}    {}'.format(i, value))
    for j,value in sorted(kelime_sayi.items()):
        print('{}    {}'.format(j, value))

You can try :你可以试试 :

>>> a
['c', 'a', 'b', 'b', 'c', 'b', 'b', 'b', 'a', 'a', 'b', 'a', 'b', 'c', 'c', 'a', 'c', 'a', 'a', 'b', 'a', 'a', 'c', 'a', 'b', 'c', 'c', 'c', 'b', 'a']
>>> b=Counter(a)
>>> b
Counter({'a': 11, 'b': 10, 'c': 9})
>>> for i,j in zip(b.most_common(), b.items()):
...     print('{} {} {} {}'.format(i[0], i[1], j[0], j[1]))

Output :输出

a 11 c 9
b 10 a 11
c 9 b 10

Question : print two dictionaries side by side问题:并排打印两本词典

    for i, v1, v2 in enumerate(zip(kelime_sayi.most_common(), sorted(kelime_sayi.items()), 1):
        print('{}    {} {}'.format(i, v1, v2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM