字符串小练习

1. 使用while循环输出1 2 3 4 5 6 8 9 10

count=1
while count <=10:
    if count==7:
        count+=1
        continue
    print(count)
    count+=1

2. 求1-100的所有数的和

s=0
count=0
while count <=100:
    s=s+count
    count+=1
print(s)

3. 输出 1-100 内的所有奇数

s=0
count=0
s1=0
while count <=100:
    if count % 2 !=0:
        s=s+count
        count+=1
    elif count % 2 ==0:
        s1=s1+count
        count+=1
print(s,s1)

5. 求1-2+3-4+5 … 99的所有数的和

s1=0
s2=0
count=0
while count <100:
    if count % 2 !=0:
        s1=s1+count
    else:
        s2=s2+count
    count+=1
print(s1-s2)

6.求平均值

a_list=[]
file_str='sd:asdf:32:sdf\n'\
'qw:213:321:wqew\n'\
'sad:ds:12312:dsa\n'
for file in file_str.split('\n'):
    if len(file.strip('')) !=0:
        a_list.append(int(file.split(':')[2]))
print(sum(a_list)/len(a_list))

7.字符串的分割:

#way1:字符串拼接处理
def splist_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+=' '
            continue
        else:
            string_new+=str_value
    print(string_new.split(' '))
splist_str1()
way2: 使用正则,split进行格式化字符串
import re
def split_str3():
    ds=[';','|',',','\t']
    s='qwer|tyu;iop|asd,fgh jkl xyz'
    print(re.split(r'[;|,\t]',s))
split_str3()

def split_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 if 6>=len(x)>=5])
print(list(filter(lambda x:6>=len(x)>=5,text2)))
def f(x):
    if 6>=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
def count_time(func):
    def wrap():
        start=time.time()
        func()
        stop=time.time()
        print('cost:{}'.format(stop-start))
    return wrap()
@count_time
def fun1():
    t=''
    for n in nums:
        t+=n
    return t
@count_time
def fun2():
    return ''.join(nums)

fun1()
fun2()
#说明使用join的效率要比使用生成器迭代高,但是比较消耗内存。使用迭代器的开销要小很多
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值