python计算两个excel文件中所有坐标点之间的距离

news/2024/7/21 7:40:06 标签: 列表, python, 机器学习, 数据挖掘, excel
python"># -*- coding: utf-8 -*-
"""
Created on Wed Jul  7 14:57:44 2021
@author: Administrator
"""
import xlrd
import math
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
#将excel的两列转化为二个列表

worksheet = xlrd.open_workbook(u'C:/Users/Administrator/Desktop/data1.xlsx')
sheet_names= worksheet.sheet_names()
#print(sheet_names)
for sheet_name in sheet_names:
    sheet = worksheet.sheet_by_name(sheet_name)
    rows = sheet.nrows # 获取行数
    cols = sheet.ncols # 获取列数,尽管没用到
    all_content1 = []
    for i in range(1,rows) : #excel第一行是列名,从第二列开始取数
        cell1 = sheet.cell_value(i, 0) # 取第一列列数据
        try:
            all_content1.append(cell1)
        except ValueError as e:
            pass
for sheet_name in sheet_names:
    sheet = worksheet.sheet_by_name(sheet_name)
    rows = sheet.nrows # 获取行数
    cols = sheet.ncols # 获取列数,尽管没用到
    all_content2 = []
    for i in range(1,rows) :
        cell2 = sheet.cell_value(i, 1) # 取第二列列数据
        try:
            all_content2.append(cell2)
        except ValueError as e:
            pass    
length1=len(all_content1) #计算数据列表的长度
data=zip(all_content1,all_content2) #使用zip函数将两个列表打包成一个元组


worksheet1 = xlrd.open_workbook(u'C:/Users/Administrator/Desktop/data2.xlsx')
sheet_names1= worksheet1.sheet_names()
#print(sheet_names)

for sheet_name1 in sheet_names1:
    sheet1 = worksheet1.sheet_by_name(sheet_name1)
    rows1 = sheet1.nrows # 获取行数
    cols1 = sheet1.ncols # 获取列数
    all_content3 = []
    for i in range(1,rows1) : #excel第一行是列名,从第二列开始取数
        cell3 = sheet1.cell_value(i, 0) # 取第一列列数据
        try:
            all_content3.append(cell3)
        except ValueError as e:
            pass
for sheet_name2 in sheet_names1:
    sheet2 = worksheet1.sheet_by_name(sheet_name2)
    rows2 = sheet2.nrows # 获取行数
    cols2 = sheet2.ncols # 获取列数,尽管没用到
    all_content4 = []
    for i in range(1,rows2) :
        cell4 = sheet2.cell_value(i, 1) # 取第二列列数据
        try:
            all_content4.append(cell4)
        except ValueError as e:
            pass   
data1=zip(all_content3,all_content4)
'''for each in data1:
    print(each)'''
length2=len(all_content3)

for i in range(length1):
    x1=all_content1[i]
    y1=all_content2[i]
    j=0
    while j<length2:
        x2=all_content3[j]
        y2=all_content4[j]
        d=math.sqrt(math.pow((x2-x1),2)+math.pow((y2-y1),2)) 
        print('data1表中%d号点与data2表中%d号点之间的距离为:%f'%(i+1,j+1,d))
        j+=1
    

1
2
距离


http://www.niftyadmin.cn/n/1712009.html

相关文章

人工智能 遗传算法

人工智能 遗传算法 题目 用遗传算法求解函数 20 (x ^ 2) (y ^ 2) - 10 * (cos(2 * π * x) cos(2 * π * y)) (-5 < x, y <5) 的最小值 思路 分析问题&#xff0c;确定了至少需要两个类&#xff0c;即种群&#xff08;Group&#xff09;与个体&#xff08;Individu…

python根据两组GPS经纬度excel坐标数据,计算其中一组坐标点方圆1000米内另一组坐标点的个数

# -*- coding: utf-8 -*- """ Created on Wed Jul 7 14:57:44 2021 author: Administrator """ import xlrd import math import numpy as np import pandas as pd import matplotlib as mpl import matplotlib.pyplot as plt#将excel的两列转化…

网络安全 基于scrapy框架与selenium、openpyxl库爬取国外各国家疫情统计汇总信息

网络安全 基于scrapy框架与selenium、openpyxl库爬取国外各国家疫情统计汇总信息 数据来源 https://voice.baidu.com/act/newpneumonia/newpneumonia/ 思路 由于目标页面中的数据是动态加载出来的&#xff0c;所以直接发起请求得到的响应是不包含任何有用数据的&#xff0c;所…

算法设计 格雷码问题

算法设计 格雷码问题 1. 问题描述 对于给定的正整数n&#xff0c;格雷码为满足如下条件的一个编码序列&#xff1a; (1) 序列由2n个编码组成&#xff0c;每个编码都是长度为n的二进制位串。 (2) 序列中无相同的编码。 (3) 序列中位置相邻的两个编码恰有一位不同。 例如&#x…

python将GPS时间戳批量转换为日期时间(年月日时分秒)

#将GPS时间戳数据批量转化为日期时间 import numpy as np import pandas as pd import time#设置Spyder右侧console区的print输出行列数无限制 pd.set_option(display.max_columns, None) pd.set_option(display.max_rows, None) #读取数据 datapd.read_excel(C:/Users/Adminis…

算法设计 L型组件填图问题

算法设计 L型组件填图问题 1. 问题描述 设B是一个nn棋盘&#xff0c;n2k,(k1,2,3,…)。用分治法设计一个算法&#xff0c;使得&#xff1a;用若干个L型条块可以覆盖住B的除一个特殊方格外的所有方格。其中&#xff0c;一个L型条块可以覆盖3个方格。且任意两个L型条块不能重叠覆…

算法设计 最长递增子序列问题

算法设计 最长递增子序列问题 1. 问题描述 求一个由n个整数组成的整数序列的最长递增子序列。一个整数序列的递增子序列可以是序列中非连续的数按照原序列顺序排列而成的。 最长递增子序列是其递增子序列中长度最长的。 2. 具体要求 输入&#xff1a;输入的第一行是一个正整数…

算法设计 凸多边形的三角剖分

算法设计 凸多边形的三角剖分 1. 问题描述 设P是一个有n个顶点的凸多边形&#xff0c;P中的弦是P中连接两个非相邻顶点的线段。用P中的n-3条弦将P剖分成n-2个三角形&#xff08;如下图所示&#xff09;。使得n-3弦的长度之和最小的三角剖分称为最优三角剖分。 2. 具体要求 输入…