String
1.lower(),upper()
print ‘Hello’.lower()
结果为hello
print ‘Hello’.upper()
结果为HELLO
2.去掉空格
lstrip()去掉左边的空格
rstrip()去掉右边的
strip()去掉左右的
Files
1.open(‘1.txt’,’r’)
open有两个参数,文件名和方式(包括r读取,w写)
若省略第二个参数,默认为r
open打开并不读取,只是建立一种链接
例:hand = open(‘1.txt’,’r’)
print hand
2.换行\n,占一个字符
X
Y
以上长度为3
3.for循环打开语句
xfile = open(‘1.txt’,’r’)
for line in file:
print line
以上3行语句,打开了文件1,同时读取了文件中的每一行
4.一次性读取全部文件内容,非一行行的
xfile = open(‘1.txt’,’r’)
inp = file.read()读取全部文件,用一行表示出来
len(inp) 全部字符数
5.查找
for line in file:
if line.startswith(‘a’)
print line
找到a开头的行,展示出来,但是会有空行(因为有两个\n,一个来自源文件,一个来自print),改成:
for line in file:
line = line.rstrip()
if line.startswith(‘a’)
print line
rstrip会删掉空格和换行号
if not line.strarswith(‘a’)
不以a开头的行
if not ‘a’ in line 判断行里有没有‘a’
List列表
1.string不可以改变,但是list可以
比如说改变list里面的第二个,可以list[1]=5
2.list的长度,len(list),结果是元素个数
3.range(n)
生成一个有n个数字的list,从0到n-1,如:
print range(4)
结果为[0,1,2,3]
4.list可以相加
a=[1,2]
b=[3,4]
c=a+b
print c
结果[1,2,3,4]
print a
结果[1,2]
5.list methods
x=list()
type(x)返回x的类型,是list
dir(x)返回list的method,append、count等等
增加list的值append:
x=list()
x.append(‘a’)
x.append(9)
print x
结果[a,9]
判断元素是否在list中,in:
a=[1,2,3,4,5,6]
1 in a返回True
7 in a返回False
7 not in a返回True
元素排序,sort():
a = [‘a’,’c’,’b’]
a.sort()
print a
结果[‘a’,’b’,’c’]
6.一些数值的build-in functions:
nums=[3,41,12,9,74,15]
print len(nums) 结果6
print max(nums) 结果74
print min(nums) 结果3
print sum(nums) 结果154
7.strings and lists
abc=‘with three words’
a=abc.split()
print a
结果[‘with’,’three’,’words’]
split()把一个string用空格(中间有多个空格也没关系)分成了三个词,变成了list
也可以用其他来拆,比如说split(“;”),split(‘@‘)
Dictionaries
1.key/value
purse= dict()
purse[‘money’] = 12
给purse一个key叫做money,赋值value12
可以计算,如purse[‘money’]=purse[‘money’]+2
print purse[‘money’]结果为14
2.lists和dictionaries区别
lst = list()
lst.append(21)
lst.append(183)
print let
结果[21,183]
lst[0]=23
print let
结果[23,183]
_______________
ddd = dict()
ddd[‘age’]=21
ddd[‘course’]=182
print ddd
结果{‘course’:182,’age’:21}
add[‘age’]=23
print ddd
结果{‘course’:182,’age’:23}
_______________
lists有顺序,但是dictionaries没有
3.dictionaries赋值
jaj = {“a”:1,”b”:2}
4.get
print counts.get(name,0)
name的值,如果name不存在,给他赋值default,0
5.counting pattern
counts = dict()
print ‘enter a line of text’
line = raw_input(“”)
words = line.split()
print “words”,words
print ‘counting…’
for word in words:
counts[word] = counts.get(word,0)
print ‘counts’,counts
6.
jaj = {“a”:1,”b”:2}
print list(jaj)
>>[‘a’,’b’]显示key
print jaj.keys()
>>[‘a’,’b’]显示key
print jaj.values()
>>[1,2]显示值
print jaj.items()
>>[(“a”,1),(“b“,2)]也是list,key&value的对
Tuples数组
1.和lists很像
但是tuple不可改变,immutable
lists可变:
x=[9,8,7]
x[2]=6
print x
结果[9,8,6]
——————————
string不可变:
y=‘abc’
y[2]=‘d’
返回traceback
——————————
tuples不可变:
z=(5,4,3)
z[2]=0
返回traceback
2.tuples很多方法不能用
append,sort等等,因为这些都会改变数组的值,数组不可变
3.tuples可以用两种方法
l=list()
dir(l)
返回append,count,extend,等等
t=tuple()
dir(t)
只有count和index
4.tuples效率很高,因为它不变
所以,如果数据不变,或者暂时使用,我们优先使用tuples
5.赋值
(x,y)=(5,6)
print y
结果为6
6.dictionaries的items()返回的就是tuples
d=dict()
d[‘a’]=1
d[‘b’]=w
for (k,v) in d.items()
print k,v
返回
a 1
b 2
——————
tups=d.items()
print tups
返回
[(‘a’,1),(‘b’,2)]
7.tuples可以比较大小
逐个比较,如果对应位置符合要求,就不会再看下一个
(0,1,2)<(1,1,2)
TRUE
0小于1,后面的不再看了
——————
(‘jones’,’sally’)<(‘jones’,’fred’)
FALSE
第一个相同,第二个s比f大
8.dictionaries无序,可以利用tuples排序,by
key
d={‘a’:10,’b’:1,’c’:22}
t=d.items()
print t
返回[(‘a’,10),(‘c’,22),(‘b’,1)]
t.sort()
print t
返回[(‘a’,10),(‘b’,1),(‘c’,22)]
#sort by key
—————————————————
简单方法,用sorted()排序,按key排
d={‘a’:10,’b’:1,’c’:22}
d.items()
返回[(‘a’,10),(‘c’,22),(‘b’,1)]
t=sorted(d.items())
t就是[(‘a’,10),(‘b’,1),(‘c’,22)]
9.排序by value
c={‘a’:10,’b’:1,’c’:22}
tmp = list()
for k,v in c.items():
tmp.append((v,k))
#for把v和k的顺序改变了,在tmp里,tmp是个list,对于tmp中的每一个tuple,v在前,以v来排序
print tmp
返回[(10,’a’),(22,’c’),(1,’b’)]
tmp.sort(reverse=Ture)
#sort从小到大排列,reverse=Ture变成从大到小
print tmp
返回[(22,’c’),(10,’a’),(1,’b’)]
————————
短代码
c={‘a’:10,’b’:1,’c’:22}
print sorted([(v,k) for k,v in c.items()])
返回[(1,’b’),(10,’a’),(22,’c’)]