Python中的容器:列表(list)、字典(dictionaries)、集合(sets)、元组(tuple)

列表List:

列表就是python中的数组,但是列表长度可变,且能包含不同类型元素

切片Slicing:为了一次性获取列表中的元素

循环Loops:

1、普通循环

2、如果想要在循环体内访问每个元素的指针,使用内置的enumerate函数

列表推导List comprehensions:

注:

['__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']

#---------------list------------------------
list = [2,4,5,6,7]
print  list[2:]   #slicing   [5, 6, 7]
print list[:-1]  #to the last-1
list.append('pig')
for name in list:   #loop1
    print  name       # 2,4,5,6,7
list.pop()  #remove the last
for idx,name in enumerate(list):  #loop2  eumerate()
    print "%d : %s" %(idx+1,name)
print list[-1]   #count from end  7
#list comprehensions
squares1 = [x**2 for x in list]
squares2 = [x**2 for x in list if x%2==0]  #conditions
print squares1," ",squares2




字典Dictionaries:

字典用来存储(键,值)对,和java中的Map差不多

循环loops:

1、在字典中,用键来迭代更加容易

2、如果要访问键和对应的值,使用iteritems方法

字典推导Dictionary comprehensions:

注:

['__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']

#------------------------dictionary----------------
dic = {'cat':4,'people':2,'dog':4}
print dic['cat']  #4
print 'people' in dic  #true
print dic.get('monkey','N/A') #get element monkey with default  N/A
del dic['dog']  #remove
for key in dic:  #get from key
    print  dic[key]   #4,2,4
for key,value in dic.iteritems():   #itreitems
    print key,value
#comprehensions
to_square = {x: x**2 for x in dic.values()}
print to_square




集合Sets:


集合是独立不同个体的无序集合


循环loops:


集合推导Set comprehensions:




['__and__', '__class__', '__cmp__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']




#------------------------sets---------------------
aniamls = {'cat','dog','pig'}
print 'cat' in aniamls #true
aniamls.add('fish')
print len(aniamls)
aniamls.remove('dog') #remove
#loop1
for idx,aniaml in enumerate(aniamls): #cannt use animals{can consider the order}
    print  idx,aniaml
nums = { x for x in enumerate(aniamls)}  #comprehension
print nums



元组Tuples:


元组是一个值的有序列表(不可改变),但是元组可以再字典中用作键,还可以作为集合的元素


注:


['__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']


#---------------------------tuples---------------------


d = {(x,x+1):x for x in range(10)}  #use dic create tuples keys
print d
print type(d)  #dic
tuple =(5,6)
print type(tuple)#tuple
print d[tuple],d[(2,3)]