zoukankan      html  css  js  c++  java
  • 字符串、组合数据类型练习

    字符串练习:

    http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html

    取得校园新闻的编号

    acc="http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html"
    print(acc[-14:-5])

    https://docs.python.org/3/library/turtle.html

    产生python文档的网址

    abb1="http://doc.python.org/3/library/"
    abb2=".html"
    abb3="turtle"
    print(abb1+abb3+abb2)

    http://news.gzcc.cn/html/xiaoyuanxinwen/4.html

    产生校园新闻的一系列新闻页网址

    add1="http://news.gzcc.cn/html/xiaoyuanxinwen"
    add2=".html"
    for i in range(2,10):
        add=add1 + str(i) + add2
        print(add)

    练习字符串内建函数:strip,lstrip,rstrip,split,count

    用函数得到校园新闻编号

    a="http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html"
    print(a.rstrip(".html").split("_")[1])

    用函数统计一歌词中单词出现的次数

    sing="""发过要发的梦 但热烈渐觉冰冻
    跌过痛过便会懂
    谁在作弄 沉默背后也想发狂
    很想找一刹来放纵 斗胆一次如反恐
    也试过太冲动 大件事没计轻重
    错过悔过仍要冲
    冲出困局 形象似义勇军般英勇
    但结果有没有停战的沟通
    如戴著面具去掩饰倦容 
    迎著炮火也依然表情轻松
    情绪不可激动 还若轻的举重
    来化解一切压力似失踪
    如放下面具去也许面红
    乘著晚风去追寻逍遥星空
    除去假的宽容 还真想不通
    曾年少的青葱 忘却世间的缚束
    谁明白像和你也许重逢
    遗憾太多却不能倒流的钟
    如琴声的哀号 连结他都哭诉
    谁明白我初衷 烦躁也是个梦
    但我真的痛"""
    
    print(sing.count(""))

    将字符串分解成一个个的单词。

    article="""China is a big old country and it has a history of more than five thousand years.
    We are proud of its culture and part of the culture even influences the world greatly. For example, 
    the thought of Confucius. As Chinese economy develops very fast and plays an important role in the world,
    more and more foreigners learn mandarin in Confucius institutions. The essence of Chinese traditional culture 
    can be tested by the time. It units the whole nation and no matter what kinds of disaster comes, we will fight 
    together for the country’s future. Today, many young people lose faith in traditional culture and they are crazy 
    for the western culture. They follow the western fashion and think we are the old style.
    But some day, they will realize they are wrong to abandon traditional culture."""
    
    
    
    a1=article.replace(","," ").replace("."," ").replace("  "," ").replace(""," ").replace("\n","").split(" ")
    print(a1)

    2.组合数据类型练习

    分别定义字符串,列表,元组,字典,集合,并进行遍历。

    字符串

    BL1 = 'Hello World'
    for x in BL1:
        print(x)

    列表

    BL2=['physics', 'chemistry', 1997, 2000,'李四']
    for x in BL2:
        print(x)

    元组

    BL3=('Jack','Luxi','faker','张三',2017)
    for x in BL3:
        print(x)

    字典

    BL4={'Massage':98,'Lisa':87,'Wanfox':76,'你是真的强':82}
    for y in BL4.keys():
        print(y)
    for x in BL4.values():
        print(x)
    for z in BL4.items():
        print(z)

    集合

    BL5=set('A3018王五是个心机BOY')
    for x in BL5:
        print(x)

    总结列表,元组,字典,集合的联系与区别。

    列表可以修改,里面可以嵌套元组。元组不可以修改,但是元组里面可以嵌套列表,嵌套的列表可以修改,列表和元组里面字符串的排列是有顺序的。字典里面的键是唯一的,键的值可以为空,但是字典里面

    的值可以相同。集合可以定义一大串字符,但是与字典一样,它们的排列是无序的。元组,列表可以充当集合、字典里面的元素,其中列表可以修改,元组不能修改

  • 相关阅读:
    【图片加载大小优化】
    img标签实现和背景图一样的显示效果——object-fit和object-position
    【ios bug解决】 输入框聚焦时光标不显示
    service worker 实现页面通信
    【获取url 问号后参数】防中文乱码
    js去掉url后某参数【函数封装】
    ES6字符串模板
    ES6扩展运算符和rest运算符
    ES6变量的解构赋值
    ES6新的声明方式,var let const三种声明方式的区别
  • 原文地址:https://www.cnblogs.com/yjwamao/p/8609693.html
Copyright © 2011-2022 走看看