新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:Pythoninsert()
python 中的insert()函數(shù)有助于將給定元素插入到列表的指定索引中。

創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)整合營(yíng)銷推廣、網(wǎng)站重做改版、瓜州網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場(chǎng)景定制、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為瓜州等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
**list.insert(i, elem)** #where element may be string, number, object etc.
插入()參數(shù):
insert()函數(shù)接受兩個(gè)參數(shù)。如果元素被插入到指定的索引中,所有剩余的元素都會(huì)向右移動(dòng)。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 指數(shù) | 需要插入元素的索引 | 需要 |
| 元素 | 要插入列表的元素 | 需要 |
插入()返回值
這個(gè)方法不返回值。它通過(guò)添加元素來(lái)修改原始列表。如果給定的索引為零,元素將插入到第一個(gè)位置,如果索引為二,元素將插入到第二個(gè)元素之后,即第三個(gè)位置。
Python 中insert()方法的示例
示例 1:如何將元素插入列表
# alphabets list
alphabets = ['a', 'b', 'd', 'e']
# 'c' is inserted at index 2
# the position of 'c' will be 3rd
vowel.insert(2, 'c')
print('Updated List:', alphabets)
輸出:
Updated List: ['a', 'b', 'c', 'd', 'e']
示例 2:如何將元組作為元素插入列表
firs_list = [{1, 2}, [5, 6, 7]]
# ex tuple
ex_tuple = (3, 4)
# inserting a tuple to the list
firs_list.insert(1, ex_tuple)
print('Final List:', firs_list)
輸出:
Final List: [{1, 2}, (3, 4), [5, 6, 7]] 標(biāo)題名稱:創(chuàng)新互聯(lián)Python教程:Pythoninsert()
URL鏈接:http://www.dlmjj.cn/article/ccspcgp.html


咨詢
建站咨詢
