首先打开文件,遍历文件中的每一行,将每一行看做成列表,将列表的中的每一个元素遍历,如果有所需的数据就记录下来,方便以后的操作。
import math
job=input("请输入关键字:")
addr=input("请输入地址:")
sal=float(input("请输入薪资(万/月):"))
def P(job,addr,sal):
file = open("D:\\1.txt","r")
addrjob=0
job_num=0
addr_num=0
salminsum = 0
salmaxsum = 0
S_2=0
i=0
addrjob=0
for each_line in file:
i=i+1
list = each_line.split('\t')
if list[0]== job and list[1]==addr:
addrjob=addrjob+1
if list[0]==job:
job_num=job_num+1
if list[1]==addr:
addr_num=addr_num+1
salminsum=salminsum+float(list[2])
salmaxsum=salmaxsum+float(list[3])
file.close()
file = open("D:\\1.txt","r")
i=0
for each_line1 in file:
i=i+1
li = each_line1.split('\t')
if li[1]==addr:
S_2=S_2+((float(li[2])-(salminsum/addr_num))**2)/addr_num
file.close()
p=addr_num/i
p_job_addr=addrjob/addr_num
bu=salminsum/addr_num
f=(1/(math.sqrt(2*S_2*(math.pi))))*(math.e)**((-(sal-bu)**2)/(2*S_2))
print("该情况的概率为:",p*(p_job_addr)*f)
P(job,addr,sal)
#b=P("java","北京",1.8)
#print("北京java工资为1.8w:",b)
#s=P("java","上海",1.8)
#print("上海java工资为1.8w:",s)
#g=P("java","广州",1.8)
#print("广州java工资为1.8w:",g)
#sz=P("java","深圳",1.8)
#print("深圳java工资为1.8w:",sz)
#pb=P("python","北京",1.8)
#print("北京python工资为1.8w:",pb)