python基础--列表

高洛峰
Libérer: 2017-02-17 11:41:12
original
1204 Les gens l'ont consulté

列表

列表是最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作

1.定义列表

fruits = ['apple','banana','orange']
Copier après la connexion

2.通过下标访问列表中的元素,下标从0开始计数

>>> fruits[0]
'apple'
>>> fruits[2]
'orange'
>>> fruits[-1]
'orange'
>>> fruits[-2]
'banana'
Copier après la connexion

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']
Copier après la connexion

4.追加,append()

>>> fruits
['apple', 'banana', 'orange', 'peal', 'grape']
>>> fruits.append('newpeach')
>>> fruits
['apple', 'banana', 'orange', 'peal', 'grape', 'newpeach']
Copier après la connexion

5.插入元素,insert()

在下标1处插入一个西瓜(watermelon)

['apple', 'banana', 'orange', 'peal', 'grape', 'newpeach']
>>> fruits.insert(1,'watermelon')
>>> fruits
['apple', 'watermelon', 'banana', 'orange', 'peal', 'grape', 'newpeach']
Copier après la connexion

6.修改列表中的元素

将banana修改为樱桃cherry

>>> fruits
['apple', 'watermelon', 'banana', 'orange', 'peal', 'grape', 'newpeach']
>>> fruits[2]='cherry'
>>> fruits
['apple', 'watermelon', 'cherry', 'orange', 'peal', 'grape', 'newpeach']
Copier après la connexion

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']
Copier après la connexion

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']
Copier après la connexion

9.拷贝 copy()

['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
>>> fruits2 = fruits.copy()
>>> fruits2
['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
Copier après la connexion

10.统计 count()

 >>> fruits.count('apple')
 1
Copier après la connexion

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']
Copier après la connexion

12.获取下标 index()

['watermelon', 'radish', 'peal', 'grape', 'cucumber', 'cabbage', 'apple']
>>> fruits.index('apple')
6
# 只返回找到的第一个下标
Copier après la connexion


 更多python基础--列表相关文章请关注PHP中文网!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!