【我用Python--study】#Random to get the brand
import random
brandlists=['HuaWei','Apple','Vivo','mi','OPPO']
#random.seed(0)
x=random.randint(0,4)
name=brandlists[x]
print(name)
#get a security code
import random
list1 = []
for i in range(4):
a=random.randrange(0,10)
list1.append(str(a))
sc = "".join(list1)
print(sc)#get a security code(with letter and number)
import random
list2 = []
for i in range(4):
b=random.randrange(0,10)
if b < 5:
num=random.randrange(65,91)
list2.append(chr(num))
else:
num1=random.randrange(0,10)
list2.append(str(num1))
sc2 = "".join(list2)
print(sc2)
#open txt and select the useful information
f=open("name.txt")
names=f.readlines()
f.close()
f=open("vote.txt")
votes=f.readlines()
f.closef=open("vote1.txt","w")
D={}
NUM=0
for vote in votes:
num = len(vote.split())
if num==1 and vote in names:
D[vote[:-1]]=D.get(vote[:-1],0)+1
NUM+=1
else:
f.write(vote)
f.close()
l=list(D.items())
l.sort(key=lambda s:s[:1],reverse=True)
name=[0][0]
score=[0][1]
print("useful_numbers:{} person:{},numbers:{}".format(NUM,names,score))
#gey the decimal
a = eval(input("please input number: "))
b = int(a)
c = a-b
print("{}".format(round(c,4)))
#Using number get the letter
s = input("please input one number: ")
print("get one letter: {}".format(chr(ord('a')+int(s)-1)))
#jieba库的使用
1分词
import jieba
s = input("请输入一个字符串")
n = len(s)#len进行词数统计时要进行列表形式的转换
m = len(jieba.lcut(s))#jieba.lcut分词后,使得数据变成列表型数据(可以用:type(jieba.lcut(s) 测试))
print("中文字符数为{},中文词语数为{}。".format(n,m))
2分词和计数
import jieba
excludes = {"我爱","学习",\
\
"学习","爱我","我爱","学习",\
\
"学习","爱我","我爱","学习",\
\
"学习","爱我"}
f = open("文本.txt","r",encoding = 'UTF-8')
txt = f.read()
f.close()
words = jieba.lcut(txt)
counts = {}
for word in words:
if len(word)==1:
continue
else:
counts[word]=counts.get(word,0)+1
for word in excludes:
del(counts[word])#删除不需要的词items = list(counts.items())#加入items使得键和值都转换成列表保留
items.sort(key=lambdax:x[1],reverse = True)#sort排序 reverse = True降序 False升序键值排序参数 lamda x:x[1]
for i in range(15):
word,count = items[i]
print("{0:<10} {1:>5}".format(word,count))#0指word,1指count;
:<左对齐,:>右对齐;
10,5表示宽度
#if条件计算
n = eval(input("请输入数量:"))
#cost = n*160*折扣(if elif)
if n<=1:
cost = n*160*1
elif n<=4:
cost = n*160*0.9
elif n<=9:
cost = n*160*0.8
else:
cost = n*160*0.7cost = int(cost)
print("总金额:",cost)