栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Day10

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Day10

1. 复习week 5 lecture 5.3 B, 5.3 C

2. 练习题(前6题)

3. 拓展升级练习 + 夯实基础

FileIO 复习

students = tuple() #标准形式
with open('s.txt') as f:
    for line in f:
        students += int(line),#不断创建tuple
    print(len(students))

 检测 remove() 语法括号里的内容是value还是index

#删除括号里的值
li = [1, 2, 3, 4, 5]
li.remove(2)
print(li)

可变参数

#可变参数, 调用时候比声明时传入了更多的参数
def show(file_path, *args, **kwargs):
    print(file_path)
    print("*args = ", args)
    print("*kwargs = ", kwargs)
show('22222', 'aaa','bbb',haha = 'xxx')

sorted用法

ls = [
    {'name': 'eric', 'grade':100, 'age':65},
    {'name': 'haha', 'grade':33, 'age':44},
    {'name': 'eee', 'grade':33, 'age':44},
    {'name': 'eer', 'grade':11, 'age':22},
]
info = {'eric':100, 'eee':22, '22':33, 'ss':44, }
print(info)
ls_info = list(info.items())#变成列表元组形式
print(ls_info)
# def guizi(d):
#     return d['grade'] #通过key来排序
# print(sorted(ls, key=guizi))
print(sorted(ls, key=lambda x:x['age']))
print(sorted(ls_info, key=lambda x:x[1], reverse=True))

类型与类型之间的互转

#字典与列表互转
info = {'eric':100, 'eee':22, '22':33, 'ss':44, }
for key in info:
    print(info[key])
for k, v in info.items():
    print(k, v)
print(list(info.values()))
print(list(info.keys()))
print(list(info.items()))
ls = [('eric', 100), ('eee', 22), ('22', 33), ('ss', 44)]
print(dict(ls))

#列表跟字符串之间的转化
print(list('abccde'))
li = ['a', 'b', 'c', 'c', 'd', 'e']
print(''.join(li))
print(' '.join(li))
print('/'.join(li))

#元组跟列表
li = ['a', 'b', 'c', 'c', 'd', 'e']
print(tuple(li))
t = ('a', 'b', 'c', 'c', 'd', 'e')
print(list(t))

#集合和列表
li = ['a', 'b', 'c', 'c', 'a', 'a']
print(set(li))
print(list(set(li)))

 歌词查重次数

example:《BLUE》Troye Sivan

创建 blue.txt,粘贴歌词

test3 运行程序

简单查重程序如下:

with open('blue.txt') as f:
    data = f.read()
    data = data.replace(',','')
    data1 = data.split(' ')
    print(data1)
    counts = {}
    for word in data1:
        counts[word] = counts.get(word, 0) +1
    print(counts)
    li = list(counts.items())
    print(li)
    li.sort(key=lambda x:x[1], reverse = True)
    print(li[:2])

stopwords规避之后,歌词查重

引用stopwords.txt

with open('blue.txt') as f:
    data = f.read()
    data = data.replace(',','')
    data1 = data.split(' ')
    print(data1)
    counts = {}
    for word in data1:
        counts[word] = counts.get(word, 0) +1
    print(counts)
#stopwords规避常用词
    stopwords = set(open('stopwords.txt').read().split())
    for word in stopwords:
        if word in counts.keys():
            del counts[word]
#遍历并删除歌词中的常用词
    li = list(counts.items())
    print(li)
    li.sort(key=lambda x:x[1], reverse = True)
    print(li[:2])

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/313678.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号