1

I have a list of a random word as a string and I need to count the frequency of it.

a = ['a','ccc','bb','ccc','a','ccc','bb','bb','a','bb']

I want to make it into a loop. So the output will be

   1 a          3
   2 bb         4
   3 ccc        3

with the number is aligned in the right with 4 spaces or character in the left, elements on the list are aligned in the left with 5 characters in the left and the frequency aligned in the right like above.

I know how to count the frequency but I don't know how to arrange them

total_word = {}
for word in clear_word:
    if word not in total_word:
        total_word[word] = 0
    total_word[word] += 1

Sorry to interrupt

7 Answers7

3

There are at least two efficient ways:

from collections import Counter, defaultdict
a = ['a','ccc','bb','ccc','a','ccc','bb','bb','a','bb']
# method 1:
d = defaultdict(int)
for elem in a:
    d[elem] += 1
for ctr, k in enumerate(sorted(d), start = 1):
    print(ctr,k,'\t',d[k])

# method 2:
d = Counter(a)
for ctr, k in enumerate(sorted(d), start = 1):
    print(ctr,k,'\t',d[k])

Output:

1 a      3
2 bb     4
3 ccc    3

EDIT: Here you go:

a = ['a','ccc','bb','ccc','a','ccc','bb','bb','a','bb']
unique = sorted(set(a))
for ctr, i in enumerate(unique,start=1):
    print(ctr,i,'\t',a.count(i))
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
1

Try this, using collections.Counter

>>> from collections import Counter
>>> i=1
>>> for k, v in Counter(a).items():
        print(f"{i:<3} {k:<10} {v}")
        i+=1

Output:

1   a          3
2   ccc        3
3   bb         4
shaik moeed
  • 5,300
  • 1
  • 18
  • 54
1

If you like me occasionally have to work on python less then 2.6 you could use oldscool string-formatting like:

print "%3s %-10s %s" % (i, the_word, count)

Here:

  • %3s will occupy 3 characters and get you left aligned text
  • %-10s will occupy 10 characters and be right (the minus sign) aligned

This formatting will work in any python-version.

UlfR
  • 4,175
  • 29
  • 45
  • Why use this over f-strings or `.format()`? – Dan Oct 15 '19 at 12:13
  • As stated in my message, this is "oldscool". But its working and if you grew up using other ancient things like C or `awk` that notation is more familiar. But of course you should preferably learn the `.format()` notation since it gives you greater power and flexibility. – UlfR Oct 15 '19 at 12:30
0

Hi based on your code this loop display your results

for a in total_word.keys():
     print(a ,'\t',  total_word[a])

This my output

 a       3
 bb      4
 ccc     3
Jad
  • 109
  • 10
0

You can align your columns using f-strings (see here to understand the :>{padding}):

padding = max(len(word) for word in total_word.keys()) + 1
for word, count in total_word.items():
    print(f"{word:<{padding}}{count:>3}")

if you wanted the index as well then add in enumerate:

for idx, (word, count) in enumerate(total_word.items()):
    print(f"{idx:<3}{word:<{padding}}{count:>3}")

Putting it all together:

clear_word = ['a'] * 3 + ['ccc'] * 4 + ['bb'] * 10

total_word = {}
for word in clear_word:
    if word not in total_word:
        total_word[word] = 0
    total_word[word] += 1

padding = max(len(word) for word in total_word.keys()) + 1
for idx, (word, count) in enumerate(total_word.items()):
    print(f"{idx:<3}{word:<{padding}}{count:>3}")

your output is

0  a     3
1  ccc   4
2  bb   10
Dan
  • 45,079
  • 17
  • 88
  • 157
0

If it is only about string formatting, you can prepare string similarly to:

arr = ['a', 'bb', 'ccc']

for i in range(len(arr)):
  print('{} {:4} {}'.format(i, arr[i], i+5))

I am using this site as a resource for string formatting https://pyformat.info/#string_pad_align

Dolfa
  • 796
  • 4
  • 21
0

I guess this is what your want:

Try this:

clear_word = ['a','ccc','bb','ccc','a','ccc','bb','bb','a','bb','bb','bb','bb','bb','bb','bb','bb','bb','bb']
total_word = {}
for word in clear_word:
    if word not in total_word:
        total_word[word] = 0
    total_word[word] += 1

for i, k in enumerate(total_word):
    print("    {0:2} {1:3}     {2:5}".format(i, k, total_word[k]))

That is output: align output

Siro
  • 76
  • 5