# Python-week04 **Repository Path**: shuimushisan/python-week04 ## Basic Information - **Project Name**: Python-week04 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-10-11 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Python-week04 ### 列表相关方法 序号|方法|作用 ---|:--:|---: 1|list.append()|在列表末尾添加新的元素 2|list.count()|统计某个元素在列表中出现的次数 3|list.extend()|在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表) 4|list.index()|从列表中找出某个值第一个匹配项的索引位置 5|list.insert()|将对象插入列表 6|list.pop()|移除列表中的一个元素,并且返回该元素的值(若无输入数字,则默认为最后一个元素) 7|list.remove()|移除列表中某个值的第一个匹配项 8|ist.clear()|清空列表 9|list.copy()|复制列表 *** #### 练习代码 ### 练习1 打印 ``` # 占位符 name = input("姓名:") day = input("日期:") course = input("课程:") info = '''-------INFO OF %s ------- 名字:%s 日期:%s 课程:%s '''% (name,name,day,course) print(info) # format方式1字符串 name = input("姓名:") day = input("日期:") course = input("课程:") info = '''----- INFO OF ''' + name +'''------''' + ''' 名字:''' + name+''' 日期:''' + day+''' 课程:''' + course print(info) # format方式2 name = input("姓名:") day = input("日期:") course = input("课程:") info2 = '''-------INFO OF {_name} ------- 名字:{_name} 日期:{_day} 课程:{_course} '''.format(_name=name, _day=day, _course=course) print(info2) ``` *** ### 练习2 课表练习 ``` print("今日课程") print("(注意:日期请输入阿拉伯数字)") import datetime today = int(input("今日周几:")) if today == 1: print("有课"); print("1.上课时间:14:30-16:50,用户与视觉界面设计:3实206"); print("只有一节课,还挺轻松。") elif today == 2: print("有课"); print("1.上课时间:12:50-14:20,illustrator软件应用:2实203"); print("2.上课时间:14:30-15:55,大学英语(三)A班:10-405"); print("3.上课时间:16:10-17:35,广告心理学:1-204"); print("连着上三节课,太难受了吧...") elif today == 3: print("有课"); print("1.上课时间:8:00-9:30,毛概实践:5-303"); print("2.上课时间:18:45-20:55,Python语言:2实203"); print("今天早晚各一节课,中午要好好休息才行。") elif today == 4: print("有课"); print("1.上课时间:12:50-14:20,大学英语(三)A班:10-405"); print("2.上课时间:14:30-15:55,击剑:东区体育馆西面"); elif today == 5: print("有课"); print("1.上课时间:8:00-10:35,毛概理论:7-404"); print("2.上课时间:14:30-16:50,API、机器学习与人工智能:3实212"); print("周五了!!坚持就是胜利!") else: print("今天周末没课,做点什么充实自己叭~") print(" ") ``` *** ### 练习3 列表 ``` names = ["齐天","巨鹿","神树","降临","斗牛"] print(names) # append增加数据,可以增加到数据的最后 names.append("七重人格") print(names) # insert可以指定一个位置来增加数据 names.insert(1,"地球之盐") print(names) # 可以直接找到列表中某个值,然后赋新值 names[3] = "寻" print(names) # remove可以指定某个数据进行删除 names.remove("寻") print(names) # 如果想要清除数据可以使用clear进行清除 names.clear() print(names) # del可以选中[x]的某个数据位置进行删除 del names[1] print(names) # 如果加上指定位置,names.pop(1)<==> del names[1] names.pop(1) print(names) ``` *** ### 练习4 列表切片 ``` # 列表的切片 names = ["地球之盐","齐天","巨鹿","神树","降临","斗牛","七重人格"] print(names[1],names[2]) # 切片 仍然是一个列表 回顾 range(1,3) 末位 print(names[1:5]) # 如果从列表首位开始,可以省略0 print(names[0:4]) print(names[:4]) # 可以看出省略0和不省略0的输出的结果是一致的 # 从末位开始取,则使用”-”号 print(names[-6:-1]) print(names[-6:]) # python的count可以帮助我们计算在一组数据中某个数据一共出现了几次 # 比如以下数据里,可以利用它快速计算出4出现了几次 number = [1,2,3,4,4,4,4,4,4,4,4,4,5,6,6,3,2,1] print(number.count(4)) ``` *** #### 思考题 ### 练习代码 ``` songs = ["地球之盐","地球之盐","地球之盐","齐天","齐天","巨鹿","神树","神树","神树","降临","斗牛","斗牛","斗牛","斗牛","七重人格","七重人格"] # 循环遍历每一个元素 song = "巨鹿" for song in songs: print(song) # 如果遍历到某一个前面出现过,存到一个[] from collections import Counter #引入Counter chongfu = dict(Counter(songs)) print ([key for key,value in chongfu.items()if value > 1]) # 计算重名个数 print("地球之盐:",songs.count("地球之盐")) print("齐天:",songs.count("齐天")) print("神树:",songs.count("神树")) print("斗牛:",songs.count("斗牛")) print("七重人格:",songs.count("七重人格")) ``` *** #### 列表训练营 ### 练习代码1 字符串统计 ``` import string from collections import namedtuple def str_count(s): '''找出字符串中的中英文、空格、数字、标点符号个数''' count_en = count_dg = count_sp = count_zh = count_pu = 0 s_len = len(s) for c in s: if c in string.ascii_letters: count_en += 1 elif c.isdigit(): count_dg += 1 elif c.isspace(): count_sp += 1 elif c.isalpha(): count_zh += 1 else: count_pu += 1 total_chars = count_zh + count_en + count_sp + count_dg + count_pu if total_chars == s_len: return namedtuple('Count', ['total', 'zh', 'en', 'space', 'digit', 'punc'])(s_len, count_zh, count_en, count_sp, count_dg, count_pu) else: print('Something is wrong!') return None return None s = input("请输入字符串:") count = str_count(s) print(s, end='\n\n') print('该字符串共有 {} 个字符,其中有 {} 个汉字,{} 个英文,{} 个空格,{} 个数字,{} 个标点符号。'.format(count.total, count.zh, count.en, count.space, count.digit, count.punc)) ``` *** ### 练习代码2 投票 ``` print("班长竞选现在开始") print("班长候选人有:刘亦菲、范冰冰、彭于晏和迪丽热巴") print("请五位同学进行投票") student = { "刘亦菲":0, "范冰冰":0, "彭于晏":0, "迪丽热巴":0 } count = 0 while count<5: name = input("请输入你心仪班长的姓名:") if name == "刘亦菲": student["刘亦菲"]+=1 elif name == "范冰冰": student["范冰冰"]+=1 elif name == "彭于晏": student["彭于晏"] += 1 else: student["迪丽热巴"]+=1 count +=1 print(student) ```