标签:
def Function(args): print args print apply(Function,‘aaa‘) #apply 是用来执行函数的
#!coding:utf-8 assert(1==1)#取值相等则往下走 print ‘aaa‘
def foo(*arg): for item in arg: print item # print arg1,arg2 foo(‘ys‘,‘as‘,‘a‘,‘asadad‘)
def show(**args):#多个参数时用一个*,当调用的是字典且为多个参数时用两个** for item in args.items(): print item show(name=‘ys‘,age=‘23‘)
li = [‘watch‘,‘car‘,‘hourse‘] for item in enumerate(li,1): print item for i in li: print
a = ‘8*8‘ b = ‘3+5‘ print eval(a) print eval(b) #eval 可以把字符串当成表达式来执行
#coding:utf-8 li = [11, 22, 33] def asd(arg): if arg < 22: return True else: return False temp = filter(asd,li)#filter 判断当前取值如为真则打印出来,为假则pass print temp
#coding:utf-8 #反射 #以字符串的形式导入模块 #以字符串的形式执行函数 temp = ‘sys‘ mod = __import__(temp) print mod.path
class A(object): def __init__(self): print ‘This is A‘ def save(self): print ‘save of A‘ class B(A): def __init__(self): print ‘This is B‘ class C(A): def __init__(self): print ‘This is C‘ # def save(self): # print ‘save of C‘ class D(B,C): def __init__(self): print ‘this is D‘ e = D() e.save() #f = C() #f.save() #g = B() #g.save()
import os ip = os.popen("/sbin/ifconfig | grep ‘inet addr‘ | awk ‘{print $2}‘").read() ip = ip[ip.find(‘:‘)+1:ip.find(‘\n‘)] print ip
import socket localIP = socket.gethostbyname(socket.gethostname()) print "local ip: %s " % localIP
temp = lambda x,y:x+y print temp(3,10) def foo(x,y): return x+y print foo(1,10)
#!coding:utf-8 def Foo(args): return args + 100 li = [11,22,33] #方法一 #for i in li: # print Foo(i) #方法二 #temp = map(Foo,li) #print temp #方法三 temp = map(lambda args:args+50,li) print temp
import md5 hash = md5.new() hash.update(‘admin‘) print hash.hexdigest()
import hashlib hash = hashlib.md5() hash.update(‘admin‘) print hash.hexdigest()
def outer(fun): def wrapper(arg): print ‘check it!‘ res = fun(arg) return res # res = return ‘yongsan‘ print res return wrapper @outer #@outer = outer(func1) def func1(arg): print ‘func1‘,arg return ‘yongsan‘ @outer def func2(): print ‘func2‘ @outer def func3(): print ‘func3‘ respose = func1(‘alex‘) print respose #func2() #func3()
#coding:utf-8 import pickle data = {‘k1‘:123,‘k2‘:‘hello‘} #pickle.dumps 将数据通过特殊的形式转换为只有Python语言能够识别的字符串 p_str = pickle.dumps(data) #print p_str #pickle.dump 将数据通过特殊的形式转换为只有Python语言能够识别的字符串,并写入文件 #with open(‘result.pk‘,‘w‘) as fp: # pickle.dump(data,fp) import json #josn.dumps 将数据通过特殊的形式转换为所有程序语言都识别的字符串 j_str = json.dumps(data) print j_str #josn.dump 将数据通过特殊的形式转换为所有程序语言都识别的字符串,并写入文件 with open(‘result.pk‘,‘w‘) as fp: json.dump(data,fp)
import pickle li = [‘alex‘,11,22,‘ok‘,‘sb‘] dumpsed = pickle.dumps(li) print dumpsed print type(dumpsed) loadsed = pickle.loads(dumpsed) print loadsed
#/usr/bin/env python #coding:utf-8 import random#random 用于生成随机数 print random.random() #print random.randint(1,2)#生成的是整数 #print random.randrange(1,10) #生成随机验证码 checkcode = ‘‘ for i in range(4): current = random.randrange(0,4) if current != i: temp = chr(random.randint(65,90)) else: temp = random.randint(0,9) checkcode += str(temp) print checkcode
#coding:utf-8 li = [11, 22, 34] print reduce(lambda x,y:x+y,li) #答案:67 运行机制是累加 1):x = 11 y = 22 x+y = 33; 2):x = 33 y = 34 x + y = 77 #reduce 的x和y必须得有两个参数
def AlexReadlines(): seek = 0 while True: with open(‘temp.txt‘,‘r‘) as f: f.seek(seek) data = f.readline() if data: seek = f.tell() yield data else: return for i in AlexReadlines(): print i.strip(‘\n‘)
s = ‘my name is:{0},age:{1}‘ print s.format(‘alex‘,‘18‘)
import time import datetime asd=time.strftime(‘%Y-%m-%d %H:%M:%S‘) print as
f=open(‘/usr/home/yongsan/test/account_lock.txt‘,‘r‘) f.read() f.close #with 有自动关闭文件的功能,相当于多执行了一次f.close() with open(‘/usr/home/yongsan/test/account_lock.txt‘,‘r‘) as f: print ‘xxx‘ print ‘ok‘
def foo(): yield 1 re = foo() print re for i in re: print i #print range(10) #print xrange(10) #for item in xrange(10): # print item
x = [1,2,3,4] y = [2,4,6,8] z = [2,3,4,5,8] print zip(x,y,z) #木桶效应
res4 = re.compile(‘\d+‘)#comlile编译搜索内容,加速查找 print res4.findall(‘asa1a111s222ja333sla444sjalsmkal‘)
#coding:utf-8 import re res1 = re.search(‘(\d+)\w*(\d+)‘,‘asa1a12sj33asla55sjalsmkal‘) #\w* 代表下划线、字母、数字+‘\‘ *是只零到多个 print res1.group()#打印所有匹配项 print res1.groups()#只获取组内数据 res2 = re.search(‘a{1,4}‘,‘aaaaa‘) print res2.group()
import re res1 = re.match(‘\d+‘,‘12asa1asjaslasjalsmkal‘) res2 = re.search(‘\d+‘,‘12asa1asjaslasjalsmkal‘) res3 = re.findall(‘\d+‘,‘12asa221a333sjas444lasjalsmkal‘) print res1,res2,res3
import re ip = ‘12.34.13.ahdi.345rs.rwqp.sjalkkss.123.apdoa.10.39.5.117_w3asla~!@#1212‘ print re.findall(‘\d+‘,ip) print re.findall(‘[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}‘,ip) print re.findall(‘(?:\d{1,3}\.){3}\d{1,3}‘,ip)
标签:
原文地址:http://www.cnblogs.com/liyongsan/p/5573108.html