를 생성하는 방법 하나 이상의 목록에서 목록에서 사용하는 파이썬 기능

0

질문

내가 만들려고 하는 8 개의 퍼즐 문제 해결자는 다른 알고리즘을 사용하여,같은 BFS,DFS,A 가 포함되어 있습니다. using python. 에 대한 사람들에 익숙하지 않은 문제의 8 퍼즐 문제는 게임으로 구성된 3 행 3 열에 있습니다. 로 이동할 수 있습니다 빈 타일만 수평 또는 수직으로,0 은 빈 타일이 있습니다. 그것은 다음과 같습니다(수가 없었 추가 이미지 때문에 내 계정 reputation.):

https://miro.medium.com/max/679/1*yekmcvT48y6mB8dIcK967Q.png

initial_state = [0,1,3,4,2,5,7,8,6]
goal_state = [1,2,3,4,5,6,7,8,0]
    
def find_zero(state):
       global loc_of_zero
       loc_of_zero = (state.index(0))


def swap_positions(list, pos1, pos2):
       first = list.pop(pos1)
       second = list.pop(pos2-1)

       list.insert(pos1,second)
       list.insert(pos2,first)
       return list

 def find_new_nodes(state):
      if loc_of_zero == 0:
         right = swap_positions(initial_state,0,1)
         left = swap_positions(initial_state,0,3)
         return(right,left)




find_zero(initial_state)
print(find_new_nodes(initial_state))   

문제는 나는 이것이 내가 원하는 기능"find_new_nodes(주)"return2 개의 다른 목록을 선택할 수 있습니다 가장 유망한 노드를 알고리즘에 따라니다)합니다. 하지만 출력이 나의 코드로 구성되어 있는 두 개의 동일한 목록입니다.

이것은 출력: ([4, 0, 3, 1, 2, 5, 7, 8, 6], [4, 0, 3, 1, 2, 5, 7, 8, 6])

나는 무엇을 할 수 있게 돌아 2 다른 나열? 나의 목표는 반환 가능한 모든 움직임에 따라 0,를 사용하 find_new_nodes 기능입니다. 죄송하는 경우 이 질문을,이번에 처음으로 만드는 프로젝트에 이 복잡합니다.

3

최고의 응답

1

문제는 swap_positions 에 대한 참조를 가져옵 글로벌 initial_state 와 복제하지 않습니다. 그래서 모두 통화 swap_positions 변 동일한 배열입니다. 는 솔루션을 복제하는 배열에 먼저 전화: right = swap_positions(initial_state[:],0,1)

아마도 더 나은 솔루션 swap_positions 수도:

# please do not name variables same as builtin names
def swap_positions(lis, pos1, pos2):
       # create a new tuple of both elements and destruct it directly
       lis[pos1], lis[pos2] = lis[pos2], lis[pos1]
       return lis

또한 여기에서

2021-11-22 13:05:24
0

정말 없는"동일한 두 개의 목록",당신은 하나의 목록은 개체는 당신이 돌아온다. 을 수정하지 않고 원래 목록에서 작동하는 두 개의 다른 목록을 통과해야 복사본다.

initial_state = [0,1,3,4,2,5,7,8,6]
goal_state = [1,2,3,4,5,6,7,8,0]

def find_zero(state):
    global loc_of_zero
    loc_of_zero = (state.index(0))


def swap_positions(states, pos1, pos2):
    first = states.pop(pos1)
    second = states.pop(pos2-1)

    states.insert(pos1,second)
    states.insert(pos2,first)
    return states

def find_new_nodes(states):
    if loc_of_zero == 0:
        right = swap_positions(states.copy(),0,1) # pass around a copy
        left = swap_positions(states.copy(),0,3) # pass around a copy
        return(right,left)

find_zero(initial_state)
print(find_new_nodes(initial_state))

측 참고 1:나는 이름의 vairable list 하기 states,그렇지 않으면 그것은 그림자 목록에 내장 기능

측 참고 2: find_new_nodes 으로 작동하지 않았 매개 변수는 대신 그것을 사용되는 글로벌 목록입니다. 내가 변경되었습니다.

Side note3:다른 방법이 있을 복사본 만들기(얕은)목록. 나는 생각한 list.copy() 은 가장 자세한 정보를 표시 하나입니다. 당신은 또한 복사본을 사용하 모듈,사용 [:] 또는 다른 뭔가가 있습니다.

출력:

([1, 0, 3, 4, 2, 5, 7, 8, 6], [4, 1, 3, 0, 2, 5, 7, 8, 6])
2021-11-22 13:06:24
0

고 확인,우선,일부 생각...

  1. 도를 사용하지""목록에 변수로,그것은이에 대한 식별자의 목록 형식입니다. 그것은 당신이 변화되고 있습니다.

  2. 일반적으로,그것은 나쁜 생각하는 글로벌 사용하 vars 등 loc_of_zero.

에 대한 문제점:

나는 문제는 당신의 많은 참조가 동일한 변수입니다. 그것을 방지하려고합니다. 하나의 아이디어:

from copy import deepcopy
def swap_positions(list0, pos1, pos2): 
    list1 = deepcopy(list0) 
    first = list1.pop(pos1) 
    second = list1.pop(pos2-1) 

    list1.insert(pos1,second) 
    list1.insert(pos2,first) 
    return list1 
2021-11-22 13:12:44

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................