如何在循环中同时处理列表中的两个值,属性错误

如何解决如何在循环中同时处理列表中的两个值,属性错误

我需要遍历一个点列表来计算一条线的距离。但是我从我创建的这段代码中得到了一个属性错误。我认为这是因为我重载了加法功能。如何解决这个问题,以便我可以计算距离并将其存储在列表中?

我尝试用 dist = Line(i,i+1).distance 替换 dist = Line(i[0],i[1]).distance,但它会产生索引错误,我需要一个用于索引的重载函数,但说明中没有说明。

import math

class Point:
    '''Class that creates points. Attributes: Eastings and northings'''
    def __init__(self,x,y):
        self.eastings = x
        self.northings = y
    def getCoords(self):
        self.coords = (self.eastings,self.northings)
        return self.coords
    def setCoords(self,other_x,other_y):
        self.eastings = float(other_x)
        self.northings = float(other_y)
    def __str__(self):
        return f"{self.eastings},{self.northings}"
    def __add__(self,new_point):
        return Line(self,new_point)

class Line(Point):
    '''Class that creates line object based on two points'''
    def __init__(self,start,end):
        self.start = start
        self.end = end
        self.latitude = self.end.eastings - self.start.eastings
        self.departure = self.end.northings - self.start.northings
        self.distance = math.sqrt((self.latitude)**2 + (self.departure)**2)
        self.azimuth = math.degrees(math.atan2(self.departure,self.latitude))
    def __getitem__(self,index):
        if index == 0:
            ans = self.start
        elif index == 1:
            ans = self.end
        else:
            print("invalid index")
        return ans

class Lot(Line):
    def __init__(self,name,lstpoints):
        self.name = name
        self.corners = self.__setCorners(lstpoints)
        self.lines = self.__getLines(lstpoints)
        self.left = None
        self.right = None
    def __setCorners(self,lstpoints):
        dct = {}
        for k,val in enumerate(lstpoints):
            new = {f"C{k+1}":val}
            dct.update(new)
        return dct
    def __getLines(self,lstpoints):
        lst_lines = []
        lst_dist = []
        for k,val in enumerate(lstpoints):
            if k == len(lstpoints)-1:
                break
            else:
                new = f"C{k+1}-C{k+2}"
                lst_lines.append(new)
        for i in lstpoints:
            dist = Line(i,i+1).distance
        dct_lines = dict(zip(lst_lines,lst_dis))
        return dct_lines

#test code that yields the error
a = Point(0,0)
b = Point(1,1)
c = Point(1,0.5)
lot = Lot("A1",[a,b,c])
type(Lot)

错误

AttributeError                            Traceback (most recent call last)
<ipython-input-78-8644b897c98b> in <module>()
      2 b = Point(1,1)
      3 c = Point(1,0.5)
----> 4 lot = Lot("A1",c])
      5 type(Lot)

<ipython-input-77-8492f1fa52db> in __init__(self,lstpoints)
      3         self.name = name
      4         self.corners = self.__setCorners(lstpoints)
----> 5         self.lines = self.__getLines(lstpoints)
      6         self.left = None
      7         self.right = None

<ipython-input-77-8492f1fa52db> in __getLines(self,lstpoints)
     22                 lst_lines.append(new)
     23         for i in lstpoints:
---> 24             dist = Line(i,i+1).distance
     25         dct_lines = dict(zip(lst_lines,lst_dis))
     26         return dct_lines

<ipython-input-65-7ac5af0efeef> in __add__(self,new_point)
     13         return f"{self.eastings},{self.northings}"
     14     def __add__(self,new_point):
---> 15         return Line(self,new_point)

<ipython-input-72-7743a2ec7710> in __init__(self,end)
      4         self.start = start
      5         self.end = end
----> 6         self.latitude = self.end.eastings - self.start.eastings
      7         self.departure = self.end.northings - self.start.northings
      8         self.distance = math.sqrt((self.latitude)**2 + (self.departure)**2)

AttributeError: 'int' object has no attribute 'eastings'

解决方法

尝试替换这些行:

for i in lstpoints:
        dist = Line(i,i+1).distance

通过这些:

for i in range(len(lstpoints)):
        dist = Line(lstpoints[i],lstpoints[i+1]).distance

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?