xuemei 的个人资料May's Home照片日志列表更多 工具 帮助

日志


5月13日

学习Python

1. Python also supports complex numbers (复数)。imaginary numbers are written with a suffix of ‘j’ or ‘J’. Complex numbers
with a nonzero real component are written as ‘(real+imagj)’, or can be created with the ‘complex(real, imag)’ function. Complex numbers are always represented as two floating point numbers, the real and imaginary part (实部,虚部). To extract these parts from a complex number z, use z.real and z.imag.
 
2。Multiple assignment: the variables a and b simultaneously get the new values 0 and 1.
   a, b = 0, 1                   
   The right-hand side expressions are evaluated from the left to the right.
   a, b = b, a+b
 
3. To iterate over the indices of a sequence, combine range() and len() as follows:
   >>> a = [’Mary’, ’had’, ’a’, ’little’, ’lamb’]
   >>> for i in range(len(a)):
   ... print i, a[i]
   ...
   0 Mary
   1 had
   2 a
   3 little
   4 lamb
 
4. There are three built-in functions that are very useful when used with lists: filter(), map(), and reduce().
‘filter(function, sequence)’ returns a sequence (of the same type, if possible) consisting of those items from the
sequence for which function(item) is true. For example,
>>> def f(x): return x % 2 != 0 and x % 3 != 0
...
>>> filter(f, range(2, 25))
[5, 7, 11, 13, 17, 19, 23]
 
‘map(function, sequence)’ calls function(item) for each of the sequence’s items and returns a list of the return
values. For example,
>>> def cube(x): return x*x*x
...
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
 
‘reduce(func, sequence)’ returns a single value constructed by calling the binary function func on the first two
items of the sequence, then on the result and the next item, and so on. For example, to compute the sum of the numbers
1 through 10:
>>> def add(x,y): return x+y
...
>>> reduce(add, range(1, 11))
55
 
>>> def sum(seq):
... def add(x,y): return x+y
... return reduce(add, seq, 0)
...
>>> sum(range(1, 11))
55
>>> sum([])
0
 
5. Another useful data type built into Python is the dictionary. Dictionaries are sometimes found in other languages
as “associative memories” or “associative arrays”. Unlike sequences, which are indexed by a range of numbers,
dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys.
>>> tel = {’jack’: 4098, ’sape’: 4139}
>>> tel[’guido’] = 4127
>>> tel
{’sape’: 4139, ’guido’: 4127, ’jack’: 4098}
>>> tel[’jack’]
4098
>>> del tel[’sape’]
>>> tel[’irv’] = 4127
>>> tel
{’guido’: 4127, ’irv’: 4127, ’jack’: 4098}
>>> tel.keys()
[’guido’, ’irv’, ’jack’]
>>> tel.has_key(’guido’)
True 
 
6. Methods of File Objects
 
f.read(size)
f.readline()
f.readlines()
f.write(string)
f.tell()         returns an integer giving the file object’s current position in the file, measured in bytes from the beginning of the file.
f.seek()
f.close()
 
Python provides a standard module called pickle. This is an amazing module that can take almost any Python object (even some forms
of Python code!), and convert it to a string representation; this process is called pickling. Reconstructing the object from the string representation is called unpickling.
 
pickle.dump(x, f)
x = pickle.load(f)

评论

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。

若要添加评论,请使用您的 Windows Live ID 登录(如果您使用过 Hotmail、Messenger 或 Xbox LIVE,您就拥有 Windows Live ID)。登录


还没有 Windows Live ID 吗?请注册

引用通告

此日志的引用通告 URL 是:
http://mayzhaomayzhao.spaces.live.com/blog/cns!1C0E33D895DA730A!199.trak
引用此项的网络日志