`
jamie.wang
  • 浏览: 340392 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Python复习笔记—数据结构

阅读更多

1. List对象

一些方法:

 

>>> lista
['steven', 3.14]
>>> lista.extend(['python', 'adam', 'alex'])
>>> lista
['steven', 3.14, 'python', 'adam', 'alex']
>>> lista.pop()
'alex'
>>> lista
['steven', 3.14, 'python', 'adam']
>>> lista.pop(2);
'python'
>>> lista
['steven', 3.14, 'adam']
>>> lista.index(3.14)
1

 

2. Filter

filter(f, seqence)返回一个序列,其中的元素(x)调用f(x)返回true,即过滤掉返回false的元素

 

>>> filter(lambda x : x % 2 != 0, range(10))
[1, 3, 5, 7, 9]

 

3. Map

map(f, sequence)在每个sequence元素(x)上调用f(x),返回整个序列调用后的结果

 

>>> map(lambda x : x * x, range(5))
[0, 1, 4, 9, 16]

 

且支持多个sequnce

>>> map(lambda x, y : x + y, range(5), range(5))
[0, 2, 4, 6, 8]

4. Reduce

reduce(f, sequence)在第一个元素和第二个元素上调用f,接着是结果和第三个元素,最后返回最终一个结果

 

>>> reduce(lambda x, y : x + y, range(5))
10

 

5. List的初始化

可以用一个范围和一个计算试来灵活的初始化List

 

>>> squares = [x**2 for x in range(10)]

>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> [(x, y) for x in range(3) for y in range(3) if x != y]
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]

>>> def is_prime(n) :
	for i in range(2, n) :
		if 0 == n % i :
			return False
	return True
>>> [ x for x in range(30) if is_prime(x) ]
[0, 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

6. Del

del可以删除元素,也可删除一个范围的元素,甚至整个对象

 

>>> lista
['steven', 'lucy', 'adam']
>>> del lista[0]
>>> lista
['lucy', 'adam']
>>> del lista[0:1]
>>> lista
['adam']
>>> del lista
>>> lista

Traceback (most recent call last):
  File "<pyshell#421>", line 1, in <module>
    lista
NameError: name 'lista' is not defined 

7. Tuples

tuple包含一组逗号(,)分隔的值

 

>>> t = 3, 'one', 3.14
>>> t[0]
3
>>> t = t, 'fxx', False
>>> t
((3, 'one', 3.14), 'fxx', False)
>>> t = 'single', # <-- note trailing comma
>>> t[0]
'single'

8. Sets

set是元素的集合,没有重复元素,可以做集合运算

 

>>> s0 = set([1, 'six', 0, 5, False, 2, 'nine', 0, 'seven'])
>>> t = 6, 'nine', 8, 'twelve', 2, True, 0, 'seven'
>>> s1 = set(t)
>>> s0 & s1
set([0, 1, 2, 'nine', 'seven']) # note: 0 == False, 1 == True
>>> s0 | s1
set([0, 1, 2, 5, 6, 'twelve', 8, 'six', 'seven', 'nine'])
>>> s0 - s1
set(['six', 5])
>>> s0 ^ s1
set(['twelve', 5, 6, 8, 'six'])
>>> 

9. Dicts

dict相当于其他语言中的map

 

 

>>> 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']
>>> 'guido' in tel
True
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127}
>>> dict([(x, x**2) for x in (2, 4, 6)])     # use a list comprehension
{2: 4, 4: 16, 6: 36}
>>> dict(sape=4139, guido=4127, jack=4098)
{'sape': 4139, 'jack': 4098, 'guido': 4127}

 

10. 遍历集合

 

>>> tels = dict(sape=4139, guido=4127, jack=4098)
for k, v in tels.iteritems() :
	print k + ':',v

sape: 4139
jack: 4098
guido: 4127
>>> for i, v in enumerate(['one', 'two', 'three']) :
	print i + 1, ' : ' + v

	
1  : one
2  : two
3  : three
>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']

>>> for q, a in zip(questions, answers):
	print 'What is your {0}?  It is {1}.'.format(q, a)

What is your name?  It is lancelot.
What is your quest?  It is the holy grail.
What is your favorite color?  It is blue.

 

11. 比较

序列的比较是按照类似字符串比较的方式进行。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics