0
다차원 배열을 반복하고 요소의 인덱스를 반환하는 데 문제가 있습니다. 내가하고있는 것은 다차원 배열을 통해 이동하는 MoveUp 함수를 만드는 것입니다.MultiDimensional Array의 항목 인덱스를 반환하고 해당 항목의 특성을 확인하는 방법
현재 내 find 메서드는 다차원 배열의 첫 번째 인덱스 내의 첫 번째 배열에있는 개체의 올바른 인덱스를 반환합니다. 다차원 배열 외부의 나머지 항목은 오류를 반환합니다.
UnboundLocalError: 'local variable 'column' refrenced before assignment'
또한 MoveUp 함수에서 오류가 발생합니다. 필드 배열의 항목 하나를 "활성"으로 설정하고 배열에서 위로 이동하는 시작점이되도록합니다. 하지만 어떤 이유로 내 코드가 내게이 오류를 준다.
AttributeError: 'list' object has no attribute 'status'
class Square:
def SetName(self, name):
self.name = name
def SetOwner(self,name):
self.controlled_by = name
def SetLED(self, name):
self.led = name
def SetPosition(self, position):
self.posiion = position
def SetStatus(self, status):
self.status = status
B1_A = Square()
B1_A.SetName("B_R1_A")
B1_A.SetOwner("Open")
B1_A.SetLED("Off")
B1_A.SetStatus("UnActive")
B1_B = Square()
B1_B.SetName("B_R1_B")
B1_B.SetOwner("Open")
B1_B.SetLED("Off")
B1_B.SetStatus("UnActive")
B2_A = Square()
B2_A.SetName("B_R2_A")
B2_A.SetOwner("Open")
B2_A.SetLED("Off")
B2_A.SetStatus("UnActive")
B2_B = Square()
B2_B.SetName("R_R2_B")
B2_B.SetOwner("Open")
B2_B.SetLED("Off")
B2_B.SetStatus("UnActive")
B3_A = Square()
B3_A.SetName("R_R3_A")
B3_A.SetOwner("Open")
B3_A.SetLED("Off")
B3_A.SetStatus("UnActive")
R1_A = Square()
R1_A.SetName("R_R1_A")
R1_A.SetOwner("Open")
R1_A.SetLED("Off")
R1_A.SetStatus("UnActive")
R1_B = Square()
R1_B.SetName("R_R1_B")
R1_B.SetOwner("Open")
R1_B.SetLED("Off")
R1_B.SetStatus("UnActive")
R2_A = Square()
R2_A.SetName("R_R2_A")
R2_A.SetOwner("Open")
R2_A.SetLED("Off")
R2_A.SetStatus("UnActive")
R2_B = Square()
R2_B.SetName("R_R2_B")
R2_B.SetOwner("Open")
R2_B.SetLED("Off")
R2_B.SetStatus("UnActive")
R3_A = Square()
R3_A.SetName("R_R3_A")
R3_A.SetOwner("Open")
R3_A.SetLED("Off")
R3_A.SetStatus("UnActive")
# MultiDimensional Array
Field = [[B1_A,B1_B],[B2_A,B2_B],[B3_A],[R3_A],[R2_A,R2_B],[R1_A,R1_B]]
#Find Index of Element in MultiDimenstional Array
def find(l, elem):
for row, i in enumerate(l):
try:
column = i.index(elem)
return row, column
except ValueError:
return row, column
return -1
print(find(Field, B1_A)) #Returns (0,0) Correct
print(find(Field, B1_B)) #Returns (0,1) Correct
#print(find(Field, B2_B)) #Throws Error
# Set a Square Status Active
B1_B.status ="Active"
def MoveUp():
#Iterate through each item in Field
for i in Field:
#if item status equal to Active
if i.status == "Active":
#if item index 3 item in the multidimensional array
if find(Field, i) == (2,0):
#current position is this index
current_position = (find(Field, i))
print(current_position)
#new position is this index + 1
new_position = current_position + 1
#old position if new position - 1
old_position = new_position - 1
#Set the servos status in the new postion to Active
Field(new_position).status = "Active"
#Set the servos status in the old position to UnActive
Field(old_position).status = "UnActive"
else:
#current position is this index
current_position = (find(Field, i))
#new position is this index + 2
new_position = current_position + 2
#old position if new position - 2
old_position = new_position - 2
#Set the servos status in the new postion to Active
Field(new_position).status = "Active"
#Set the servos status in the old position to UnActive
Field(old_position).status = "UnActive"
MoveUp()
업데이트 : 기능을 추가 검색 및 다차원 목록에서 항목을 검색 할 수있다 – Arnial