首页 > 其他 > 详细

元祖变成字符串

时间:2017-01-12 22:53:00      阅读:373      评论:0      收藏:0      [点我收藏+]

 

 元祖变成字符串采用这种方式:

 技术分享

 字符串中替换部分字符串

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)

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‘)]

它只匹配对应的,成对输出,剩下的不输出

返回的都是固定格式的,返回元祖组成的列表,这是确定的格式。

数组,字符串,元祖他们相互之间是可以用zip()的:

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

 

元祖变成字符串

原文:http://www.cnblogs.com/lonely-buffoon/p/6279769.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!