gpt4 book ai didi

Python如何正确应用类

转载 作者:行者123 更新时间:2023-11-28 21:53:05 25 4
gpt4 key购买 nike

我写了下面的类和程序:

class Coordinate(object):
def __init__(self,x,y):
self.x_coordinate = x
self.y_coordinate = y

class CoordinatRow(object):
def __init__(self):
self.coordinaterow = []

def add_coordinaterow(self, coordinate):
self.coordinaterow.append(coordinate)

def weave(self,other):
lijst = []
for i in range(len(self.coordinaterow)):
lijst.append(self.coordinaterow[i])
lijst.append(other.row[i])
coordinaterow = lijst
print coordinaterow

程序:

from Coordinates import Coordinate, CoordinatRow
from ipy_lib import file_input

'''functies'''

def split_and_add(invoer):
rij = invoer.split('=')
for line in rij:
process_row(line)

def process_row(line):
temp_coordinate_row = CoordinatRow()
rij = line.split()
for coordinate in rij:
coor = process_coordinate(coordinate)
temp_coordinate_row.add_coordinaterow(coor)
return temp_coordinate_row

def process_coordinate(coordinate):
idunno = coordinate.split(',')
return Coordinate(int(idunno[0]),int(idunno[1]))

'''programma'''
bestand = file_input()
split_and_add(bestand)
rows = split_and_add(bestand)
for row in rows:
row.weave(row)

输入如下所示:

5,4 4,5 8,7=6,3 3,2 9,6 4,3=7,6=9,8=5,5 7,8 6,5 6,4

每行由 = 分隔。我想像这样将每一行与下一行编织在一起:

5.4, 4.5, 8.7 weave with 6.3, 3.2, 9.6, 4.3
5.4, 6.3, 4.5, 3.2, 8.7, 9.6, 4.3

并将下一行放入这个新行。

问题在于我认为我应用类(class)的方式。我收到此错误:

for row in rows:
TypeError: 'NoneType' object is not iterable

这里出了什么问题?

最佳答案

def split_and_add(invoer):
rij = invoer.split('=')
for line in rij:
yield process_row(line)

关于Python如何正确应用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27252599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com