zrong 发表于 2017-4-26 11:11:21

Python 随想一些有用feature

#coding=gbk
import os #operating system
import sys #system
import copy
from pprint import pprint #perfect print
from operator import attrgetter
a =
b =
#python使用引用计数
c = a
print c
#
c = -1
print c
#[-1, 2, 3, 4, 5]
print a
#[-1, 2, 3, 4, 5]
a = 1
print c
#
print a
#
#打包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 = j #d = value
pprint(d)
#{1: 6, 2: 7, 3: 8, 4: 9, 5: 10}
#python使用引用计数,使用深度copy
d = {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 # 列表解析
print a+b #
#string:
print "&".join()#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(""))#<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 =
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
sys.exit(0)
页: [1]
查看完整版本: Python 随想一些有用feature