#way1:字符串拼接处理defsplist_str1():
my_strings='qwer|tyu;iop|asd,fgh jkl xyz'
split_types=['|',';',',','\t',' ']
string_new=''for str_value in my_strings:
if str_value in split_types:
string_new+=' 'continueelse:
string_new+=str_value
print(string_new.split(' '))
splist_str1()
way2: 使用正则,split进行格式化字符串
import re
defsplit_str3():
ds=[';','|',',','\t']
s='qwer|tyu;iop|asd,fgh jkl xyz'
print(re.split(r'[;|,\t]',s))
split_str3()
defsplit_str4():
ds=[';','|',',','\t']
s='qwer|tyu;iop||||asd,fgh jkl xyz'
print(re.split(r'[;|,\t ]+',s))
split_str4()
8.找到最长的单词:
text1='Hi Everyone your work is going to fill a large part of your life. '\
'And the only way to be truly satisfied is to do what '\
'you believe is great work in United Nations'
text2=text1.split(' ')
text3=sorted(text2,key=lambda x:len(x),reverse=True)
print(text3[0])
#找到单词的长度是5或者6的
print([x for x in text2 if6>=len(x)>=5])
print(list(filter(lambda x:6>=len(x)>=5,text2)))
deff(x):if6>=len(x)>=5:
return x
print([x for x in map(f,text2) if x])
#列出首字母大写的单词:
print([w for w in text2 if w.istitle()])
import re
print([w for w in text2 if re.search(r'^[A-Z]',w)])
#打印最频繁的单词:from collections import Counter
print(Counter(text2).most_common(3))
字符串拼接
#用+号
names=['Hello',' James',',',' how',' are',' you', '!']
#way1:
s=''for n in names:
s+=n
print(s)
#way2:直接使用join来拼接
print(''.join(names))
#如果比较复杂:
names2=['Hello',123,'James']
print(''.join(map(str,names2))) #如果列表特别大不建议使用,开销比较大,可以使用生成器迭代一下
print(''.join((str(x) for x in names2)))
# 用+和join的区别:
nums=[str(x) for x in range(1,100000)]
import time
defcount_time(func):defwrap():
start=time.time()
func()
stop=time.time()
print('cost:{}'.format(stop-start))
return wrap()
@count_timedeffun1():
t=''for n in nums:
t+=n
return t
@count_timedeffun2():return''.join(nums)
fun1()
fun2()
#说明使用join的效率要比使用生成器迭代高,但是比较消耗内存。使用迭代器的开销要小很多