**列表:
列表操作包含以下函数:
1、len(list):列表元素个数
2、max(list):返回列表元素最大值
3、min(list):返回列表元素最小值
4、list(seq):将元组转换为列表
列表操作包含以下方法:
1、list.append(obj):在列表末尾添加新的对象
2、list.count(obj):统计某个元素在列表中出现的次数
3、list.extend(seq):在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
4、list.index(obj):从列表中找出某个值第一个匹配项的索引位置
5、list.insert(index, obj):将对象插入列表
6、list.pop(obj=list[-1]):移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
7、list.remove(obj):移除列表中某个值的第一个匹配项
8、list.reverse():列表元素颠倒
9、list.sort([func]):对原列表进行排序(从小到大)
#list
nums = [0, 1, 2, 3, 4]
even_squares = [x ** 2 for x in nums if x % 2 == 0]
print (even_squares )
animals = ['cat', 'dog', 'monkey']
for idx, animal in enumerate(animals):
print ('#%d: %s' % (idx + 1, animal))
#dictionary
d = {'cat': 'cute', 'dog': 'furry'}
d.get('cat','no element') #如果找不到,就返回备选
d = {'person': 2, 'cat': 4, 'spider': 8}
for animal, legs in d.items():
print ('A %s has %d legs' % (animal, legs))
nums = [0, 1, 2, 3, 4]
even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0}
print( even_num_to_square )# Prints "{0: 0, 2: 4, 4: 16}"
#set
animals = {'cat', 'dog'}
animals.add('fish')
参考:
http://blog.youkuaiyun.com/lilongsy/article/details/70895753