Python - TypeError:类型为“...”的对象没有len() [英] Python - TypeError: object of type '...' has no len()
本文介绍了Python - TypeError:类型为“...”的对象没有len()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一个类:
class CoordinateRow(object):
def __init __ ):
self.coordinate_row = []
def add(self,input):
self.coordinate_row.append(input)
def weave (self,other):
result = CoordinateRow()
length = len(self.coordinate_row)
for i in range(min(length,len(other))):
result.add(self.coordinate_row [i])
result.add(other.coordinate_row [i])
返回结果
这是我的程序的一部分:
def verwerk_regel
方法,则可以使用
cr = CoordinateRow()
coordinaten = regel.split()
用于coordinaten中的协调:
verwerkt_coordinaat = verwerk_coordinaat(coordinaat)
cr.add(verwerkt_coordinaat)
cr2 = CoordinateRow()
cr12 = cr.weave(cr2)
print cr12
def verwerk_coordinaat(coordinaat):
coordinaat = coordinaat.split ,$)
x = coordinaat [0]
y = coordinaat [1]
nieuw_coordinaat = Coordinate(x)
adjusted_x = nieuw_coordinaat.pas_x_aan()
return str但是我在cr12处遇到一个错误,但是我没有收到任何错误信息。 = cr.weave(cr2):
适用于i在范围内(min(length,len(other))):
TypeError:类型CoordinateRow的对象没有len()
解决方案c $ c> __ len __
len(self)
和len(other)
:class CoordinateRow(object):
def __init __(self):
self。 coordinate_row = []
def add(self,input):
self.coordinate_row.append(input)
def __len __(self):
return len(self.coordinate_row)
def weave(self,other):
result = CoordinateRow()
for i在范围内(min(len(self),len其他))):
result.add(self.coordinate_row [i])
result.add(other.coordinate_row [i])
返回结果
在[10] c = CoordinateRow()
In [11]:c.coordinate_row + = [1,2,3,4,5]
在[12]中:otherc = CoordinateRow()
在[ 13]:otherc.coordinate_row + = [4,5,6,7]
在[14]:c.weave(otherc).coordinate_row
[1,4,2,5,3,6 ,4,7]
Here is a class:
class CoordinateRow(object): def __init__(self): self.coordinate_row = [] def add(self, input): self.coordinate_row.append(input) def weave(self, other): result = CoordinateRow() length = len(self.coordinate_row) for i in range(min(length, len(other))): result.add(self.coordinate_row[i]) result.add(other.coordinate_row[i]) return result
This is a part of my program:
def verwerk_regel(regel): cr = CoordinateRow() coordinaten = regel.split() for coordinaat in coordinaten: verwerkt_coordinaat = verwerk_coordinaat(coordinaat) cr.add(verwerkt_coordinaat) cr2 = CoordinateRow() cr12 = cr.weave(cr2) print cr12 def verwerk_coordinaat(coordinaat): coordinaat = coordinaat.split(",") x = coordinaat[0] y = coordinaat[1] nieuw_coordinaat = Coordinate(x) adjusted_x = nieuw_coordinaat.pas_x_aan() return str(adjusted_x) + ',' + str(y)
But I'm geting an error at "cr12 = cr.weave(cr2)":
for i in range(min(length, len(other))):
TypeError: object of type 'CoordinateRow' has no len()
解决方案You need to add a
__len__
method, then you can uselen(self)
andlen(other)
:class CoordinateRow(object): def __init__(self): self.coordinate_row = [] def add(self, input): self.coordinate_row.append(input) def __len__(self): return len(self.coordinate_row) def weave(self, other): result = CoordinateRow() for i in range(min(len(self), len(other))): result.add(self.coordinate_row[i]) result.add(other.coordinate_row[i]) return result In [10]: c = CoordinateRow() In [11]: c.coordinate_row += [1,2,3,4,5] In [12]: otherc = CoordinateRow() In [13]: otherc.coordinate_row += [4,5,6,7] In [14]:c.weave(otherc).coordinate_row [1, 4, 2, 5, 3, 6, 4, 7]
这篇关于Python - TypeError:类型为“...”的对象没有len()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文