为了账号安全,请及时绑定邮箱和手机立即绑定

为什么此列表返回 Null 值?

为什么此列表返回 Null 值?

九州编程 2022-08-16 15:38:20
我正在为我的离散结构课程做一个作业,要求我从输入集A返回一个列表,该列表创建所有可能的三元组(顺序无关紧要)。我正在使用我之前在程序中使用的一个函数,该函数创建一个对列表并附加在对中尚不存在的任何元素,删除所有重复的三元组。结束集将所有元素返回为 None。可能导致这种情况的原因是什么?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 Bprint 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 Cprint 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], ...]"
查看完整描述

2 回答

?
慕尼黑的夜晚无繁华

TA贡献1864条经验 获得超6个赞

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


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], ...]



查看完整回答
反对 回复 2022-08-16
?
偶然的你

TA贡献1841条经验 获得超3个赞

问题出在 。的返回值为 none - 它通过附加值而不返回值进行修改。您将需要改用。C.append(y.append(x))y.append(x)yxy + [x]



查看完整回答
反对 回复 2022-08-16
  • 2 回答
  • 0 关注
  • 140 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号