1 列表List
a) 格式: [元素1,元素2...]
b) 动态类型语言, 其内组成元素可以是多种类型
c) 常用方法如下
声明集合写法:
>>> a = [1,2,3,4,'5']
>>> type(a)
<type 'list'>
>>> b =[1,2,3,[4,'5'],6]
>>> type(b)
<type 'list'>
>>> for i in b:
print i
1
2
3
[4, '5']
6
>>>
集合方法 x.append(y)是将此元素作为一个变量添加到集合x中, x.extend(y)是将y里的每个元素添加到x集合末尾,看下面写法
>>> a = [x for x in range(5)]
>>> print a
[0, 1, 2, 3, 4]
>>> b = [5,6]
>>> print b
[5, 6]
>>> a.append(b)
>>> print a
[0, 1, 2, 3, 4, [5, 6]]
>>> a.extend(b)
>>> print a
[0, 1, 2, 3, 4, [5, 6], 5, 6]
查看a的属性和方法,'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort' 表示自带的方法
>>> dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__',
'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
查询集合元素为5的个数
>>> a.count(5)
1
在第0个位置插入元素
>>> a.insert(0,'a')
>>> print a
['a', 0, 1, 2, 3, 4, [5, 6], 5, 6]
队尾元素弹出来
>>> a.insert(1,'b')
>>> print a
['a', 'b', 0, 1, 2, 3, 4, [5, 6], 5, 6]
>>> temp = a.pop()
>>> print temp
6
>>> temp = a.pop()
>>> print temp
5
>>> temp = a.pop()
>>> print temp
[5, 6]
>>> print a
['a', 'b', 0, 1, 2, 3, 4]
remove(元素1) 是将集合中的第一个出现的元素1删除掉,如果有多个的话,需要重复执行此方法,如果删除完毕在继续删除,会报异常
>>> a.append(0)
>>> print a
['a', 'b', 0, 1, 2, 3, 4, 0]
>>> a.remove(0)
>>> print a
['a', 'b', 1, 2, 3, 4, 0]
>>> a.remove(0)
>>> print a
['a', 'b', 1, 2, 3, 4]
>>> a.remve(0)
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
a.remve(0)
AttributeError: 'list' object has no attribute 'remve'
排序操作
>>> a.sort()
>>> print a
[1, 2, 3, 4, 'a', 'b']
内部元素获取写法, 集合[index1:index2] 表示获取集合角标index1(含)到角标index2(不含)的元素
a[:1] 等同于 a[0:1] a[-1]表示从倒数第一个位置开始取
>>> b = a[:1]
>>> print b
[1]
>>> b = a[:3]
>>> print b
[1, 2, 3]
>>> b = a[3:6]
>>> print b
[4, 'a', 'b']
>>> b = a[0:3]
>>> print b
[1, 2, 3]
>>> a[-1]
'b'
>>> a[-3:-1] 表示从倒数第三个位置开始(含) 到倒数第一个位置截止(不含)
[4, 'a']
2 元组 Tuple
a) 格式: a=(元素1,元素2...)
b) 动态类型语言, 其内组成元素可以是多种类型
c) 和list的区别:
c.1) 他们都是索引集合对象
c.2) 两者实现机制不一样,设计目的也不一样,元组设计为了查询,也只有查询的方法,查询效率很高
d) 案例:
>>> a = (1,2,3) 定义元组
>>> type(a)
<type 'tuple'>
>>> dir(a) 查看元组属性和方法,可以看到 只有两个方法 count index
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__',
'__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
>>> for i in a:
print i
1
2
3
>>> c = (y for y in a) 快速拷贝元组
>>> for i in c:
print i
1
2
3
>>> a[1] 和数组一样的获取内部元素的写法
2
>>> a[:2]
(1, 2)
>>> b = [x for x in a] 元组和list相互转换
>>> print b
[1, 2, 3]
>>> b.append(4) list修改内部元素后
>>> c = (x for x in b) 在换成元组
>>> for i in c:
print i
1
2
3
4
3 字典Dict
a) 格式: a={key1:val1,key2:val2...}
b) 案例:
>>> a = {'b':1,'d':2} 声明写法,类似于map
>>> print a
{'b': 1, 'd': 2}
>>> dir(a)
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__',
'__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__setitem__', '__sizeof__', '__str__', '__subclasshook__',
'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault',
'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
>>> if a.has_key('b'): 查看是否含有key为b的键值对
print a['b']
1
>>> a.has_key('b')
True
>>> a.has_key('c')
False
>>> for o in a.iteritems(): 遍历map里的所有数据,得到每一个键值对
print o
('b', 1)
('d', 2)
>>> b = [x for x in a.iterkeys()] 遍历得到所有的Keys
>>> print b
['b', 'd']
>>> a.values() 遍历得到所有的values
[1, 2]
>>> a.viewkeys()
dict_keys(['b', 'd'])
>>> help(sorted)
Help on built-in function sorted in module __builtin__:
sorted(...)
sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
4 集合 Set
>>> a = [1,2,3,4,5,5,6]
>>> b = set(a)
>>> type(b)
<type 'set'>
>>> b
set([1, 2, 3, 4, 5, 6])
set里面元素不能重复,这里目前先介绍这么些,以后工作中遇到了在细化
5 map reduce filter
map函数的定义:
map(function, sequence[, sequence, ...]) -> list
函数的第一个参数是一个函数,剩下的参数是一个或多个序列,返回值是一个集合。
function可以理解为是一个一对一或多对一函数,
map函数的作用:
是以参数序列中的每一个元素调用function函数(对每一个元素使用相同方式进行处理),返回包含每次function函数返回值的list。
map案例:
>>> a= [1,2,3,4,5]
>>> map(lambda x:x*2,a)
[2, 4, 6, 8, 10]
reduce函数的定义:
reduce(function, sequence[, initial]) -> value
function参数是一个有两个参数的函数,
reduce依次从sequence中取一个元素,,和上一次调用function的结果做参数再次调用function。 读起来有点费劲,
看下面案例:
>>>reduce(lambda x, y: x + y, [2, 3, 4, 5, 6]) 等效于 ((((2+3) + 4) + 5) + 6) 结果为20
>>>20
reduce函数的说明:
reduce函数必须是个内联函数,否则会报错,结果不会正常输出。
reduce函数的作用:对参数序列中元素进行累积
filter函数的定义:
filter(function or None, sequence) -> list, tuple, or string
filter函数的作用:
对指定序列执行过滤操作,filter函数会对序列参数sequence中的每个元素调用function函数,最后返回的结果包含调用结果为True的元素。
>>> filter(lambda x:x%2,a) x模2为真(非0)---> 等同于获取奇数
[1, 3, 5]
>>> filter(lambda x:not x%2,a) 获取偶数
[2, 4]