Fun python 3.2 one-liner

Terry Reedy tjreedy at udel.edu
Tue Mar 29 16:06:44 EDT 2011


On 3/29/2011 5:50 AM, Raymond Hettinger wrote:
> from collections import Counter
> from itertools import product
>
> print('\n'.join('*'*(c//2000) for _,c in sorted(Counter(map(sum,
> product(range(6), repeat=8))).items())))

The line break makes that hard to read; the axis is not labeled (and 
labels help understand the code). So my suggested revision is:

from collections import Counter
from itertools import product

print('\n'.join('{:2d}: {}'.format(i, '*'*(c//2000)) for i,c in sorted(
     Counter(map(sum,product(range(6), repeat=8))).items() )))

  0:
  1:
  2:
  3:
  4:
  5:
  6:
  7: *
  8: ***
  9: *****
10: ********
11: ************
12: ******************
13: *************************
14: ********************************
15: *****************************************
16: *************************************************
17: ********************************************************
18: **************************************************************
19: ******************************************************************
20: *******************************************************************
21: ******************************************************************
22: **************************************************************
23: ********************************************************
24: *************************************************
25: *****************************************
26: ********************************
27: *************************
28: ******************
29: ************
30: ********
31: *****
32: ***
33: *
34:
35:
36:
37:
38:
39:
40:

-- 
Terry Jan Reedy




More information about the Python-list mailing list