这个例子的需求是,将一个字符串转换成以字符为元素的列表,列表首位元素可以是空字符,然后在字符中间插入若干空字符元素,然后将其转化为字符串,要求去除首位空格,但保留中间的空字符为占位符,也就是有几个空字符,就用几个占位符(*,?等)替代。
字符串和列表可以通过 list, join 方法来进行互转,
#list() can convert string to list,
#"".join() can convert list to string,
#and remove the empty char at the begining and the end of the word
word = 'good'
wordlist = list(word)
wordlistwithblank = ['',''] + wordlist + ['']
wordlistwithblank2word = "".join(wordlistwithblank)
print(word)
print(wordlist)
print(wordlistwithblank)
print(wordlistwithblank2word)
结果
good
['g', 'o', 'o', 'd']['', '', 'g', 'o', 'o', 'd', '']good
通过 join 方法合并列表时,中间的空字符也会被去除,如以下代码输出的结果,如果这不是我们想要的结果,应该怎么改进呢?
#list() can convert string to list,
#"".join() can convert list to string, it will remove the empty char at the middle of the word. that's not what we expecte
word = 'good'
wordlist = list(word)
wordlistwithblank = ['',''] + wordlist + ['']
wordlistwithblank.insert(4,'')
wordlistwithblank2word = "".join(wordlistwithblank)
print(word)
print(wordlist)
print(wordlistwithblank)
print(wordlistwithblank2word)
结果
god['g', 'o', 'd']['', '', 'g', 'o', '', 'd', '']god
字符串的strip方法可以去除指定的首尾字符,如以下例子所示
'''
Python中的strip用于去除字符串的首位字符,
同理,lstrip用于去除左边 的字符,
rstrip用于去除右边的字符。
这三个函数都可传入一个参数,指定要去除的首尾字符。
注意的是,传入的是一个字符数组,编译器去除两端所有相应 的字符,直到没有匹配的字符,比如:
'''
#依次被去除首尾在['s','a','y']数组内的字符,直到字符在不数组内。
word = 'saaaayyes noyaaaass'
print (word.strip('say') )
输出结果为
es no
利用字符 * 连接列表,然后利用strip去除首尾不需要的 * , 这样发现字符串中间多出了一些不必要 *
#replace '' with *
word = 'god'
wordlist = list(word)
wordlistwithblank = ['',''] + wordlist + ['']
wordlistwithblank.insert(4,'')
wordlistwithblank2wordwithstar = "*".join(wordlistwithblank)
wordlistwithblank2word = wordlistwithblank2wordwithstar.strip('*')
print(word)
print(wordlist)
print(wordlistwithblank)
print(wordlistwithblank2wordwithstar)
print(wordlistwithblank2word)
输出结果为:
god['g', 'o', 'd']['', '', 'g', 'o', '', 'd', '']**g*o**o*d*g*o**o*d
再进行替换,可以保留这个字符串的正确结构,即以问号做了占位符,以后可以用它来做正则匹配,找到字典中和它结构相同的字符串。
wordlistwithblank2word = wordlistwithblank2word.replace('**','?')
wordlistwithblank2word = wordlistwithblank2word.replace('*','')
wordlistwithblank2word
输出结果为
go?d
到此为止,似乎已经满足我们的要求了,但是再多测一步看看,在字符串中间多插入一个空字符,其他不变
#insert 2 '' into the string , replace '' with *
word = 'god'
wordlist = list(word)
wordlistwithblank = ['',''] + wordlist + ['']
wordlistwithblank.insert(4,'')
wordlistwithblank.insert(4,'')
wordlistwithblank2wordwithstar = "*".join(wordlistwithblank)
wordlistwithblank2word = wordlistwithblank2wordwithstar.strip('*')
wordlistwithblank2word = wordlistwithblank2word.replace('**','?')
wordlistwithblank2word = wordlistwithblank2word.replace('*','')
wordlistwithblank2word
print(word)
print(wordlist)
print(wordlistwithblank)
print(wordlistwithblank2wordwithstar)
print(wordlistwithblank2word)
结果为:
god['g', 'o', 'd']['', '', 'g', 'o', '', '', 'd', '']**g*o***d*go?d
可见两个空字符被替换成了一个占位符,说明这个方法是失败的,那该如何改进呢?
找不到现成的方法,就自己动手做一个
def convert2word(slist):
wordsub = ''
startflag = False;
for s in slist:
if len(s) == 0 and startflag == False:
continue
elif len(s) == 0 and startflag == True:
wordsub = wordsub + '?'
else:
startflag = True
wordsub = wordsub + s
wordsub = wordsub.strip('?')
return wordsub
print(convert2word(wordlistwithblank))
输出结果如下:
go??d
字符串中间正确包含了两个所需要的占位符,保留了所转换字符串的结构
领取专属 10元无门槛券
私享最新 技术干货