網站首頁 學習教育 IT科技 金融知識 旅遊規劃 生活小知識 家鄉美食 養生小知識 健身運動 美容百科 遊戲知識 綜合知識
當前位置:趣知科普吧 > IT科技 > 

append在python中是什麼意思

欄目: IT科技 / 發佈於: / 人氣:1.85W

python中append是怎樣的呢?下面就讓我們一起來了解一下吧:

append是屬於python中的一個函數,它主要是用來在列表末尾添加新的對象。

語法格式

list.append(obj)

參數:

list     列表對象
obj    添加到列表末尾的對象

返回值:

append()函數是沒有返回值的,但是其會修改原本的列表。

append在python中是什麼意思

參考範例:

1、

輸入命令:

#!/usr/bin/python

#Filename:append.py

a=[-1,3,'aa',85,90,'dasd']

a.append('add')

print a

輸出結果:

[-1, 3, 'aa', 85, 90, 'dasd', 'add']

append在python中是什麼意思 第2張

2、

append函數可以在數組後加上相應的元素,輸入命令:

a=[1,2,3]

a.append(5)

執行結果[1, 2, 3, 5]:

a=[1,2,3]

a.append([5])

此時的執行結果是 [1, 2, 3, [5]]

結果不再是一個數組,而是list。

運用append生成多維數組,輸入命令:

import numpy as np

a=[] 

for i in range(5): 

    a.append([])

    for j in range(5): 

        a[i].append(i)

輸出結果:

[[0, 0, 0, 0, 0],

 [1, 1, 1, 1, 1],

 [2, 2, 2, 2, 2],

 [3, 3, 3, 3, 3],

 [4, 4, 4, 4, 4]]