English 中文(简体)
Python dictionary simple way to add a new key value pair
原标题:

Say you have,

foo =  bar 
d = { a-key : a-value }

And you want

d = { a-key : a-value , foo : bar }
e = { foo :foo}

I know you can do,

d[ foo ] = foo

#Either of the following for e
e = { foo :foo}
e = dict(foo=foo)

But, in all these way to add the variable foo to dict, I have had to use the word foo twice; once to indicate the key and once for its value.

It seems wasteful to me to use foo twice. Is there a simpler way, in which you can tell python "Add this variable to the dictionary with its name as the key and its value as the value"?

最佳回答

Actutally using foo twice is remarkably common in python programs. It is used extensively for passing on arguments eg

def f(foo, bar):
    g(foo=foo)

Which is a specialised case of the dictionary manipulations in your question.

I don t think there is a way of avoiding it without resorting to magic, so I think you ll have to live with it.

问题回答

you can do something like this

def add_dry_foo(d, namespace, fooName):
    d[fooName] = namespace[fooName]

foo =  oh-foo 
d = {}
add_dry_foo(d, locals(),  foo )
print d

You can use:

name =  foo 
d[name] = vars[name]

I don t see the difference between your d and e cases: both set foo to the value of foo.

It gets trickier if you want to bury this in a function:

def add_variable(d, name):
    # blah

because then it has to use inspect to start poking around in frames.

This sounds like a larger problem that might have a nicer solution if you wanted to describe it to us. For example, if the problem is that you don t care just about foo, but in fact, a whole slew of local variables, then maybe you want something like:

d.update(locals())

which will copy the names and value of all the local variables into d.

If you don t want to pass all of locals() (which may be a security risk if you don t fully trust the function you re sending the data too), a one-line answer could be this:

dict([ (var, locals()[var]) for var in [ foo , bar ] ])

or in Python 3.0 this would become possible:

{ var: locals()[var] for var in [ foo , bar ] }

To add all the local variables to a dict you can do:

d.update(locals())

The same works for function calls:

func(**locals())

Note that depending on where you are locals() might of course contain stuff that should not end up in the dict. So you could implement a filter function:

def filtered_update(d, namespace):
    for key, value in namespace.items():
        if not key.startswith( __ ):
            d[key] = value

filtered_update(d, locals())

Of course the Python philosophy is "explicit is better than implicit", so generally I would walk the extra mile and do this kind of stuff by hand (otherwise you have to be careful about what goes on in your local namespace).

You could use eval, although I m not sure that I d recommend it.

>>> d = dict()
>>> foo =  wibble 
>>> def add(d, name):
        d[name] = eval(name)


>>> add(d,  foo )
>>> d
{ foo :  wibble }

Edit: I should point out why I don t recommend "eval". What happens if you do something like this? (from: http://mail.python.org/pipermail/tutor/2005-November/042854.html)

>>> s = "(lambda loop: loop(loop)) (lambda self: self(self))"
>>> add(d, s)

Traceback (most recent call last):
  File "<pyshell#54>", line 1, in <module>
    add(d, s)
  File "<pyshell#43>", line 2, in add
    d[name] = eval(name)
  File "<string>", line 1, in <module>
  File "<string>", line 1, in <lambda>
  File "<string>", line 1, in <lambda>
  ...
  File "<string>", line 1, in <lambda>
RuntimeError: maximum recursion depth exceeded

It seems to me what you are talking about is an enhancement to parameter passing functionality:

def func(*vars):

provides a tuple of ordered values without keys

def func(**vars):

provides a dict of key value pairs, that MUST be passed as key=value pairs.

def func(***vars):

WOULD PROVIDE a dict of key value pairs, passed either explicitly as key=value, or implicitly as key (a variable, literals would cause error without key=)

SO:

(x1,x2,x3) = (1,2,3)
def myfunc(***vars):
  retrun vars
myfunc(x1,x2,x3)
>>> { x1 :1, x2 :2, x3 :3}

But of course, this is just wishful thinking...





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签