URN Logo
UNIX Resources » Linux » China Linux Forum » Python 编 程 » 19 » [精华] Python 食谱--1.3: 不用过多的引用来构造一个字典
announcement 声明: 本页内容为中国Linux论坛的内容镜像,文章的版权以及其他所有的相关权利属于中国Linux论坛和相应文章的作者,如果转载,请注明文章来源及相关版权信息。
Resources
China Linux Forum(finished)
Linux Forum(finished)
FreeBSD China(finished)
linuxforum.net
  业界新闻与评论
  自由软件杂谈
  IT 人生
  Linux软件快递
  翻译作坊
  Linux图书与评论
  GNU Emacs/XEmacs
  Linux 中文环境和中文化
  Linux桌面与办公软件
  Linux 多媒体与娱乐版
  自由之窗Mozilla
  笔记本电脑上的Linux
  Gentoo
  Debian 一族
  网络管理技术
  Linux 安装与入门
  WEB服务器和FTP服务器
  域名服务器和邮件服务器
  Linux防火墙和代理服务器应用
  文件及打印服务器
  技术培训与认证
  Linux内核技术
  Linux 嵌入技术
  Linux设备驱动程序
  Linux 集群技术
  LINUX平台数据库
  系统和网络安全
  CPU 与 编译器
  系统计算研究所专栏
  Linux下的GUI软件开发
  C/C++编程版
  PHP 技 术
  Java&jsp技术
  Shell编程技术
  Perl 编 程
  Python 编 程
  XML/Web Service 技术
  永远的Unix
  FreeBSD世界
   
[精华] Python 食谱--1.3: 不用过多的引用来构造一个字典
Author: rings    Posted: 2004-04-24 00:21    Length: 5,559 byte(s)
[Original] [Print] [Top]
1.3 Constructing a Dictionary Without Excessive Quoting
不用过多的引用来构造一个字典

Credit: Brent Burley
1.3.1 Problem
问题

You'd like to construct a dictionary without having to quote the keys.
你想构造一个不使用引用键值(key)的字典

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中来, 你可以发现你自己需要构造许多的字典。然而,标准的方法,也就是众所周知的字典的显示,比你想象的有一点点混乱。 因为它需要引用键值。 例如:
data = { 'red' : 1, 'green' : 2, 'blue' : 3 }

When the keys are identifiers, there's a cleaner way:
当键值是标识符的时候,有一个更简洁的方法:

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

You might also choose to forego some simplicity to gain more power. For example:
你可能也选择去简化他们来获得更高的威力。例如:

def dodict(*args, **kwds):
d = {}
for k, v in args: d[k] = v
d.update(kwds)
return d
tada = dodict(*data.items( ), yellow=2, green=4)
[译者:在python2.3中,tada = dodict(*data.items( ), yellow=2, green=4)
不能执行。有一个语法错误]

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里的说法)。
%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.3.4 See Also
参考
The Library Reference section on mapping types.

[Original] [Print] [Top]
« Previous thread
[精华] Python 食谱--1.4:从字典中获取一个值
Python 编 程
19
Next thread »
[精华] Python 食谱--1.2: 不使用临时变量来交换值
     

Copyright © 2007 UNIX Resources Network, All Rights Reserved.      About URN | Privacy & Legal | Help | Contact us
备案序号: 京ICP备05006143    webmaster: webmaster@unixresources.net
This page created on 2008-07-17 04:11:40, cost 0.035935878753662 ms.