易截截图软件、单文件、免安装、纯绿色、仅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真的好吗?

为了google的google appengine而学习python
发现他真的不如c语言好学,倒不是他难懂,而是相关文档太少,而且不规范。
也许是我的问题吧,我还是喜欢c语言的头文件,还有msdn的那种帮助。一眼就知道使用方法。
说c语言难学,可能是因为没有仔细看这两个东西吧 ......

Python几种并发实现方案的性能比较

 
#!/Library/Frameworks/Python.framework/Versions/2.5/bin/python
# encoding: utf-8
import sys, time
import thread
 
SLEEP_TIME = 0.0001
 
def run_benchmark(n, m):
    # print(">> Python 2.5.1, stackless 3.1b3 here (N=%d, M=%d)!\n" % (n, m))
    lock ......

Python监视进程


由subprocess创建一个进程,然后进行监视
每一秒钟查看一次,如果正在运行,打印pid和running,如果已停止,,继续执行任务并打印Termined
shell和stdout均设置为False
也许这对做病毒的守护进程很好
#!/usr/bin/env python
import subprocess , sys , time
p=subprocess.Popen(['ping','127.0.0.1','-n','10'], she ......

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 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号