Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何在函数中调用函数,并将列表作为两个函数的参数插入_Python_Function - Fatal编程技术网

Python:如何在函数中调用函数,并将列表作为两个函数的参数插入

Python:如何在函数中调用函数,并将列表作为两个函数的参数插入,python,function,Python,Function,这是一段伪代码,只是为了简单地解释我要做的事情 x = [1, 2, 3, 4, 5, 6] def func_1(x): a = ... b = ... if a == 2 and b == 5: do this to the list... def func_2(x): if a == 6 and b == 2: do this to the list... 这是一段伪代码,只是为了简单地解释我要做的事情 我希望能够 a

这是一段伪代码,只是为了简单地解释我要做的事情

x = [1, 2, 3, 4, 5, 6]

def func_1(x):
    a = ...
    b = ...
    if a == 2 and b == 5:
        do this to the list...


def func_2(x):
    if a == 6 and b == 2:
        do this to the list...
这是一段伪代码,只是为了简单地解释我要做的事情

我希望能够

a) 在一个函数中运行两个单独的ifs,但要分别处理它们

b) 具有单独的函数,但具有从第一个函数到另一个函数的a和b值(或第一个函数末尾的调整列表作为第二个函数的参数)

当我打电话

x = func_1(x)
x = func_2(x)
上面说

<function func_1 at 0x244384>

当我将其全部放在一个函数中时,它也会这样做:(

下面是正在工作(或在本例中不工作)的代码。警告:代码很长。可能有更简单的方法来实现我在Deploy_Destroyer_2中想要的功能,但除非这是问题所在,否则我不会为此提供建议

def InitBoards():
    Player = [[1]*(6 if x<6 else 12) for x in range(12)]
    Opponent = [[0]*(6 if x<6 else 12) for x in range(12)]

    return Player,Opponent

def Deploy_Destroyer_1(Player):
    rand_col_1 = randint(0,11)
    if rand_col_1 < 6:
        rand_row_1 = randint(0,11)
        return rand_col_1
        return rand_col_2
    else:
        rand_row_1 = randint(6,11)
        return rand_col_1
        return rand_row_1
    if Player[rand_row_1][rand_col_1] == 2:
        Deploy_Destroyer_1(Player)


def Deploy_Destroyer_2(Player):
    if rand_col_1 == 5 and rand_row_1 == 6:
        #can be 1, 2, 3 or 4... in that order below
        rand_position_1 = randint(1,4)
        if rand_position_1 == 1:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 4:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif rand_col_1 in range(1,4) and rand_row_1 in range(1,10):
        #can be any 1, 2, 3 or 4... in that order below
        rand_position_1 = randint(1,4)
        if rand_position_1 == 1:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 4:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif rand_col_1 in range(5,10) and rand_row_1 in range(7,10):
        #can be any 1, 2, 3 or 4... in that order below
        rand_position_1 = randint(1,4)
        if rand_position_1 == 1:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 4:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif rand_col_1 == 0 and rand_row_1 == 0:
        #can be any 1, 2, 3 or 4... in that order below
        rand_position_1 = randint(1,4)
        if rand_position_1 == 1:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 4:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif (rand_col_1 == 5 and rand_row_1 == 0) or (rand_col_1 == 11 and rand_row_1 ==6):
        #can be one or four
        #check brackets and booleans here
        rand_position_1 = randint(1,2)
        if rand_position_1 == 1: #position 1
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2: #position 4
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif rand_col_1 == 0 and rand_row_1 == 11:
        #can be 2 or 3
        rand_position_1 = randint(2,3)
        if rand_position_1 == 2: #position 2
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3: #position 3
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
    elif rand_col_1 == 11 and rand_row_1 == 11:
        #can be 2 or 4
        rand_position_1 = randint(1,2)
        if rand_position_1 == 1: #position 2
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 2: #position 4
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif (rand_row_1 == 0 and rand_col_1 in range(1,4)) or (rand_row_1 == 6 and rand_col_1 in range(6,10)):
        #can be 1, 3 or 4
        #check brackets and booleans here
        rand_position_1 = randint(1,3)
        if rand_position_1 == 1: #position 1
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2: #position 3
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 3: #position 4
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif (rand_col_1 == 5 and rand_row_1 in range(1,5)) or (rand_col_1 == 11 and rand_row_1 in range(7,10)):
        #can be 1, 2 or 4
        #check brackets and booleans here
        rand_position_1 = randint(1,3)
        if rand_position_1 == 1: #position 1
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2: #position 2
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3: #position 4
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif rand_col_1 == 0 and rand_row_1 in range(1,10):
        #can be 1, 2 or 3... in that order below
        rand_position_1 = randint(1,3)
        if rand_position_1 == 1:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
    elif rand_col_1 in range(1,10) and rand_row_1 == 11:
        #can be 2, 3 or 4
        rand_position_1 = randint(1,3)
        if rand_position_1 == 2: #position 2
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3: #position 3
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 4: #position 4
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    return Player


Player, Opponent = InitBoards()
Player = Deploy_Destroyer_1
Player = Deploy_Destroyer_2

print Player
print Opponent
def InitBoards():

Player=[[1]*(6如果x列表是可变对象,那么答案是“就这么做吧”。它会像魔术一样工作(而在大多数情况下,它是一个陷阱)

我应该看更多的细节,为了让a和b也进入第二个函数的范围,许多可能的解决方案之一是:

def f1(l):
    …
    return l, a, b

def f2(l, a, b):
    …

f2(*f1(x))

如果我们只讨论f1、f2、a和b,而不是问题域,则无法提出合适的解决方案。

这有什么问题:

x = [1, 2, 3, 4, 5, 6]

def f(x):
    a = ...
    b = ...
    if a == 2 and b == 5:
        # do this to the list...
        pass

    if a == 6 and b == 2:
        # do this to the list...
        pass

    return x

x = f(x)
?


(另一方面,上面是合法的Python3.x,因为
..
是一个内置对象!)

你说的“分别对待它们”是什么意思?为什么你不能把它们都放在一个函数中?对于第二个问题,你需要调用函数!
x=func\u 1()
。已经编辑了这个问题,抱歉,我不知道如何解释它,除非有人问他们还需要从mepost工作代码(而不是psudo代码)中获得什么,并且在编辑过程中,您的函数似乎没有显式返回,这意味着它们不返回任何值,因此您将
None
分配给
x
这个答案,这是在吹走您的列表与问题一样神秘。我尝试了这个方法,但它只是给了我一个错误,即当Deploy_Destroyer_2请求三个@jhermann时,它只提供了一个参数,然后您忽略了“*”。
不是python 2.7中切片表达式之外的内置对象。我不确定这是否有意义(我可能错了,看看我给出的工作代码和ifs的条件。我想让它检查一组“先是if,然后是下一组”)@user2387370