python项目:excel数据处理并打包

python项目:excel数据处理并打包,第1张

用一个新输入的点利用三个标准点对称出三个点,然后在所有点中找出离这对称出的三个点最近的一个点,用欧式距离

# -*- coding:utf-8 -*-
from openpyxl import Workbook, load_workbook
import math
#实例化
x=input("请输入坐标").split()
print(x)
list1=[[122,159,113],[31,92,153],[155,85,78]]
for i in range(2):
    for j in range(2):
        list1[i][j]=2*list1[i][j]-int(x[i])
print("三个对称点坐标为:")
print(list1)
wb=load_workbook("E:/color.xlsx")
#print(wb.sheetnames)
sheet=wb.get_sheet_by_name("0508")
#print(sheet["B5"].value)
#print(sheet["B"+x[0]].value)
list=[]
for row in sheet.iter_rows(min_row=2,max_row=87,min_col=4,max_col=6):
    k=0
    sum1, sum2, sum3 = 0, 0, 0
    for cell in row:
        x1=(list1[0][k]-cell.value)*(list1[0][k]-cell.value)
        x2 = (list1[1][k] - cell.value) * (list1[1][k] - cell.value)
        x3 = (list1[2][k] - cell.value) * (list1[2][k] - cell.value)
        sum1+=x1
        sum2+=x2
        sum3+=x3
        #print(cell.value,end=",")
    sum1=math.sqrt(sum1)
    sum2 = math.sqrt(sum2)
    sum3 = math.sqrt(sum3)
    list.append(sum1+sum2+sum3)
    #print()
print("各点距离列表为:")
print(list)
list2=list
lista=[]
for i in range(len(list2)):
    lista.append(i+1)
print(len(lista))
print(len(list2))
for i in range(len(list2)-1,0,-1):
    for j in range(i):
        if list2[j]>list2[i]:
            list2[j], list2[i] = list2[i], list2[j]
            lista[j], lista[i] = lista[i], lista[j]
print("排序后各点距离列表为:")
print(list2)
print("对应ID列表为:")
print(lista)
list3=[]
# for i in range(len(list2)-1):
#     for j in range(len(list)-1):
#         if list2[i]==list[j]:
#             list3.append(sheet["B"+str(i+2)].value)
for i in lista:
    list3.append(sheet["B" + str(i + 1)].value)
print("对应名字列表为:")
print(list3)
enda=input("点击退出")


知识点:# -- coding:utf-8 --必须要加,不然会报错
一个空间换时间上的优化
得在pycharm里的控制台用pyinstaller命令
要用pyinstaller打包数据文件得用绝对路径
excel *** 作直接看代码大概就能懂吧。。。

1.1修改了两个bug,range数给少了,k忘了累加1
根据业务需要改成三个分开算了:

# -*- coding:utf-8 -*-
from openpyxl import Workbook, load_workbook
import math
#实例化
x=input("请输入坐标").split()
print(x)
list1=[[122,159,113],[31,92,153],[155,85,78]]
for i in range(3):
    for j in range(3):
        list1[i][j]=2*list1[i][j]-int(x[i])
print("三个对称点坐标为:")
print(list1)
wb=load_workbook("E:/color.xlsx")
#print(wb.sheetnames)
sheet=wb.get_sheet_by_name("0508")
#print(sheet["B5"].value)
#print(sheet["B"+x[0]].value)
list=[]
# for row in sheet.iter_rows(min_row=2,max_row=87,min_col=4,max_col=6):
#     k=0
#     sum1, sum2, sum3 = 0, 0, 0
#     for cell in row:
#         x1=(list1[0][k]-cell.value)*(list1[0][k]-cell.value)
#         x2 = (list1[1][k] - cell.value) * (list1[1][k] - cell.value)
#         x3 = (list1[2][k] - cell.value) * (list1[2][k] - cell.value)
#         sum1+=x1
#         sum2+=x2
#         sum3+=x3
#         k=k+1
#         #print(cell.value,end=",")
#     sum1=math.sqrt(sum1)
#     sum2 = math.sqrt(sum2)
#     sum3 = math.sqrt(sum3)
#     list.append(sum1+sum2+sum3)
#     #print()
# print("各点距离列表为:")
# print(list)
listA,listB,listC=[],[],[]
for row in sheet.iter_rows(min_row=2,max_row=87,min_col=4,max_col=6):
    k=0
    sum=0
    for cell in row:
        x=(list1[0][k]-cell.value)*(list1[0][k]-cell.value)
        sum+=x
        k=k+1
        #print(cell.value,end=",")
    sum=math.sqrt(sum)
    listA.append(sum)
    #print()
print("离第一个标准对称点距离列表为:")
print(listA)
for row in sheet.iter_rows(min_row=2,max_row=87,min_col=4,max_col=6):
    k=0
    sum=0
    for cell in row:
        x=(list1[1][k]-cell.value)*(list1[1][k]-cell.value)
        sum+=x
        k=k+1
        #print(cell.value,end=",")
    sum=math.sqrt(sum)
    listB.append(sum)
print("离第二个标准对称点距离列表为:")
print(listB)
for row in sheet.iter_rows(min_row=2,max_row=87,min_col=4,max_col=6):
    k=0
    sum=0
    for cell in row:
        x=(list1[2][k]-cell.value)*(list1[2][k]-cell.value)
        sum+=x
        k=k+1
        #print(cell.value,end=",")
    sum=math.sqrt(sum)
    listC.append(sum)
print("离第三个标准对称点距离列表为:")
print(listC)
print()
# print("各点距离列表为:")
# print(list)

print("关于第一个对称点:")
listA2=listA
lista=[]
for i in range(len(listA2)):
    lista.append(i+1)
# print(len(lista))
# print(len(listA2))
for i in range(len(listA2)-1,0,-1):
    for j in range(i):
        if listA2[j]>listA2[i]:
            listA2[j], listA2[i] = listA2[i], listA2[j]
            lista[j], lista[i] = lista[i], lista[j]
print("排序后各点距离列表为:")
print(listA2)
print("对应ID列表为:")
print(lista)
listA3=[]
# for i in range(len(list2)-1):
#     for j in range(len(list)-1):
#         if list2[i]==list[j]:
#             list3.append(sheet["B"+str(i+2)].value)
for i in lista:
    listA3.append(sheet["B" + str(i + 1)].value)
print("对应名字列表为:")
print(listA3)

print()
print("关于第二个对称点:")
listB2=listB
listb=[]
for i in range(len(listB2)):
    listb.append(i+1)
# print(len(listb))
# print(len(listB2))
for i in range(len(listB2)-1,0,-1):
    for j in range(i):
        if listB2[j]>listB2[i]:
            listB2[j], listB2[i] = listB2[i], listB2[j]
            listb[j], listb[i] = listb[i], listb[j]
print("排序后各点距离列表为:")
print(listB2)
print("对应ID列表为:")
print(listb)
listB3=[]
# for i in range(len(list2)-1):
#     for j in range(len(list)-1):
#         if list2[i]==list[j]:
#             list3.append(sheet["B"+str(i+2)].value)
for i in listb:
    listB3.append(sheet["B" + str(i + 1)].value)
print("对应名字列表为:")
print(listB3)
print()

print("关于第三个对称点:")
listC2=listC
listc=[]
for i in range(len(listC2)):
    listc.append(i+1)
# print(len(listc))
# print(len(listC2))
for i in range(len(listC2)-1,0,-1):
    for j in range(i):
        if listC2[j]>listC2[i]:
            listC2[j], listC2[i] = listC2[i], listC2[j]
            listc[j], listc[i] = listc[i], listc[j]
print("排序后各点距离列表为:")
print(listC2)
print("对应ID列表为:")
print(listc)
listC3=[]
# for i in range(len(list2)-1):
#     for j in range(len(list)-1):
#         if list2[i]==list[j]:
#             list3.append(sheet["B"+str(i+2)].value)
for i in listc:
    listC3.append(sheet["B" + str(i + 1)].value)
print("对应名字列表为:")
print(listC3)
enda=input("点击退出")


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/904557.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-15
下一篇 2022-05-15

发表评论

登录后才能评论

评论列表(0条)

保存