1. 1.3 Constructing a Dictionary Without Excessive Quoting

不用过多的引用来构造一个字典

Credit: Brent Burley

1.1. 1.3.1 Problem

问题

You'd like to construct a dictionary without having to quote the keys.

你想构造一个不使用引用键值(key)的字典

1.2. 1.3.2 Solution

解决

Once you get into the swing of Python, you may find yourself constructing a lot of dictionaries. However, the standard way, also known as a dictionary display, is just a smidgeon more cluttered than you might like, due to the need to quote the keys. For example:

一旦你积极投入到python中来, 你可以发现你自己需要构造许多的字典。然而,标准的方法,也就是众所周知的字典的显示,比你想象的有一点点混乱。 因为它需要引用键值。 例如:

   1 data = { 'red' : 1, 'green' : 2, 'blue' : 3 }

When the keys are identifiers, there's a cleaner way:

当键值是标识符的时候,有一个更简洁的方法:

   1 def makedict(**kwargs):
   2     return kwargs
   3 data = makedict(red=1, green=2, blue=3)

You might also choose to forego some simplicity to gain more power. For example:

你也可以放弃一些简洁性来获得更高的威力。例如:

   1 def dodict(*args, **kwds):
   2     d = {}
   3     for k, v in args: d[k] = v
   4     d.update(kwds)
   5     return d
   6 tada = dodict(*data.items(  ), yellow=2, green=4)

[译者:在python2.3中,tada = dodict(*data.items( ), yellow=2, green=4) 不能执行。有一个语法错误]

1.3. 1.3.3 Discussion

讨论

The syntax for constructing a dictionary can be slightly tedious, due to the amount of quoting required. This recipe presents a technique that avoids having to quote the keys, when they are identifiers that you already know at the time you write the code.

由于需要一定数量的引用,构造一个字典的语法有点乏味。这个配方展示了一种技术,在你写代码时你已经知道他们是标识符了,它可以避免引用键值。

I've often found myself missing Perl's => operator, which is well suited to building hashes (Perl-speak for dictionaries) from a literal list:

我经常发现我自己会想念perl的=>操作符, 它很合适从一个文字列表中去建造一个hash(字典在perl里的说法)。

   1 %data = (red => 1, green => 2, blue => 3);

The => operator in Perl is equivalent to Perl's own , except that it implicitly quotes the word to its left.

Perl的=>操作符相当于perl的own(译注:不懂perl,不太明白), 除了它隐式引用一个它左边的单词。

Perl's syntax is very similar to Python's function-calling syntax for passing keyword arguments. And the fact that Python collects the keyword arguments into a dictionary turned on a light bulb in my head.

Perl的语法和python为传递一个keyword参数的函数调用语法非常相象。 并且,python收集keyword参数到一个字典里的事实给了我启发

When you declare a function in Python, you may optionally conclude the list of formal arguments with *args or **kwds (if you want to use both, the one with ** must be last). If you have *args, your function can be called with any number of extra actual arguments of the positional, or plain, kind. Python collects all the extra positional arguments into a tuple and binds that tuple to the identifier args. Similarly, if you have **kwds, your function can be called with any number of extra actual arguments of the named, or keyword, kind. Python collects all the extra named arguments into a dictionary (with the names as the keys and the values as the values) and binds that dictionary to the identifier kwds. This recipe exploits the way that Python knows how to perform the latter task.

当你在python里声明一个函数的时候,你可以随意的决定具有*args 或者**kwds形式的正式的参数列表(如果你想他们两个一起使用,具有**形式的那个必须发放到最后)。 如果你有一个*args, 你的函数在调用时,它可以有任意数量的额外位置参数,或者称平凡参数。Python收集所有额外的位置参数到一个tuple并且绑定那个tuple到标识符args。同样地,如果你有**kwds,你的函数在调用时,可以有任意数量和种类的命名参数,或者称关键字参数。 Python收集所有的特别的命名字参到一个字典里(名字作为键值,他的值作为字典的值)并且绑定这个字典到标识符kwds。 这个配方利用了这种方法(python知道怎么样执行后来的任务)

The makedict function should be very efficient, since the compiler is doing work equivalent to that done with a dictionary literal. It is admittedly idiomatic, but it can make large dictionary literals a lot cleaner and a lot less painful to type. When you need to construct dictionaries from a list of key/item pairs, possibly with explicit override of, or addition to, some specifically named key, the dodict function (although less crystal-clear and speedy) can be just as handy. In Python 2.2, the first two lines of dodict can be replaced with the more concise and faster equivalent:

makedict函数应该是非常有效的,因为编译器正在做的工作相当于带着一个字典文字去做。不可否认那是一个惯用方法。但是他能让很大的字典文字更简洁,更少一些打字的痛苦。当你需要从一个具有key/item对的列表中构造一个字典, 而且有可能要覆盖,或者附加一些特别的命名键值的时候,dodict函数(尽管不太清楚和快速)也不失为一种便利方法。在python2.2中,dodict的前两行能被更简明和快速的等价物来替换:

d = dict(args)

1.4. 1.3.4 See Also

参考

The Library Reference section on mapping types.