8 练习

作者: 萌二宝 | 来源:发表于2020-03-18 15:55 被阅读0次
#练习:
#请编写一个函数实现去掉字符串的头尾空格:
#如: trim(' hello') = 'hello'
#trim('hello ') = 'hello'
#trim('  hello world ') = 'hello world'
#trim('     hello world    ') = 'hello world'
def strJudge(str):
    #print(str.rfind(' '))
    print('input:{}'.format(str))
    if len(str) == 0:
        return "please input something"
    while str[0] == " ":
        str = str[1:]
        if len(str) == 0:
            return
    if len(str) == 0:
        return "nothing left"
    while str[-1] == " ":
        str = str[:-1]
        if len(str) == 0:
            break
    print("out: {}".format(str))
strJudge("       ")
strJudge("    sdf   ")
strJudge(" sdlkfj")
strJudge("sdlkfj.  ")
strJudge("")
---------------------------------------------
input:       
input:    sdf   
out: sdf
input: sdlkfj
out: sdlkfj
input:sdlkfj.  
out: sdlkfj.
input:
Out[9]:
'please input something'
#每个国家都有一定的地区号码, eg:+30希腊 +45丹麦 +51秘鲁 +65新加坡 +86中国
#写代码求出一下电话号码里面有几个国家:
# "+86 1123 +65 1234 +51 2347 +30 9123 +65 1246 +30 2347 +86 2935"
#python的高效率
str = "+86 1123 +65 1234 +51 2347 +30 9123 +65 1246 +30 2347 +86 2935"
print(str)
arrNew = str.split()
dic = {"+30":"希腊", "+45":"丹麦", "+51":"秘鲁", "+65":"新加坡", "+86": "中国"}
resultS = set([dic[item] for item in arrNew if item.startswith('+')])
print(resultS)
print(len(resultS))
--------------------------------------------------------------------------
+86 1123 +65 1234 +51 2347 +30 9123 +65 1246 +30 2347 +86 2935
{'中国', '秘鲁', '希腊', '新加坡'}
4

相关文章

  • UG编程练习图持续更新,需要的小伙伴请自行保存哦!

    练习1 练习2 练习3 练习4 练习5 练习6 练习7 练习8 练习9 练习10 练习11 练习12 练习13 练...

  • 练习8

    描线把眼睛又画毁了,下次一定小心 感觉不上色倒还好看些

  • 练习8

    九宫格 关键词:乐高 时间:5分钟 编故事 关键词:乐高 今天我玩了乐高,我努力努力还是不行的一个过程。...

  • 练习(8)

    很多天没写了,手有点生了。

  • 练习8

    1.每隔3秒到系统上获取已经登录的用户的信息;如果发现用户hacker登录,则将登录时间和主机记录于日志/var/...

  • 练习8

    刚才感受到一股惊吓,在这样的恐惧中,开始时我体验到气悬于胸。脑袋停止转动,然后是隐隐的不安和快速的心跳同时袭来。 ...

  • 练习8

  • 8 练习

  • 练习(8)

    现在正在发生的事情,过去发生过,将来一定会再发生!

  • 练习8

    【记录格式6】 一、想做、需要做的这件事是什么? 朋友问我关于写练习的情况。 二、请跳出来观识这件事能发生需要的客...

网友评论

      本文标题:8 练习

      本文链接:https://www.haomeiwen.com/subject/mxccyhtx.html