如何解决如何在循环中同时处理列表中的两个值,属性错误
我需要遍历一个点列表来计算一条线的距离。但是我从我创建的这段代码中得到了一个属性错误。我认为这是因为我重载了加法功能。如何解决这个问题,以便我可以计算距离并将其存储在列表中?
我尝试用 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 举报,一经查实,本站将立刻删除。