易截截图软件、单文件、免安装、纯绿色、仅160KB

Python的字典操作

 
Python的字典操作
Python提供了多种应用于字典的操作。因为字典为容器,内建len函数可以将字典当作单一参数使用听切返回字典对象中项目(键/值对)的数目。
字典会员
在Python2.2以及后面版本,D中的k运算符检测是否对象k是字典D中的键。如果是返回True如果不是返回False。相似的,
索引一个字典
字典D中的值与相关的键k被索引表示为:D[k]。索引字典没有的键会引起意外。例如:
d = { 'x':42, 'y':3.14, 'z':7 } 
d['x']                          
# 42
d['z']                          
# 7
d['a']                          
# raises exception
平整赋值到一个使用还不在字典中的键的索引(例如,D[newkey]=value)是一个可行的操作,该操作加载键和值到字典里新的项目中。例如:
d = { 'x':42, 'y':3.14, 'z':7 } 
d['a'] =
16                     
# d is now {'x':42,'y':3.14,'z':7,'a':16}
del
D[k]中的del语句,删除字典中拥有键k的项目。如果k不是字典D中的键,del
D[k]就会引起意外。
字典方法
字典对象提供了多种方法,如下表格所示。非变异方法返回结果,但不改变它们使用的对象。在下面列表中,D和D1代表任何字典对象,k代表D中任何有效的键,x为任何对象。
方法
描述
Non-mutating methods
D.copy( )
Returns a (shallow) copy of the dictionary
D.has_key(k)
Returns True if k is a key in D, otherwise returns False
D.items( )
Returns a copy of the list of all items (key/value pairs) in
D
D.keys( )
Returns a copy of the list of all keys in D
D.values( )
Returns a copy of the list of all values in D
D.iteritems( )
Returns an iterator on all items(key/value pairs) in D
D.iterkeys( )
Returns an iterator o


相关文档:

python中的exec 函数

      在项目里面一个解析文本的工具里面用到了这个命令来赋值,开始一直知道意思,呵呵 查了下,找到方法如下:
exec
语句用来执行储存在字符串或文件中的Python语句。例如,我们可以在运行时生成一个包含Python代码的字符串,然后使用
exec
语句执行这些语句。下面是一个简单的例子。
> ......

eat python 003

Documentation for C's fopen():
---
r Open text file for reading. The stream is positioned at the beginning
of the file.
r+ Open for reading and writing. The stream is positioned at the
beginning of the file.
w Truncate file to zero length or create text file for writing. The
stream is posi ......

一个删除文件的Python程序

   Python是一种面向对象的解释性的计算机程序设计语言,也是一种功能强大而完善的通用型语言,已经具有十多年的发展历史,成熟且稳定。Python 具有脚本语言中最丰富和强大的类库,足以支持绝大多数日常应用。它具有简单、易学、免费、开源、可移植性、解释性、面向对象、可扩展性、可嵌入性以及丰富的库等特性, ......

深入 GalCon:Python、游戏开发和人工智能

Alex. J. Champandard
赖勇浩(http://laiyonghao.com) 译
原文地址:http://aigamedev.com/open/interviews/inside-galcon-python-ai/
AiGameDev.com在这个周一的 pratical insights 是对Phil Hassey采访。Phil 是一个独立游戏开发者,也是游戏产业社区的活跃分子。他曾赢得2006年4月的 Ludum Dare 比赛(这个比赛关注 ......

Python & XML


Chapter 1
Python and XML
Python and XML are two very different animals, each with a rich
history. Python is a full-scale programming language that has grown
from scripting world roots in a very organic way, through the vision
and guidance of Python's inventor, Guido van Rossum. Guido continue ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号