[問題] 改變list中多個元素的值

看板Python作者 (就這樣吧)時間10年前 (2015/09/26 22:52), 編輯推噓0(004)
留言4則, 4人參與, 最新討論串1/1
請問版上諸位大神,請問有沒有除了for loop以外改變一個list中多個元素的值的方法 例如: list1 = [0]*10 我想要把當中第2、4、7個值改為1 但是python裡好像不能直接指定多個index來改變特定位置的值 請問除了用for來一個一個改值之外,有沒有其他的方式? python的版本為2.7和3.4 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.230.223.224 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1443279167.A.2BB.html

09/26 23:23, , 1F
list1[2], list1[4], list1[7] = [1] * 3
09/26 23:23, 1F

09/26 23:25, , 2F
list1[2] = list1[4] = list1[7] = 1
09/26 23:25, 2F

09/26 23:28, , 3F
numpy.array可以用list來當index
09/26 23:28, 3F

09/26 23:45, , 4F
感謝諸位
09/26 23:45, 4F
文章代碼(AID): #1M1h4_Ax (Python)
文章代碼(AID): #1M1h4_Ax (Python)