作者DrRd (就这样吧)
看板Python
标题[问题] 改变list中多个元素的值
时间Sat Sep 26 22:52:45 2015
请问版上诸位大神,请问有没有除了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://webptt.com/cn.aspx?n=bbs/Python/M.1443279167.A.2BB.html
1F:→ alibuda174: list1[2], list1[4], list1[7] = [1] * 3 09/26 23:23
2F:→ darkgerm: list1[2] = list1[4] = list1[7] = 1 09/26 23:25
3F:→ Fungshui: numpy.array可以用list来当index 09/26 23:28
4F:→ DrRd: 感谢诸位 09/26 23:45