8 练习

#练习:
#请编写一个函数实现去掉字符串的头尾空格:
#如: 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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 2017.1.8终于得闲画完,年关好忙啊 ₍₍ (̨̡ ‾᷄ᗣ‾᷅ )̧̢ ₎₎
    水墨镇阅读 151评论 0 3
  • 记录1 1.伙伴A(名字):颜东东 2.对方原话简单描述:顾客收到的到付价格比我算的寄付价格低 3.你感受到对方对...
    f134c363a9b1阅读 178评论 0 0
  • 这个片段是拆书家从书中挑选出来的学习内容,跟着我一起来读吧! 谈吐优雅。听起来很不错,不是吗?这会将我们置于这样一...
    成不熟阅读 116评论 0 0
  • 练习内容:用图片填充文字——年终述职报告封面 练习步骤: 1.新建幻灯片:版式空白,设计比例4:3; 2.插入图片...
    陈陈_19b4阅读 242评论 0 0
  • 本周,古典老师《超级个体》的主题是精力管理, 由于现代职业人士工作时间长、休息时间少、生活无规律等特点, 个人的精...
    池公子阅读 148评论 2 3