亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么此列表返回 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 關注
  • 96 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號