python杂记

<log id = 1 >

#coding=gbk

 中间的“=”不能和旁边的字符分开写,晕。


<log id = 2 >

list的append,extend的区别。

一个是当成一个对象加入,一个是作为一个list合并。

f = ['a' , 'b']

l = ['c' , 'd']

f.append(l)    

print f

>>['a' , 'b' , ['c' , 'd']]

========================================

f = ['a' , 'b']

l = ['c' , 'd']

f.extend(l)    

print f

>>['a' , 'b' , 'c' , 'd']


<log id = 3 >

str()一般是将数值转成字符串。 
repr()是将一个对象转成字符串显示,注意只是显示用,有些对象转成字符串没有直接的意思。如list,dict使用str()是无效的,但使用repr可以,这是为了看它们都有哪些值,为了显示之用。 


<log id = 4 > /*2013-4-28*/

小心set初始化。

我想初始化一个string的set。听说可以set(X)这样的初始化。

a = ‘string’

s = set(a)

但是这个时候注意:a作为一个list送去给set初始化的。不信你试len(s)   绝对是6

可以这么来 s = set() 

                   s.add(a)

set加add   list加append  map就直接map[X] = y


<log id = 4 >/*2013-4-29*/

string to int or float

string.atoi()         string.atof()

string.find(s,sub[,start[,end]])返回在s[start:end]范围内子串sub在字符串s当中出现的最小下标,没有找到返回-1

string.count(s,sub[,start[,end]])返回在s[start:end]范围内子串sub在字符串s当中出现的次数


<log id = 5>/*2013-5-24*/

python正则表达式点击打开链接

group的命名规则需要注意

python的findall 把所有匹配上的子串返回

python中compile中r 的解释:点击打开链接

为正则表达式使用 Python 的 raw 字符串表示;在字符串前加个 "r" 反斜杠就不会被任何特殊方式处理,所以 r"\n" 就是包含"\" 和 "n" 的两个字

符,而 "\n" 则是一个字符,表示一个换行。正则表达式通常在 Python 代码中都是用这种 raw 字符串表示。

python正则中文点击打开链接 phanzi=re.compile(u'[\u4e00-\u9fa5]');#这里要加u,注意


python None pass 点击打开链接


无穷小无穷大

float('-inf')

float('+inf')


python dictionary get方法http://www.cnpythoner.com/post/301.html


python多线程:点击打开链接

不加锁的简单多线程

  1 #!/usr/bin/python
  2 import sys
  3 import re
  4 import threading
  5 import thread
  6 import time
  7 import random
  8
  9 mylock = threading.RLock()
 10 num = 0  #Shared resource
 11
 12 class timer(threading.Thread): #The timer class is derived from the class threading.Thread
 13     def __init__(self, num, interval):
 14         threading.Thread.__init__(self)
 15         self.thread_num = num
 16         self.interval = interval
 17         self.thread_stop = False
 18
 19     def run(self): #Overwrite run() method, put what you want the thread do here
 20         print str(self.thread_num) + ' run'
 21         global num
 22         #mylock.acquire() #Get the lock
 23         # Do something to the shared resource
 24         print 'Thread %s locked! num=%s'%(self.thread_num,str(num))
 25         if num <= 10:
 26             time.sleep(random.randrange(10)/5)
 27             num = num + 1
 28             print 'Thread %s released! num=%s'%(self.thread_num,str(num))
 29             #mylock.release()  #Release the lock.
 30
 31 def test():
 32     thread1 = timer(1, 1)
 33     thread2 = timer(2, 2)
 34     thread3 = timer(3, 3)
 35     thread4 = timer(4, 4)
 36     thread5 = timer(5, 5)
 37
 38     thread1.start()
 39     thread2.start()
 40     thread3.start()
 41     thread4.start()
 42     thread5.start()
 43     return
 44
 45 if __name__=='__main__':
 46     test()

运行结果:



加锁的简单多线程

  1 #!/usr/bin/python
  2 import sys
  3 import re
  4 import threading
  5 import thread
  6 import time
  7 import random
  8
  9 mylock = threading.RLock()
 10 num = 0  #Shared resource
 11
 12 class timer(threading.Thread): #The timer class is derived from the class threading.Thread
 13     def __init__(self, num, interval):
 14         threading.Thread.__init__(self)
 15         self.thread_num = num
 16         self.interval = interval
 17         self.thread_stop = False
 18
 19     def run(self): #Overwrite run() method, put what you want the thread do here
 20         print str(self.thread_num) + ' run'
 21         global num
 22         mylock.acquire() #Get the lock
 23         # Do something to the shared resource
 24         print 'Thread %s locked! num=%s'%(self.thread_num,str(num))
 25         if num <= 10:
 26             time.sleep(random.randrange(10)/5)
 27             num = num + 1
 28             print 'Thread %s released! num=%s'%(self.thread_num,str(num))
 29             mylock.release()  #Release the lock.
 30
 31 def test():
 32     thread1 = timer(1, 1)
 33     thread2 = timer(2, 2)
 34     thread3 = timer(3, 3)
 35     thread4 = timer(4, 4)
 36     thread5 = timer(5, 5)
 37
 38     thread1.start()
 39     thread2.start()
 40     thread3.start()
 41     thread4.start()
 42     thread5.start()
 43     return
 44
 45 if __name__=='__main__':
 46     test()

结果:



关于如何结束一个进程的东西:点击打开链接http://stackoverflow.com


<log id = 6>/*2013-7-27*/

和其他语言一样,Python为string对象提供了转换大小写的方法:upper() 和 lower()。还不止这些,Python还为我们提供了首字母大写,其余小写的capitalize()方法,以及所有单词首字母大写,其余小写的title()方法。


<log id = 6>/*2013-8-19*/

迭代器(iterator) 生成器表达式(generator expression)  列表解析(list comprehension)点击打开链接

生成器点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值