pprint defaultdict one record per line

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Mar 17 14:09:45 EDT 2013


On 17/03/2013 17:45, Peng Yu wrote:
> Hi,
>
> pprint can not print defaultdict one record per line. Is there some
> other convenient way in python to print one record per line?
>
> ~/linux/test/python/man/library/pprint/function/pprint$ ./main.py
> {'two': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], 'one': [0,
> 1, 2, 3, 4, 5, 6, 7, 8, 9]}
> {'one': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
>   'two': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]}
> defaultdict(<type 'list'>, {'two': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
> 11, 12, 13, 14], 'one': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]})
> ~/linux/test/python/man/library/pprint/function/pprint$ cat main.py
> #!/usr/bin/env python
>
> import pprint
>
> d=dict(one=range(10), two=range(15))
> print d
> pprint.pprint(d)
>
> from collections import defaultdict
> d=defaultdict(list)
> d['one']=range(10)
> d['two']=range(15)
> pprint.pprint(d)
>

Try writing a for loop that prints one record per line.

-- 
Cheers.

Mark Lawrence




More information about the Python-list mailing list