Python
喜欢伪装成学霸的学渣
这个作者很懒,什么都没留下…
展开
-
Python 字典排序
'''对字典中的值进行排序''' from random import randint dic={x:randint(60,100) for x in 'abcdf'} #使用sorted d1=sorted(dic) print(d1) ['a', 'b', 'c', 'd', 'f'] #会发现返回的是一个以键排列的list #思路 #字典转换成元祖比较 #(98,'a')&...原创 2020-01-01 23:17:42 · 66 阅读 · 0 评论 -
词频 统计
#不保留分隔符 re.split(r'\W+','hello, world') ['hello','world'] #保留分隔符 re.split(r'(\W+)','hello, world') # ['hello', ', ', 'world'] 假设xiaoshuo.txt为一个英文小说 r=open('xiaoshuo.txt','r').read() a=re.split(...原创 2020-01-01 22:18:31 · 111 阅读 · 0 评论 -
列表解析 列表元素频率统计 列表筛选
from collections import Counter from random import randint #产生一个list lis=[randint(0,10) for _ in range(10)] #筛选大于5的元素 l1=[x for x in lis if x>5] print(l1) [9, 7, 9, 6, 7] #如果带上else 则需要把if 写在f...原创 2020-01-01 18:02:50 · 235 阅读 · 0 评论 -
tuple1
from collections import namedtuple student=namedtuple('student','name age') s1=student(name='tom',age=19) print(s1.name) print(s1.age) name,age=range(2) s2=('apple',12) print(s2[name]) prin...原创 2020-01-01 17:38:46 · 206 阅读 · 0 评论