How to print the name of a list?

Discussion in 'Python' started by oriana.falco@thalesesec.com, Aug 22, 2005.

  1. Guest

    Hi!

    I was wondering what can I do to print the name of a list that is
    inside a list. For example:


    exported = ['123','456']
    imported = ['789','012']

    client1 = ['cl1b','cl1a']
    client2 = ['cl2a','cl2b']

    host = ['imported','exported']
    client = ['client1','client2']

    list = ['host', 'client']

    My goal is to print a string that reads:
    'host imported 789'
    ...and subsequently print more strings that read:
    'host imported 012'
    'host exported 123'
    'host exported 456'
    ......


    Thanks in advance for the help....
    Oriana
    , Aug 22, 2005
    #1
    1. Advertising

  2. In general, no --- there is no unique mapping between references and
    names.

    For debugging, however, it is sometimes useful to try this kind of
    reverse lookup by iterating over the global dictionary:

    def guess_name(thing):
    for name, reference in globals.iteritems():
    if thing is reference:
    return name
    else:
    return None

    # example use:

    foo = [1, 2, 3]
    bar = [4, 5, 6]
    baz = [foo, bar]

    for thing in baz:
    print guess_name(thing)

    # prints "foo" and "bar"


    I should emphasize that this is NOT failsafe, and it shouldn't be used
    in any way except for debugging IMHO. If you need to associate names
    with objects in a guaranteed way, use a dictionary.
    Lonnie Princehouse, Aug 22, 2005
    #2
    1. Advertising

  3. wrote:
    > Hi!
    >
    > I was wondering what can I do to print the name of a list that is
    > inside a list. For example:
    >
    >
    > exported = ['123','456']
    > imported = ['789','012']
    >
    > client1 = ['cl1b','cl1a']
    > client2 = ['cl2a','cl2b']
    >
    > host = ['imported','exported']
    > client = ['client1','client2']
    >
    > list = ['host', 'client']
    >
    > My goal is to print a string that reads:
    > 'host imported 789'
    > ...and subsequently print more strings that read:
    > 'host imported 012'
    > 'host exported 123'
    > 'host exported 456'
    > .....
    >
    >
    > Thanks in advance for the help....
    > Oriana
    >


    def printContents(seq, output=""):
    for itemname in seq:
    item = globals().get(itemname, None)
    if isinstance(item, (list, tuple)):
    printContents(item, "%s%s " % (output, itemname))
    else:
    print "%s%s" % (output, itemname)

    exported = ['123','456']
    imported = ['789','012']

    client1 = ['cl1b','cl1a']
    client2 = ['cl2a','cl2b']

    host = ['imported','exported']
    client = ['client1','client2']

    # you shouldn't reassing 'list', so let's call it something else
    namelist = ['host', 'client']

    printContents(namelist)


    Output:
    host imported 789
    host imported 012
    host exported 123
    host exported 456
    client client1 cl1b
    client client1 cl1a
    client client2 cl2a
    client client2 cl2b


    Let me know if this wasn't what you wanted :)

    --
    dOb
    =?ISO-8859-1?Q?Tomi_Ky=F6stil=E4?=, Aug 22, 2005
    #3
  4. Guest

    Wow, thanks.....that's a neat little function....I'll still check out
    the dictionary stuff. For now, this helps a lot though...

    THANKS!!!
    , Aug 22, 2005
    #4
    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. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    817
    Roedy Green
    Jan 2, 2006
  2. ding feng
    Replies:
    2
    Views:
    2,801
    ding feng
    Jun 25, 2003
  3. keto
    Replies:
    0
    Views:
    922
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    344
    David Cournapeau
    Dec 30, 2008
  5. Xavier Noria

    print LIST vs print join "", LIST

    Xavier Noria, Apr 16, 2004, in forum: Perl Misc
    Replies:
    14
    Views:
    194
    Uri Guttman
    Apr 19, 2004
Loading...

Share This Page