本文實例講述了python實現給數組按片賦值的方法。分享給大家供大家參考。具體如下:
這段代碼可以直接給數組的第5-7個元素賦值
1
2
3
|
inventory = [ "sword" , "armor" , "shield" , "healing potion" ] inventory[ 4 : 6 ] = [ "orb of future telling" ] print inventory |
運行結果如下:
['sword', 'armor', 'shield', 'healing potion', 'orb of future telling']
希望本文所述對大家的Python程序設計有所幫助。