How to pretty-print cyclic dictionaries?

Discussion in 'Python' started by Dietrich Bollmann, Apr 29, 2010.

  1. Hi,

    I would like to represent graphs as cyclic dictionaries in Python.

    The python code for the graphs is generated by some other program
    (written in lisp) and I wonder what would be the best syntax for writing
    the cycles in Python?

    The following works:

    >>> a = {}
    >>> a['a'] = a


    As can be seen here:

    >>> a

    {'a': {...}}
    >>> a['a']

    {'a': {...}}
    >>> a['a']['a']

    {'a': {...}}
    >>> a['a']['a']['a']

    {'a': {...}}
    >>>


    but I wonder if there is some easier syntax to represent the cycles?

    "Tags" like the following would be ideal:

    [0] {'a': [0]}

    but is there something like this in Python?

    Thanks, Dietrich


    PS: If there is such a representation, how could I make Python print it
    out?

    The normal printing is not very informative:

    >>> a

    {'a': {...}}

    And Pythons pretty-print functionality is not better either:

    >>> pprint.pprint(a)

    {'a': <Recursion on dict with id=3076782660>}

    Any idea?

    Thanks again :)
    Dietrich Bollmann, Apr 29, 2010
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Montgomery
    Replies:
    0
    Views:
    618
    Andrew Montgomery
    Sep 17, 2003
  2. Alan G Isaac

    pretty print 2D arrays

    Alan G Isaac, Aug 12, 2004, in forum: Python
    Replies:
    1
    Views:
    548
    Colin J. Williams
    Aug 13, 2004
  3. John Hunter
    Replies:
    5
    Views:
    656
    Paul McNett
    Sep 28, 2004
  4. lysdexia
    Replies:
    6
    Views:
    498
    John Machin
    Dec 2, 2007
  5. Brandon
    Replies:
    12
    Views:
    487
    Brandon
    Aug 15, 2008
Loading...

Share This Page