看文字太。。。枯燥了
找了个视频看 虽然是生肉..
坚持python一小时√
——
python命令行参数
$ python test.py arg1 arg2 arg3#!/usr/bin/python # -*- coding: UTF-8 -*- import sys print '参数个数为:', len(sys.argv), '个参数。' print '参数列表:', str(sys.argv)
list
a = [1,2,3]
sorted(a)这个函数适用于所有可迭代对象
>>> sorted(a,reverse = True) #由大到小排列
>>> a = ['ccc','aaaa','d']
In [18]: sorted(a)
Out[18]: ['aaaa', 'ccc', 'd'] #按照首字母排列
In [19]: sorted(a,key = len) #KEY函数决定了排列的数学
Out[19]: ['d', 'ccc', 'aaaa']
def last(s):
return s[-1]
sorted(a,key = last) #按照最后一个排序
In [7]: ':'.join(a) #将所有的元素连接起来 返回一个字符串
Out[7]: 'cc:aaaz:abcdz'
In [11]: b.split(':')
Out[11]: ['cc', 'aaaz', 'abcdz'] #恢复先前的分割
range(num) #生成序列
tuple (1,2,3) #元组
一组不可改变的数值
如果你之前用一种方式sort,之后再用一种方式sort 最好的方法是使用Tuple
dictory
d = {}
In [37]: d.get('a')
Out[37]: 'alpha'
In [38]: d.keys()
Out[38]: ['a', 'g', 'o']
In [40]: d.values()
Out[40]: ['alpha', 'gamma', 'omega']
In [41]: for k in sorted(d.keys()): print 'keys:',k,'->',d[k]
keys: a -> alpha
keys: g -> gamma
keys: o -> omega
In [43]: d.items()
Out[43]: [('a', 'alpha'), ('g', 'gamma'), ('o', 'omega')]
In [44]: for tuples in d.items(): print tuples
('a', 'alpha')
('g', 'gamma')
('o', 'omega')
files
#!/usr/bin/python2.7 -tt
import sys
def Cat(filename):
f = open(filename,'rU')
#lines = f.readlines()#This lines is a list
text = f.read() #this is a str
print text,
#print lines
f.close()
def main():
Cat(sys.argv[1])
if __name__ == '__main__':
main()
###word count###
Part 4
def Find(pat,text):
match = re.search(pat,text)
if match: print match.group()
else: print 'noooot found'
..g 前面两个是
w 字符
d 数字类型
s 好像是个空格符?
+ 跨过很大的空格
S non-whitespace 到第一个非空白字符停下来
In [22]: Find(r'ds+d','1 2 3')
1 2
w+
In [25]: Find(r':w+','bllah:hhhh dnsod ooooc.ll123lled piiif gabcd')
:hhhh
In [26]: Find(r':w','bllah:hhhh dnsod ooooc.ll123lled piiif gabcd')
:h
In [28]: Find(r':.+','bllah:hhhh dnsod ooooc.ll123lled piiif gabcd')
:hhhh dnsod ooooc.ll123lled piiif gabcd