2017-10-05 66 views
1

我有4個生產者具有不同的屬性,如他們的新產品的價格,大小,客戶費率。我定義了4個代表它們的列表。更新netlogo中的列表中的項目的值

set att-price ((list p1-pr p2-pr p3-pr p4-pr));的4個生產者

set att-size ((list p1-sz p2-sz p3-sz p4-sz)) 



set att-rates ((list p1-rt p2-rt p3-rt p4-rt)) 

隨着時間的推移,價格獲取更新的所有產品的價格,所以我定義這個要做到這一點:

set (item 0 att-price) (item 0 att-price) * 0.20;生產者的產品價格的變化

set (item 1 att-price) (item 1 att-price) * 0.08 

set (item 3 att-price) (item 3 att-price) * 0.43 

但它有一個錯誤說「這不是你可以」設置「上」!

那麼我該如何更新這些項目呢? 謝謝

回答

2

您使用replace-item爲此。例如:

set att-price replace-item 0 att-price (0.2 * item 0 att-price) 

也就是說,而不是設置列表中的項目,我們正在做替換爲項目的新列表,然後我們的名單變量設置到該項目。

如果要一次替換所有項目,可以使用map。例如,看起來您可能有一個價格變化的價格比率列表:

let ratios [ 0.2 1.0 0.08 0.43 ] 
set att-price (map [ [ price ratio ] -> price * ratio ] att-price ratios) 
+0

謝謝。我用'set att-price replace-item 0(0.2 * item 0 att-price)att-price'來代替'set(item 0 att-price)(item 0 att-price)* 0.20',但是發現一個錯誤說「替換項目」預計這個輸入是一個字符串或列表,但得到一個數字。 – user710

+1

@ user710新值最後一位: https://ccl.northwestern.edu/netlogo/docs/dictionary.html#replace-item – Alan

+0

非常感謝Alan。是的,通過設置att-price replace-item 0 att-price(0.2 * item 0 att-price)',它工作得很好:) – user710