共計 436 個字符,預計需要花費 2 分鐘才能閱讀完成。
Python 中的列表(list)是一種有序的可變序列,可以存儲多個元素。insert() 是列表的一個內置方法,用于在指定位置插入一個元素。
insert() 方法的語法如下:
list.insert(index, element)
其中,index 為插入的位置,element 為要插入的元素。插入后,原來的元素將向后移動一個位置。
下面是一個使用 insert() 方法的示例:
fruits = ["apple", "banana", "cherry"]
fruits.insert(1, "orange")
print(fruits)
輸出結果為:
["apple", "orange", "banana", "cherry"]
在上面的示例中,使用 insert() 方法在索引為 1 的位置插入了一個元素 "orange"。插入后,原來的元素 "banana" 及其后面的元素都向后移動了一個位置。最終輸出的列表為 [“apple”,“orange”,“banana”,“cherry”]。
丸趣 TV 網 – 提供最優質的資源集合!
正文完