字符串练习:
http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1027/8443.html
取得校园新闻的编号
addr = 'http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0308/9005.html'print(addr[-14:-5])print(addr.rstrip('.html')[-9:])print(addr.rstrip('.html').split('_')[1])
docs.python.org/3/library/turtle.html
产生python文档的网址
site1 = 'https://docs.python.org/3/library/'site2 = '.html'want = ['turtle', 'index', 'string']for i in want: print('https://docs.python.org/3/library/{}.html'.format(i)) print(site1+i+site2)
http://news.gzcc.cn/html/xiaoyuanxinwen/4.html
产生校园新闻的一系列新闻页网址
for i in range(2, 231): s = 'http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i) print(s)
练习字符串内建函数:strip,lstrip,rstrip,split,count
用函数得到校园新闻编号
addr = 'http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0308/9005.html' print(addr.rstrip('.html')[-9:])print(addr.rstrip('.html').split('_')[1])
用函数统计一歌词中单词出现的次数
歌曲《Welcome to New York》:分析Welcome出现次数
song = '''Welcome to New York - Taylor SwiftWalking through a crowd the village is aglowKaleidoscope of loud heartbeats under coatsEverybody here wanted something moreSearching for a sound we hadn't heard beforeAnd it saidWelcome to New YorkIt's been waiting for youWelcome to New YorkWelcome to New YorkWelcome to New YorkIt's been waiting for youWelcome to New YorkWelcome to New YorkIt's a new soundtrackI can dance to this beat (beat)ForevermoreThe lights are so brightBut they never blind me (me)Welcome to New YorkIt's been waiting for youWelcome to New YorkWelcome to New YorkWhen we first dropped our bags on apartment floorsTook our broken hearts put them in a drawerEverybody here was someone else beforeAnd you can want who you wantBoys and boys and girls and girlsWelcome to New YorkIt's been waiting for youWelcome to New YorkWelcome to New YorkWelcome to New YorkIt's been waiting for youWelcome to New YorkWelcome to New YorkIt's a new soundtrackI can dance to this beat (beat)ForevermoreThe lights are so brightBut they never blind me (me)Welcome to New YorkIt's been waiting for youWelcome to New YorkWelcome to New YorkLike any great love it keeps you guessingLike any real love it's ever-changingLike any true love it drives you crazyBut you know you wouldn't change anything anything anythingWelcome to New YorkIt's been waiting for youWelcome to New YorkWelcome to New YorkWelcome to New YorkIt's been waiting for youWelcome to New YorkWelcome to New YorkIt's a new soundtrackI can dance to this beatThe lights are so brightBut they never blind meWelcome to New YorkNew soundtrackIt's been waiting for youWelcome to New YorkThe lights are so brightBut they never blind meWelcome to New YorkSo brightThey never blind meWelcome to New YorkWelcome to New York'''word = 'Welcome'print(word+'出现'+str(song.count('Welcome'))+'次')
将字符串分解成一个个的单词。
六级满分作文去掉标点符号后,分解成一个个单词,并了统计每个单词出现次数
article = ''' "avoid the rush-hours" must be the slogan of large cities the world over. whe rever you go, especially at week-ends or on holidays, you'll find that the tr ains a re packed, streets crowded, buses queued, restaurant tables shared. moreover, the smallest unforeseen event like a power-cut, an exceptionally heavy snowfall or rainfall can bring about conditions of utterinconvenience and chaos. city-dwellers are obliged by their environment to adopt a wholly unnatural way of life. theylose touch with nature and all the simple, good things of life like sunshine and f resh air atapremium . tall buildings block out the sun. traffic fumes pollute the atmosphere. the flow oftraffic goes on unceasingly and the noise never stops. the funny thing about it all in a large modern city is that you pay dearly for the "privilege" ofliving. the demand for accommodation is so great that it is of ten impossible for ordinarypeople to buy a house/flat of their own. furthermore, the cost of living is very high. just abouteve rything you buy is likely to be more expensive than it would be in the country. in addition, city-dwellers live under constant threat. the crime rate in most cities is very high. houses are often burgled. cities breed crime and violence and are full of places you would beafr aid to visit at night. if you think about it, they are not really fit to live in at all. can anyonereally doubt that the country is what man was born for and where he truly belongs? '''replace = ['"', '.', '?', ',', '(', ')']for i in replace: article = article.replace(i, ' ')artarray = article.split()print(artarray)for i in artarray: print(i+'出现'+str(article.count(i))+'次')
2.组合数据类型练习
分别定义字符串,列表,元组,字典,集合,并进行遍历。
字符串:
string = 'string'for i in string: print(i)
列表:
ls = list(range(5))ls2 = list('turtle')ls3 = ['h', 'e', 'l', 'l', 'o']for i in ls: print(i)for i in ls2: print(i)for i in ls3: print(i)
元组:
ls1 = list(range(5))ls2 = list('turtle')tuple1 = tuple(ls1)tuple2 = tuple(ls2)tuple3 = ('h', 'e', 'l', 'l', 'o')for i in tuple1: print(i)for i in tuple2: print(i)for i in tuple3: print(i)
字典:
dict = dict(zip('turtle', '123456'))for i in dict: print(i)for i in dict.keys(): print(i)for i in dict.values(): print(i)for i in dict.items(): print(i)
集合:
ls = list('turtle')set = set(ls)for i in set: print(i)
总结列表,元组,字典,集合的联系与区别。
联系:
元组、字典、集合都可以通过预先定义列表来进行创建;
并且元组中可以含有列表、列表中也可以包含元组;
元组中的列表的元素可以修改;
集合中的元素就是字典中的键值;
列表,元组,字典,集合之间可以相互转化。
区别:
元组和列表是有序列的,字典和集合是无序列的;
元组中除了列表元素里的元素可修改以外,其他不可以修改;
字典中的键不可以修改,值可以修改;
元组不能使用列表的方法增加自身的元素,集合可以使用add()方法添加元素;
元组和列表的元素可以重复,字典和集合的元素不能重复。