xuemei 的个人资料May's Home照片日志列表更多 ![]() | 帮助 |
|
5月13日 学习Python1. 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) 引用通告此日志的引用通告 URL 是: http://mayzhaomayzhao.spaces.live.com/blog/cns!1C0E33D895DA730A!199.trak 引用此项的网络日志
|
|
|