<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1.str.find()如果没有发现字符串,会返回-1,如果使用index,若字符串没发现,会报错,所以常用find。</span>
2.str.replace("bb","BB",3),后面指定了3,则会替换str前3个bb为BB。
3.for循环有点类似oracle中的for循环,若变量直接in一个列表。
4.range第一个参数为起始值,第二个参数为末尾值(不包含),第三个参数为步长。
5.list可以将一个元组转换成一个列表。
6.元组(),列表[],元组和列表都可以用下标访问,但是元组是只读的,列表是可以被修改的。
7.字符串可以用split分割,默认是空格分割,也可以用指定的字符分割。分割后拷贝称一个列表。
8.tuple可以把列表换成元组。也可以把字符串换成元组。
9.列表是可以使用insert进行插入新值。s.insert(1,'str'),在某个索引后面插入新值。
10.列表的append函数,在列表后面插入新值。
11.extend可以将一个列表整体append到另一个列表的尾部。
12.列表的pop函数,删除并且返回某个index的值,列表变短,返回index所在的值。
13.列表的remove函数,删除第一次出现的这个值。
14.列表的count返回某个值出现的次数。
15.用def来定义一个函数,以冒号缩进来标识程序体。,可以放入参数,作为形参。
16.位于函数体内的变量和形参都是局部变量。
17.如果在函数体内只能读全局变量,不能写,如果需要修改全局变量,那么就是需要声明一下,如global a。
18.可以使用return用来返回一个值。
19.可以用两个参数来接收函数返回的两个值,return不一定只能返回一个值,比如a,b = swap2(a,b)。
20.map函数第一个参数是一个函数f,第二个参数作为第一个函数的形参传入,如果有第三个参数,那么也是作为第一个函数的形参。
21.字典是一系列的键值对逗号间隔的集合。如果有多个相同的键不同的值,那么后面的值会覆盖前面的值。
22.字段需要初始化,例如d = dict()。通过d.keys()和d.values()来发访问键值。
23.dict函数可以将元组组成的列表转化成字典。
24.items可以将字典转换成列表。
25.get可以获取键对应的值,可以类似oracle的nvl函数,加上第二个参数,如果没有这个键,则返回第二个参数。
26.setdefault如果这个键没有值,那么就给这个键赋值。
27.zip可以把两个元组转换成一个由元组组成的列表。
sa = "aa bb cc dd ee ff gg hh"
sb = "ax bb dx ee fx"
sc = "dt bb fy cc hy ee tx"
bname = "bb"
ename = "ee"
st = sa.split()
n = range(0,len(st))
p1 = zip(n,st)
pt1 = dict(p1)
dp1 = pt1.keys()
print "pt1",pt1
st = sb.split()
n = range(0,len(st))
p2 = zip(n,st)
pt2 = dict(p2)
dp2 = pt2.keys()
print "pt2",pt2
st = sc.split()
n = range(0,len(st))
p3 = zip(n,st)
pt3 = dict(p3)
dp3 = pt3.keys()
print "pt3",pt3
print dp1
print dp2
print dp3
dp = []
dp.append(pt1)
dp.append(pt2)
dp.append(pt3)
print dp
for d in dp:
print 'd',d
for k in d.keys():
if d[k] == bname:
start = k
if d[k] == ename:
end = k
print start,end
for x in d.values()[start:end]:
print x,'->',
print d.values()[end]
r1 = "aa bb cc dd yx gg"
r2 = "tt yy ww cc ee hh"
bn = "bb"
en = "ee"
rd1 = r1.split()
rd2 = r2.split()
print rd1
print rd2
if bn in rd1:
bs = rd1.index(bn)
print "find begin station at", bs
if en in rd2:
es = rd2.index(en)
print "find end station at",es
i = 0
for x1 in rd1:
if x1 in rd2:
ex = rd1.index(x1)
print x1,rd1.index(x1),rd2.index(x1)
i += 1
for x in rd1[bs:ex]:
print x,'->',
print rd1[ex]
r1 = "aa bb cc dd yx gg"
r2 = "tt yy ww cc ff tx ee hh"
bn = "bb"
en = "ee"
rd1 = r1.split()
rd2 = r2.split()
print rd1
print rd2
if bn in rd1:
bs = rd1.index(bn)
print "find begin station at", bs
if en in rd2:
es = rd2.index(en)
print "find end station at",es
i = 0
for x1 in rd1:
if x1 in rd2:
ex1 = rd1.index(x1)
ex2 = rd2.index(x1)
print x1,rd1.index(x1),rd2.index(x1)
i += 1
for x in rd1[bs:ex1]:
print x,'->',
print rd1[ex1]
if ex2 < es:
for x in rd2[ex2:es]:
print x,'->',
print rd2[es]
else:
for x in rd2[ex2:es:-1]:
print x,'->',
print rd2[es]
r1 = "aa bb cc uu dd yx gg"
r2 = "tt yy ww mm cc ee hh"
r3 = "pp cx mm nn ee"
bn = "bb"
en = "ee"
rd1 = r1.split()
rd2 = r2.split()
rd3 = r3.split()
print rd1
print rd2
print rd3
def findExchange(r1,r2):
for x in r1:
if x in r2:
ex1 = r1.index(x)
ex2 = r2.index(x)
print x , r1.index(x), r2.index(x)
findExchange(rd1,rd2)
findExchange(rd2,rd3)
print '---------'
findExchange(rd1,[bn,en])
print '---------'
findExchange(rd2,[bn,en])
print '---------'
findExchange(rd3,[bn,en])