473,395 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

print names of dictionaries

Still new. Learning attributes and functions and so on.

Sorry if this is obvious, but if I'm defining a function for some
dictionaries, how can I print just the names of the dictionaries?

E.g. assuming apps, dirs, sites are dictionaries defined in the module,
how can I print just their names before doing other things with them.

def printdict(dicts=[apps, dirs, sites]):
for dict in dicts:
print ???

Thank you

rpd

Apr 26 '06 #1
11 1253
BartlebyScrivener wrote:
Still new. Learning attributes and functions and so on.
Sorry if this is obvious, but if I'm defining a function for some
dictionaries, how can I print just the names of the dictionaries?


Short answer: You can't.

http://pyfaq.infogami.com/how-can-my...e-of-an-object

....
Jay Graves

Apr 26 '06 #2
Here's an OO way that may do what you want:
class MyD(dict): .... def __init__(self,dic,rep):
.... dict.__init__(self,dic)
.... self.rep = rep
.... def __repr__(self):
.... return self.rep
.... apps = MyD({'alpha':1,'beta':2},'apps')
apps apps apps.keys()

['alpha', 'beta']

Of course, the easiest way is just to use a tuple (dict,string).

As a side note, since dict is a builtin type and function, it might not
be good style to use it as a loop variable.

THN

Apr 26 '06 #3
Wow,

That's food for thought. Thanks.

I see what they mean about change of approach. I'll just stick a key in
each dictionary called, er, name with its name value.

Thank you!

rick

Apr 26 '06 #4
Yikes,

I'll have to come back to the OO way in a month or two ;)

This works for now. I just added their names as values:

def printdict(dictionaries=[apps, dirs, sites]):
for dictionary in dictionaries:
print dictionary["name"]
keys = dictionary.keys()
keys.sort()
for key in keys:
if key != "name":
print key, ":",dictionary[key]
print '\n',

Thank you both for your help.

rpd

Apr 26 '06 #5
Here's an OO way that may do what you want:
class MyD(dict): .... def __init__(self,dic,rep):
.... dict.__init__(self,dic)
.... self.rep = rep
.... def __repr__(self):
.... return self.rep
.... apps = MyD({'alpha':1,'beta':2},'apps')
apps apps apps.keys()

['alpha', 'beta']

Of course, the easiest way is just to use a tuple (dict,string).

THN

Apr 26 '06 #6
>> Of course, the easiest way is just to use a tuple (dict,string).

I don't mean to be obtuse, but I'm not getting this one either. Is it
easier than what I did?

Thx,

rick

Apr 26 '06 #7
BartlebyScrivener wrote:
This works for now. I just added their names as values:

def printdict(dictionaries=[apps, dirs, sites]):
for dictionary in dictionaries:
print dictionary["name"]
keys = dictionary.keys()
keys.sort()
for key in keys:
if key != "name":
print key, ":",dictionary[key]
print '\n',

Thank you both for your help.


You might want to use a key of '_name', or '_name_', because
'name' is a fairly likely name to encounter.

--Scott David Daniels
sc***********@acm.org
Apr 26 '06 #8
I meant something like
def printdict(dictionaries=[(apps,'apps'), (dirs,'dirs'),
(sites,'sites')]):
for dictionary,name in dictionaries:
print name
keys = dictionary.keys()
keys.sort()
for key in keys:
print key, ":",dictionary[key]
print '\n',

It's not really easier than what you did. Instead of making sure every
dictionary in the dictionaries argument contains a 'name' key that does
what you expect, you must make sure the argument passed in is a list of
(dictionary, name) pairs. It's a little better in my personal opinion,
because you don't have to modify the dictionaries themselves, and it
avoids the problem of 'name' already existing in the dictionary, as
described by Scott Daniels. But I suppose that's only one opinion.

THN

Apr 27 '06 #9
Thomas,

Thanks. I read about tuple packing and unpacking. Now I get to see it
in action. Plus, yours is one line shorter. If programming is anything
like writing, shorter is almost always better.

Thanks,

rick

Apr 27 '06 #10
Hi,

I do not know if there is a way to overload the instantiation of all objects
in Python but I thought of something like this to fetch any object with its
name:

g_dict = {}
def create_object (v,s):
p = v
g_dict[s] = id(p)
return p

#ex
object = create_object ([1,2,3,4], 'A LIST')

Philippe

Thomas Nelson wrote:
Here's an OO way that may do what you want:
class MyD(dict): ... def __init__(self,dic,rep):
... dict.__init__(self,dic)
... self.rep = rep
... def __repr__(self):
... return self.rep
... apps = MyD({'alpha':1,'beta':2},'apps')
apps apps apps.keys()

['alpha', 'beta']

Of course, the easiest way is just to use a tuple (dict,string).

THN


Apr 27 '06 #11
OK, totally dumb !

g_dict[s] = p


Philippe Martin wrote:
Hi,

I do not know if there is a way to overload the instantiation of all
objects in Python but I thought of something like this to fetch any object
with its name:

g_dict = {}
def create_object (v,s):
p = v
g_dict[s] = id(p)
return p

#ex
object = create_object ([1,2,3,4], 'A LIST')

Philippe

Thomas Nelson wrote:
Here's an OO way that may do what you want:
> class MyD(dict):

... def __init__(self,dic,rep):
... dict.__init__(self,dic)
... self.rep = rep
... def __repr__(self):
... return self.rep
...
> apps = MyD({'alpha':1,'beta':2},'apps')
> apps

apps
> apps.keys()

['alpha', 'beta']

Of course, the easiest way is just to use a tuple (dict,string).

THN


Apr 27 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Till Plewe | last post by:
Is there a way to speed up killing python from within a python program? Sometimes shutting down takes more than 10 times as much time as the actual running of the program. The programs are...
23
by: stewart.midwinter | last post by:
No doubt I've overlooked something obvious, but here goes: Let's say I assign a value to a var, e.g.: myPlace = 'right here' myTime = 'right now' Now let's say I want to print out the two...
210
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again...
4
by: Livin | last post by:
I need to dynamically create dictionary names using strings input at the time of creation. These will then be placed into a "Parent" dictionary. I'm new to python, and programming, so please bear...
8
by: placid | last post by:
Hi all, Just wondering if anyone knows how to pop up the dialog that windows pops up when copying/moving/deleting files from one directory to another, in python ? Cheers
69
by: Edward K Ream | last post by:
The pros and cons of making 'print' a function in Python 3.x are well discussed at: http://mail.python.org/pipermail/python-dev/2005-September/056154.html Alas, it appears that the effect of...
1
by: beef | last post by:
Hello all, I am using MySQLdb 1.2.2 and have a question about the construction of the dictionary keys of a result set. Here is an example query, from which you may intuit some of the...
1
by: kirby.urner | last post by:
It's probably not news to anyone here that Python 3 gives us access to the unicode codespace for Python names, not just string literals, meaning our functions and classes, lists and dictionaries,...
14
by: cnb | last post by:
Are dictionaries the same as hashtables?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.