字符串、组合数据类型练习

1、字符串练习:

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

取得校园新闻的编号

addr = 'news.gzcc.cn/html/2018/xiaoyuanxinwen_0318/9041.html'[-14:-5]
print(addr)

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

产生python文档的网址

addr1 = 'http://docs.python.org/3.6/library/'
addr2 = '.html'
addr = addr1 + 'turtle' + addr2
print(addr)

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

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

addr1 = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
addr2 = '.html'
for i in range(2,20):
    addr = addr1 + str(i) + addr2
    print(addr)

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

用函数得到校园新闻编号

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

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

s5 = '''
作曲 : 周华健
作词 : 黄婷/王微
编曲:黄韵仁

远方的路路蜿蜒

前方的风吹多远

繁星满天陪伴我

追随着你的脚步 多少年

晨昏露霜在交错

苦乐聚散都经过

我们并肩去探索

挑战那未知的风波 不寂寞

翻山越岭唱美丽的歌

亲爱的伙伴你和我

最远的路在心上

流浪到倦了的终点是家乡

晨昏露霜在交错

苦乐聚散都经过

我们并肩去探索

挑战那未知的风波 不寂寞

悲欢又离合满旅途中

时光分不开你和我
最美的梦在心上

流浪到倦了的终点是家乡

你和我一起寻找的是家乡
'''
s6 = s5.count('流浪')
print(s6)

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

s7 = '''
Everything was going well until all of a sudden, my butt started to itch. So, of course, I scratched it. This, of course, only made things worse. Within a few seconds my bottom started to feel a burning sensation. I pulled the hose out from my back, thinking that maybe the water was too hot, but the damage was done.
'''
s8 = s7.replace(',',' ').replace('.',' ').replace('\n',' ').split(' ')
print(s8)

2.组合数据类型练习

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

 字符串定义及遍历

str = '大爱Python!'
for i in str:
    print(i)
列表定义及遍历
list = ['this','is','string','example']
for i in list:
    print(i)
元组定义及遍历
tuple = ['Michael','Bob','Tracy']
for i in range(len(tuple)):
    print(tuple[i])
字典定义及遍历
dict = {'Bob':75,'Michael':95,'Tracy':85}
for key in dict:
    print(key)
for key in dict:
    print(dict[key])

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

列表:列表是可变的数据数列,是有序的,用“[]”表示。列表支持多层嵌套而且可以对列表进行更新、增加、删除操作。

元组:元组是不可变的数据序列,是有序的,用“()”表示。元组不能对元组进行更新、增加、删除操作。支持多层嵌套,想创建包含一个元素的元组,要在元素后加“,”。

字典:有键和键值,用“()”表示,每一组用冒号连起来,然后各组用逗号隔开。字典是可变的无序的。

集合:集合是 无序、可变、不重复的。

元组和列表类似, 都是有序的,只是元组不可修改内容,这里的“不可修改内容是指不可修改元组元素的指向, 但是可以修改内容所指向的内容。集合和字典都是无序的,但是集合只有键而没有键值。

posted @ 2018-03-20 17:27  188吴梅英  阅读(315)  评论(0编辑  收藏  举报