簡體   English   中英

在while循環中附加到列表而不覆蓋列表

[英]appending to a list in while loop without overwriting the list

如果隨機數大於元素關聯的分數(在另一個列表中),我編寫了一些代碼來更改列表中元素的 state。

我正在嘗試在 while 循環進行時創建一個狀態列表的列表,但是這會導致一個列表列表,其中所有列表都是代碼的最終 state。 我在 while 循環的每個條件中使用 append function 。

抱歉,如果我沒有很好地解釋這一點,我對編碼很陌生。 這是代碼,它可能比我能更好地解釋它。

import random 
random.seed(100)
f = random.randint(0,3)
m = random.randint(0,3)
s = random.randint(0,3)

seq = [0.2]*s + [0.5]*m + [0.8]*f
random.shuffle(seq)
state = [0]*len(seq)


record = []
count = 0

while count < len(seq):
    if count == 0 and random.random() < seq[count]:
        state[count]+=1
        print(state)
        record.append(state)
        count += 1
    elif count == 0 and random.random() > seq[count]:
        state[count]+=0
        print(state)
        record.append(state)
        count += 0
    elif count > 0  and random.random() < seq[count]:
        state[count]+=1
        state[count-1]-=1
        print(state)
        record.append(state)
        count += 1
    elif count > 0  and random.random() > seq[count]:
        state[count]+=0
        state[count-1]-=0
        print(state)
        record.append(state)
        count += 0

print(record)

print(state) function 顯示了我想要從 kernel 中獲得的列表。 然而,“記錄”列表只有程序的最終 state 的列表。

這是預期的清單和我得到的清單

expectedlist = [[1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 1]]

listiget = [[0, 0, 0, 0, 0, 0, 1],
            [0, 0, 0, 0, 0, 0, 1],
            [0, 0, 0, 0, 0, 0, 1],
            [0, 0, 0, 0, 0, 0, 1],
            [0, 0, 0, 0, 0, 0, 1],
            [0, 0, 0, 0, 0, 0, 1],
            [0, 0, 0, 0, 0, 0, 1],
            [0, 0, 0, 0, 0, 0, 1],
            [0, 0, 0, 0, 0, 0, 1],
            [0, 0, 0, 0, 0, 0, 1],
            [0, 0, 0, 0, 0, 0, 1],
            [0, 0, 0, 0, 0, 0, 1]]

我相信我沒有正確使用 append function。 誰能幫我嗎?

據我了解您的問題,我發現您的代碼邏輯是完全正確的,但是我認為您缺少這些東西:

  1. append 方法用於將項目添加到列表的末尾,因此請使用 add 方法。 在文檔中——https://docs.python.org/3/tutorial/datastructures.html

  2. 根據前面的答案 state 將繼續引用當前的 state 而不復制它,因此代碼如下:使用這個

     record.add(state.copy())

而不是 record.append(state)

state只引用同一個列表,所以當您將 append 它record到時,您只是將引用附加到同一個列表。 這意味着先前附加的state都將與 state 的當前state相同。 您應該在將state附加到record之前復制它,例如state[:]state.copy()或通過deepcopy

我想你需要

記錄.追加(狀態.復制())

嘗試將您的代碼更改為

import random 
random.seed(100)
f = random.randint(0,3)
m = random.randint(0,3)
s = random.randint(0,3)

seq = [0.2]*s + [0.5]*m + [0.8]*f
print(seq)
random.shuffle(seq)
state = [0]*len(seq)

record = []
count = 0

while count < len(seq):
    if count == 0 and random.random() < seq[count]:
        state[count]+=1
        record.append(state.copy())
        count += 1
    elif count == 0 and random.random() > seq[count]:
        state[count]+=0
        record.append(state.copy())
        count += 0
    elif count > 0  and random.random() < seq[count]:
        state[count]+=1
        state[count-1]-=1
        record.append(state.copy())
        count += 1
    elif count > 0  and random.random() > seq[count]:
        state[count]+=0
        state[count-1]-=0
        record.append(state.copy())
        count += 0

print(record)

我的 output

[[1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 1]]

希望這個例子能說明問題

a = []
b = [4,5,6]

a.append(b)
b[1] = 2
print(a)

這將 output

[[4, 2, 6]]

並不是

[[4, 5, 6]]

因為 append 正在維護對列表b的引用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM