python基礎--列表

高洛峰
發布: 2017-02-17 11:41:12
原創
1204 人瀏覽過

列表

列表是最常用的資料類型之一,透過列表可以對資料實現最方便的儲存、修改等操作

1.定義列表

fruits = ['apple','banana','orange']
登入後複製

2.透過下標存取列表中的元素,下標從0開始計數

>>> fruits[0]
'apple'
>>> fruits[2]
'orange'
>>> fruits[-1]
'orange'
>>> fruits[-2]
'banana'
登入後複製

3.切片

>>> fruits = ['apple','banana','orange','peal','grape']
>>> fruits[1:4]    #取下标1到下标4之间的数,包括1但不包括4
['banana', 'orange', 'peal']
>>> fruits[1:-1]   #取下标1至-1之间的数,不包括-1
['banana', 'orange', 'peal']
>>> fruits[0:3]    #从头开始取,不包括3
['apple', 'banana', 'orange']
>>> fruits[:3]     #和上句一样
['apple', 'banana', 'orange']
>>> fruits[3:]     #从下标3到最后,到末尾只能这样取
['peal', 'grape']
>>> fruits[0::2]   #从头开始,步长为2,即隔一个取一个
['apple', 'orange', 'grape']
>>> fruits[::2]    #和上句一iy
['apple', 'orange', 'grape']
登入後複製

4.追加,append()

>>> fruits
['apple', 'banana', 'orange', 'peal', 'grape']
>>> fruits.append('newpeach')
>>> fruits
['apple', 'banana', 'orange', 'peal', 'grape', 'newpeach']
登入後複製

5.插入元素,insert()

在下標1處插入一個西瓜(watermelon)

['apple', 'banana', 'orange', 'peal', 'grape', 'newpeach']
>>> fruits.insert(1,'watermelon')
>>> fruits
['apple', 'watermelon', 'banana', 'orange', 'peal', 'grape', 'newpeach']
登入後複製
列表

6.中的元素

將banana修改為櫻桃cherry

>>> fruits
['apple', 'watermelon', 'banana', 'orange', 'peal', 'grape', 'newpeach']
>>> fruits[2]='cherry'
>>> fruits
['apple', 'watermelon', 'cherry', 'orange', 'peal', 'grape', 'newpeach']
登入後複製

7.刪除

pop()在預設刪除最後一個元素後,會傳回該元素

>>> fruits
['apple', 'watermelon', 'cherry', 'orange', 'peal', 'grape', 'newpeach']
>>> del fruits[2]    #删除第二个元素
>>> fruits
['apple', 'watermelon', 'orange', 'peal', 'grape', 'newpeach']
>>> fruits.remove('orange')     #删除指定的元素
>>> fruits
['apple', 'watermelon', 'peal', 'grape', 'newpeach']
>>> fruits.pop()    #删除最后一个元素
'newpeach'
>>> fruits
['apple', 'watermelon', 'peal', 'grape']
登入後複製

8.擴充extend()

 >>> fruits
['apple', 'watermelon', 'peal', 'grape']
>>> vegetable = ['radish','cabbage','cucumber']
>>> fruits
['apple', 'watermelon', 'peal', 'grape']
>>> vegetable
['radish', 'cabbage', 'cucumber']
>>> fruits.extend(vegetable)
>>> fruits
['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
登入後複製

9.拷貝()

['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
>>> fruits2 = fruits.copy()
>>> fruits2
['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
登入後複製

10.統計count()

 >>> fruits.count('apple')
 1
登入後複製

11.排序sort() 和翻轉reverse()

>>> fruits
['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
>>> fruits.sort()
>>> fruits
['apple', 'cabbage', 'cucumber', 'grape', 'peal', 'radish', 'watermelon']
>>> fruits.reverse()
>>> fruits
['watermelon', 'radish', 'peal', 'grape', 'cucumber', 'cabbage', 'apple']
登入後複製

12.取得下標index()

['watermelon', 'radish', 'peal', 'grape', 'cucumber', 'cabbage', 'apple']
>>> fruits.index('apple')
6
# 只返回找到的第一个下标
登入後複製


12.取得下標index()

rrreee

🎜🎜12.取得下標index()🎜rrreee🎜🎜🎜列表文章請關注PHP中文網! 🎜
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!