print names of dictionaries

Discussion in 'Python' started by BartlebyScrivener, Apr 26, 2006.

  1. 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
     
    BartlebyScrivener, Apr 26, 2006
    #1
    1. Advertising

  2. BartlebyScrivener

    jay graves Guest

    jay graves, Apr 26, 2006
    #2
    1. Advertising

  3. 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
     
    Thomas Nelson, Apr 26, 2006
    #3
  4. 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
     
    BartlebyScrivener, Apr 26, 2006
    #4
  5. 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
     
    BartlebyScrivener, Apr 26, 2006
    #5
  6. 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
     
    Thomas Nelson, Apr 26, 2006
    #6
  7. >> 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
     
    BartlebyScrivener, Apr 26, 2006
    #7
  8. 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
     
    Scott David Daniels, Apr 26, 2006
    #8
  9. 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
     
    Thomas Nelson, Apr 27, 2006
    #9
  10. 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
     
    BartlebyScrivener, Apr 27, 2006
    #10
  11. 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 = 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
     
    Philippe Martin, Apr 27, 2006
    #11
  12. OK, totally dumb !

    g_dict = 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 = 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
     
    Philippe Martin, Apr 27, 2006
    #12
    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. keto
    Replies:
    0
    Views:
    958
  2. lysdexia
    Replies:
    6
    Views:
    504
    John Machin
    Dec 2, 2007
  3. Brandon
    Replies:
    12
    Views:
    490
    Brandon
    Aug 15, 2008
  4. David Cournapeau

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

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    358
    David Cournapeau
    Dec 30, 2008
  5. Dietrich Bollmann

    How to pretty-print cyclic dictionaries?

    Dietrich Bollmann, Apr 29, 2010, in forum: Python
    Replies:
    0
    Views:
    268
    Dietrich Bollmann
    Apr 29, 2010
Loading...

Share This Page