国二赏析(五)

2019-09-14  本文已影响0人  小董不太懂
import random
brandlist = ['华为','中兴','小米','魅族','三星']
random.seed(0)
i = random.randint(1,len(brandlist))
name = brandlist[i]
print(name)
import jieba
s = input('请输入待测字符串:')
n = len(s)
m = len(jieba.lcut(s))
print('中文字符个数{},中文词语个数{}'.format(n,m))
********************************************************************************
D:\anaconda\python.exe D:/bilibili大学/简书代码/推导式.py
请输入待测字符串:俄罗斯举办世界杯
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\董贺贺\AppData\Local\Temp\jieba.cache
Loading model cost 0.633 seconds.
中文字符个数8,中文词语个数3
Prefix dict has been built succesfully.
n = eval(input('请输入数量:'))
if n>0:
        if n==1:
                cost = 160
        elif n<5 and n>1:
                cost = 160*n*0.9
        elif n>4 and n<10:
                cost = 160*n*0.8
        elif n>9:
                cost = 160*n*0.7
print('总额为:',cost)
import turtle
turtle.pensize(2)
d = -45
for i in range(4):
        turtle.seth(d)
        d += 90
        turtle.fd(200)
names = input()
t = names.split()
d = {}
for i in range(len(t)):
        d[t[i]]=d.get(t[i],0)+1
ls = list(d.items())
ls.sort(key = lambda x:x[1],reverse=True)
for k in ls:
        print('{}:{}'.format(k[0],k[1]))
*******************************************************************
计算机 男优 KTV 鸭子 销售 男优 妓院 男优 计算机 男优
男优:4
计算机:2
妓院:1
鸭子:1
KTV:1
销售:1

说到这,不得不喷一下这个叫梁永的老师,您是真菜,肯定是冒充西交的研究生,西交的水平能LOW到这样了吗,真特么丢人,不会做不做就是了,还尼玛改题目,你咋不给自己出一道200分的附加题。干脆自己给自己改试卷得了。

重点说一下这个split()和replace()函数都会产生副本,不影响原文件

  1. 第一问

    方法一:

with open(r'D:\KSWJJ\66000001\sensor.txt')as f:
    lines = f.readlines()
for line in lines:
    ls = line.strip(' \n\r').split(',')
    if ls[1]==' earpa001':
        with open(r'D:\KSWJJ\66000001\earpa001.txt','a')as fo:
            fo.write('{},{},{},{}\n'.format(ls[0],ls[1],ls[2],ls[3])) 
**********************************************************************
2016/5/31 0:20, earpa001,1,1
2016/5/31 2:26, earpa001,1,6
2016/5/31 3:12, earpa001,1,1
2016/5/31 5:57, earpa001,1,6
2016/5/31 6:59, earpa001,1,1

方法二:

with open(r'D:\KSWJJ\66000001\sensor.txt')as f:
    for line in f:
        ls = line.strip(' \n\r').split(',')
        if ls[1].count(' earpa001') == 1:
            with open(r'D:\KSWJJ\66000001\earpa001.txt','a')as fo:
                fo.write('{},{},{},{}\n'.format(ls[0],ls[1],ls[2],ls[3]))
  1. 第二问
d = {}
with open(r'D:\KSWJJ\66000001\earpa001.txt')as f:
    for line in f:
        t = line.strip(' \n\r').split(',')
        s = t[2]+'-'+t[3]
        d[s]=d.get(s,0)+1
ls = list(d.items())
ls.sort(key=lambda x:x[1], reverse=True) # 该语句用于排序
for i in range(len(ls)):
    m,n = ls[i]
    fo = open(r'D:\KSWJJ\66000001\earpa001_count.txt','a')
    fo.write('{},{}\n'.format(m,n))
fo.close()
上一篇 下一篇

猜你喜欢

热点阅读