#coding=gbk import os #operating system import sys #system import copy from pprint import pprint #perfect print from operator import attrgetter a = [1, 2, 3, 4, 5] b = [6, 7, 8, 9, 10] #python使用引用计数 c = a print c #[1, 2, 3, 4, 5] c[0] = -1 print c #[-1, 2, 3, 4, 5] print a #[-1, 2, 3, 4, 5] a[0] = 1 print c #[1, 2, 3, 4, 5] print a #[1, 2, 3, 4, 5] #打包zip函数: for i,j in zip(a, b): print i, j #输出 #1 6 #2 7 #3 8 #4 9 #5 10 #字典dict: d ={} for i,j in zip(a, b): d[i] = j #d[key] = value pprint(d) #{1: 6, 2: 7, 3: 8, 4: 9, 5: 10} #python使用引用计数,使用深度copy d[1233] = {11111111111:copy.deepcopy(d)} pprint(d) #{1: 6, # 2: 7, # 3: 8, # 4: 9, # 5: 10, # 1233: {11111111111L: {1: 6, 2: 7, 3: 8, 4: 9, 5: 10}}} #链表list: print sum(a)#15 print len(a)#5 print [2 * x for x in a]#[2, 4, 6, 8, 10] 列表解析 print a+b #[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] #string: print "&".join([str(x) for x in a])#1&2&3&4&5 print "1&2&3&4&5".split("&")#['1', '2', '3', '4', '5'] print len("abc")#3 #文件 fileName = "test.py" for line in file(fileName): line = line.strip().split("\t") #eval 函数,可以解析字符串形式的python数据 print type(eval("[1,2]"))#<type 'list'> print type(eval("{1:2}"))#<type 'dict'> #os print os.listdir(".")#显示dir的所有项 print dir(os)#输出os module的所有方法 print help(os)#输出os模块的help doc string #二级排序(基于python sort是稳定的排序)This wonderful property lets you build complex sorts in a series of sorting steps. For example, to sort the student data by descending grade and then ascending age, do the age sort first and then sort again using grade: class student: def __init__(self, a, g): self.age = a self.grade = g def __str__(self): return "age=%d,grade=%d"%(self.age, self.grade) student_objects = [student(10, 2),student(3, 40)] s = sorted(student_objects, key=attrgetter('age')) # sort on age key sorted(s, key=attrgetter('grade'), reverse=True) # now sort on grape key, descending print [str(x) for x in s] sys.exit(0)