元祖變成字符串 || 字符串中替換部分字符串 || enumberate能夠找到字符串,列表,元祖的的索引和值 || zip的用法 || 判斷變量的類型

 

 元祖變成字符串採用這種方式:python

 

 字符串中替換部分字符串數組

from string import Template

s = Template('hello,${name} how are you,this is ${point},welcome to you')
d = s.substitute(name = 'tom',point = 'beijing')
print d
輸出:hello,tom how are you,this is beijing,welcome to you
#enumberate能夠找到字符串,列表,元祖的的索引和值
f = 'helloworld' for i,t in enumerate(f): if(i == 3): print t 輸出:索引3對應的值是: l a = ['aa','bb','cc','dd'] for i,t in enumerate(a): if(t == 'bb'): print 'bb的索引是:',i 輸出:bb的索引是: 1 c = ('ee','ff','gg','hh') for i,t in enumerate(c): if('gg'==t): print 'gg的索引是:',i
輸出:gg的索引是: 2

zip(arg0,arg1)this

s ,t = 'hellotom','world'
print zip(s,t)
輸出:[('h', 'w'), ('e', 'o'), ('l', 'r'), ('l', 'l'), ('o', 'd')]

sss = ['aaa','bbb','ccc','ddd']
ppp = ['ggg','hhh','iii','kkk','jjj']
print zip(sss,ppp)

輸出:[('aaa', 'ggg'), ('bbb', 'hhh'), ('ccc', 'iii'), ('ddd', 'kkk')]
 
ss = ('tt','yy','uu','ii')
pp = ('11','22','33','44','55','66')
print zip(pp,ss)
輸出:[('11', 'tt'), ('22', 'yy'), ('33', 'uu'), ('44', 'ii')]

它只匹配對應的,成對輸出,剩下的不輸出spa

返回的都是固定格式的,返回元祖組成的列表,這是肯定的格式。code

數組,字符串,元祖他們相互之間是能夠用zip()的:blog

s4 = ('acv','bdv','eger')
p4 = ['ag','sfd','asf','sf']
print zip(s4,p4)
輸出:[('acv', 'ag'), ('bdv', 'sfd'), ('eger', 'asf')]

s5 = 'helloworld'
p5 = ['aa','bb','ee','ff']
print zip(s5,p5)
輸出:[('h', 'aa'), ('e', 'bb'), ('l', 'ee'), ('l', 'ff')]

s6 = 'perfectOne'
p6 = ('eee','yyy','uuu','www','lll')
print zip(s6,p6)
輸出:[('p', 'eee'), ('e', 'yyy'), ('r', 'uuu'), ('f', 'www'), ('e', 'lll')]

判斷變量的類型:isinstance('abc',str)索引

print isinstance('abc',str)
輸出:True
print type('abc')
輸出:<type 'str'> python中的string是str
相關文章
相關標籤/搜索