Python 为什么这个列表返回空值?

Python 为什么这个列表返回空值?,python,Python,我正在为我的离散结构课程做一项作业,要求我返回一个列表,从输入集a创建所有可能的三元组(顺序无关)。我在程序中使用了一个函数,该函数创建了一个对列表,并固定在对中未包含的任何元素上,删除所有重复的三胞胎。结束集将所有元素返回为无。这可能是什么原因造成的 def listPairs(A): # type (list) -> list B = [] # Runs through every element in list A for x in A:

我正在为我的离散结构课程做一项作业,要求我返回一个列表,从输入集a创建所有可能的三元组(顺序无关)。我在程序中使用了一个函数,该函数创建了一个对列表,并固定在对中未包含的任何元素上,删除所有重复的三胞胎。结束集将所有元素返回为无。这可能是什么原因造成的

def listPairs(A):
    # type (list) -> list
    B = []
    # Runs through every element in list A
    for x in A:
        y = A.index(x) + 1
        # Makes a pair of every element in A after element x
        while y < len(A):
          B.append([x, A[y]])
          y += 1
    return B


print str(listPairs([1, 2, 3, 4, 5])) + " || Expected [[1, 2], [1, 3], [1, 4], [1, 5], [2,     3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5]"


def listTriples(A):
    # type (list) -> list
    B = listPairs(A)
    C = []
    for y in B:
        for x in A:
            if x not in y:
                C.append(y.append(x))
            if x in y:
                continue
    for z in C:
        if z in C:
            C.remove(z)
    return C


print str(listTriples([1, 2, 3, 4, 5])) + " || Expected [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], ...]"
def列表对(A):
#类型(列表)->列表
B=[]
#遍历列表中的每个元素
对于A中的x:
y=A.指数(x)+1
#在元素x之后生成每个元素的一对
当y列表
B=列表对(A)
C=[]
对于B中的y:
对于A中的x:
如果x不在y中:
C.append(y.append(x))
如果x在y中:
持续
对于C中的z:
如果z在C中:
C.移除(z)
返回C
打印str(列表三元组([1,2,3,4,5])+“| |预期[[1,2,3],[1,2,4],[1,2,5],[1,3,4],[1,3,5],[1,4,5],…]”

问题在于
C.append(y.append(x))
y.append(x)
的返回值为无-它通过追加
x
值而不返回来修改
y
。您将希望使用
y+[x]

您的第一个函数列表对()可以,但是第二个函数中的逻辑有一些问题。将每次迭代中的当前列表存储在临时变量中将有助于:

def listTriples(A):
B = listPairs(A)
C = []
for y in B:
    for x in A:
        if ((x not in y) and (x > y[len(y) - 1])):
            temp = y.copy()
            C.append(temp)
            y.remove(x)
        if x in y:
            continue

return C
如果要打印每次迭代的结果,代码应该如下所示:

def listTriples(A):
B = listPairs(A)
C = []
for y in B:
    for x in A:
        if ((x not in y) and (x > y[len(y) - 1])):
            print("x not in y")
            print("Y: ",y)
            print("X: ",x)
            print("y.append(x): ", y.append(x))
            print("new Y (temp):", y)
            print("------")
            temp = y.copy()
            C.append(temp)
            print("C: ", C)
            y.remove(x)
            print("Y:", y)
            print("------\n\n")
        if x in y:
            continue

return C
最终结果:

x not in y Y: [1, 2] X: 3 y.append(x): None new Y (temp): [1, 2, 3] ------ C: [[1, 2, 3]] Y: [1, 2] ------ x not in y Y: [1, 2] X: 4 y.append(x): None new Y (temp): [1, 2, 4] ------ C: [[1, 2, 3], [1, 2, 4]] Y: [1, 2] ------ x not in y Y: [1, 2] X: 5 y.append(x): None new Y (temp): [1, 2, 5] ------ C: [[1, 2, 3], [1, 2, 4], [1, 2, 5]] Y: [1, 2] ------ x not in y Y: [1, 3] X: 4 y.append(x): None new Y (temp): [1, 3, 4] ------ C: [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4]] Y: [1, 3] ------ x not in y Y: [1, 3] X: 5 y.append(x): None new Y (temp): [1, 3, 5] ------ C: [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5]] Y: [1, 3] ------ x not in y Y: [1, 4] X: 5 y.append(x): None new Y (temp): [1, 4, 5] ------ C: [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5]] Y: [1, 4] ------ x not in y Y: [2, 3] X: 4 y.append(x): None new Y (temp): [2, 3, 4] ------ C: [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4]] Y: [2, 3] ------ x not in y Y: [2, 3] X: 5 y.append(x): None new Y (temp): [2, 3, 5] ------ C: [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5]] Y: [2, 3] ------ x not in y Y: [2, 4] X: 5 y.append(x): None new Y (temp): [2, 4, 5] ------ C: [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5], [2, 4, 5]] Y: [2, 4] ------ x not in y Y: [3, 4] X: 5 y.append(x): None new Y (temp): [3, 4, 5] ------ C: [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5], [2, 4, 5], [3, 4, 5]] Y: [3, 4] ------ [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5], [2, 4, 5], [3, 4, 5]] || Expected [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], ...] x不在y中 Y:[1,2] X:3 y、 追加(x):无 新Y(临时):[1,2,3] ------ C:[[1,2,3]] Y:[1,2] ------ x不在y中 Y:[1,2] X:4 y、 追加(x):无 新Y(临时):[1,2,4] ------ C:[[1,2,3],[1,2,4]] Y:[1,2] ------ x不在y中 Y:[1,2] X:5 y、 追加(x):无 新Y(临时):[1,2,5] ------ C: [[1、2、3]、[1、2、4]、[1、2、5]] Y:[1,2] ------ x不在y中 Y:[1,3] X:4 y、 追加(x):无 新Y(临时):[1,3,4] ------ C:[[1,2,3],[1,2,4],[1,2,5],[1,3,4]] Y:[1,3] ------ x不在y中 Y:[1,3] X:5 y、 追加(x):无 新Y(临时):[1,3,5] ------ C:[[1,2,3],[1,2,4],[1,2,5],[1,3,4],[1,3,5]] Y:[1,3] ------ x不在y中 Y:[1,4] X:5 y、 追加(x):无 新Y(临时):[1,4,5] ------ C:[1,2,3],[1,2,4],[1,2,5],[1,3,4],[1,3,5],[1,4,5]] Y:[1,4] ------ x不在y中 Y:[2,3] X:4 y、 追加(x):无 新Y(临时):[2,3,4] ------ C:[1,2,3],[1,2,4],[1,2,5],[1,3,4],[1,3,5],[1,4,5],[2,3,4]] Y:[2,3] ------ x不在y中 Y:[2,3] X:5 y、 追加(x):无 新Y(临时):[2,3,5] ------ C:[1,2,3],[1,2,4],[1,2,5],[1,3,4],[1,3,5],[1,4,5],[2,3,4],[2,3,5]] Y:[2,3] ------ x不在y中 Y:[2,4] X:5 y、 追加(x):无 新Y(临时):[2,4,5] ------ C:[1,2,3],[1,2,4],[1,2,5],[1,3,4],[1,3,5],[1,4,5],[2,3,4],[2,3,5],[2,4,5]] Y:[2,4] ------ x不在y中 Y:[3,4] X:5 y、 追加(x):无 新Y(临时):[3,4,5] ------ C:[1,2,3],[1,2,4],[1,2,5],[1,3,4],[1,3,5],[1,4,5],[2,3,4],[2,3,5],[2,4,5],[3,4,5]] Y:[3,4] ------ [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5], [2, 4, 5], [3, 4, 5]] ||预期的[[1,2,3],[1,2,4],[1,2,5],[1,3,4],[1,3,5],[1,4,5],…]