简体   繁体   中英

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.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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