How to print the name of a list?

O

oriana.falco

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
 
L

Lonnie Princehouse

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.
 
?

=?ISO-8859-1?Q?Tomi_Ky=F6stil=E4?=

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 :)
 
O

oriana.falco

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

THANKS!!!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top