简体   繁体   English

如何使用 python 函数从列表中生成多个列表

[英]How to generate more than one list from a list, using python functions

I am trying to make a 8 puzzle problem solver using different algorithms, such as BFS,DFS, A* etc. using python.我正在尝试使用 python 使用不同的算法(例如 BFS、DFS、A* 等)制作一个 8 拼图问题求解器。 For those who are not familiar with the problem, 8 puzzle problem is a game consisting of 3 rows and 3 columns.对于不熟悉这个问题的人来说,8 字谜题是一个由 3 行 3 列组成的游戏。 You can move the empty tile only horizontally or vertically, 0 represents the empty tile.您只能水平或垂直移动空图块,0 表示空图块。 It looks like this (I couldn't add the images due to my accounts reputation.):看起来像这样(由于我的帐户声誉,我无法添加图像。):

https://miro.medium.com/max/679/1*yekmcvT48y6mB8dIcK967Q.pnghttps://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))   

The problem I have is this, I want the function "find_new_nodes(state)" return 2 different lists, so I can choose the most promising node, depending on the algorithm) and so on.我遇到的问题是,我想要 function "find_new_nodes(state)" 返回 2 个不同的列表,所以我可以选择最有前途的节点,具体取决于算法)等等。 But the output of my code consists of two identical lists.但是我的代码的 output 由两个相同的列表组成。

This is my output: ([4, 0, 3, 1, 2, 5, 7, 8, 6], [4, 0, 3, 1, 2, 5, 7, 8, 6])这是我的 output: ([4, 0, 3, 1, 2, 5, 7, 8, 6], [4, 0, 3, 1, 2, 5, 7, 8, 6])

What can I do to make it return 2 different lists?我该怎么做才能让它返回 2 个不同的列表? My goal is to return all possible moves depending on where the 0 is, using the find_new_nodes function.我的目标是使用 find_new_nodes function 根据 0 的位置返回所有可能的移动。 Apologies if this is an easy question, This is my first time making a project this complicated.抱歉,如果这是一个简单的问题,这是我第一次使项目变得如此复杂。

The problem is that swap_positions obtains a reference to the global initial_state and not a clone of it.问题是swap_positions获得对全局initial_state的引用,而不是它的克隆。 So both calls to swap_positions mutate the same array.所以对swap_positions的两个调用都会改变同一个数组。 A solution would be to clone the array on the first call: right = swap_positions(initial_state[:],0,1)一种解决方案是在第一次调用时克隆数组: right = swap_positions(initial_state[:],0,1)

probably a better solution for swap_positions would also be:可能对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

see also here也见这里

You don't really have "two identical list", you only have one list object that you're returning twice.您实际上并没有“两个相同的列表”,您只有一个要返回两次的列表 object。 To avoid modifying the original list and also two work with different lists, you should pass copies around.为了避免修改原始列表以及两个使用不同列表的工作,您应该传递副本。

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))

Side note 1: I have renamed your vairable list to states , otherwise it would shadow the built in list function旁注1:我已将您的可变list重命名为states ,否则它会隐藏内置列表 function

Side note 2: find_new_nodes did not work with the parameter, instead it used the global list.旁注 2: find_new_nodes不适用于参数,而是使用全局列表。 I changed that, too.我也改了。

Side note 3: There are different ways to create a copy of your (shallow) list.旁注 3:有不同的方法可以创建(浅)列表的副本。 I think list.copy() is the most verbose one.我认为list.copy()是最冗长的。 You could also use the copy module, use [:] or something else.您还可以使用复制模块,使用[:]或其他东西。

Output: Output:

([1, 0, 3, 4, 2, 5, 7, 8, 6], [4, 1, 3, 0, 2, 5, 7, 8, 6])

Ok, first of all, some thoughts...好的,首先,一些想法......

  1. Try to not use "list" as a variable, it's a Python identifier for "list" type.尽量不要使用“list”作为变量,它是“list”类型的 Python 标识符。 It seems that you are redefining the term.看来您正在重新定义该术语。

  2. Usually, it's a bad idea to use global vars such as loc_of_zero.通常,使用诸如 loc_of_zero 之类的全局变量是个坏主意。

About your problem:关于你的问题:

I believe that the problem is that you are getting a lot of references of the same variable.我相信问题在于你得到了很多相同变量的引用。 Try to avoid it.尽量避免它。 One idea:一个想法:

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python将多个列表组合成多个字典? - How to combine more than one list into more than one dicts by using python? 如何在不使用python中的多个if / else语句的情况下多次生成随机列表? - How do I generate a random list more than once without using multiple if/else statements in python? 如何从 Python 中的多个来源生成随机字符? - How to generate random character from more than one source in Python? 在Python清单中循环,每个循环使用多个项目 - Loop in Python List using more than one item per loop 如何在Python中将多个项目从一个列表移到另一个 - How can i move more than one item from one list to another in Python 如何将列表中的值存储到多个变量(在python django中) - how to store values in list to more than one variable ( in python django ) 如何使用列表理解等于python中的多个变量? - How to use list comprehention to equal to more than one variable in python? 如何在python中附加多个时间序列相关性列表? - how to append more than one list of time series correlation in python? 如何拆分包含多个列表的字符串? python - how to split a string containing more than one list? python 如何在python中将file.txt放入多个列表中 - How to put a file.txt into more than one list in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM