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
相关文档:
import Queue, threading, sys
from threading import Thread
import time,urllib
# working thread
class Worker(Thread):
worker_count = 0
def __init__( self, workQueue, resultQueue, timeout = 0, **kwds):
Thread.__init__( self, **kwds ) ......
#!/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程序的运行,其实一个Python程序就相当于一个应用程序,它不需要经过编译,只需要用户电脑上面安装Python环境即可。要运行一个py程序,直接双击这个py文件即可。一般情况下,没有提示用户输入或控制屏幕显示,打开一个py文件时会突然闪一下马上就退出,这是由于程序运行已经完成了。若需要显示,则要添加一 ......
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 ......
发信人: TRAD (GFans), 信区: NLP
标 题: 原创:使用python调用计算所分词
发信站: 水木社区 (Mon Nov 23 13:30:46 2009), 站内
代码很简单,但我自己摸索了一下午,发出来共享一下
把这个文件同ICTALAS30.DLL ,DATA文件夹,Configure.xm l放在同一个目录下即可。
python代码
#coding:gb2312
from cty ......