Home > Backend Development > Python Tutorial > python basics--list

python basics--list

高洛峰
Release: 2017-02-17 11:41:12
Original
1275 people have browsed it

List

List is one of the most commonly used data types. Lists can achieve the most convenient storage, modification and other operations on data

1. Define the list

fruits = ['apple','banana','orange']
Copy after login

2. Access elements in the list through subscripts, and the subscripts start counting from 0

>>> fruits[0]
'apple'
>>> fruits[2]
'orange'
>>> fruits[-1]
'orange'
>>> fruits[-2]
'banana'
Copy after login

3. Slicing

>>> 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']
Copy after login

4. Append, append()

>>> fruits
['apple', 'banana', 'orange', 'peal', 'grape']
>>> fruits.append('newpeach')
>>> fruits
['apple', 'banana', 'orange', 'peal', 'grape', 'newpeach']
Copy after login

5 .Insert element, insert()

Insert a watermelon at subscript 1

['apple', 'banana', 'orange', 'peal', 'grape', 'newpeach']
>>> fruits.insert(1,'watermelon')
>>> fruits
['apple', 'watermelon', 'banana', 'orange', 'peal', 'grape', 'newpeach']
Copy after login

6. Modify the elements in the list

Change banana to cherry

>>> fruits
['apple', 'watermelon', 'banana', 'orange', 'peal', 'grape', 'newpeach']
>>> fruits[2]='cherry'
>>> fruits
['apple', 'watermelon', 'cherry', 'orange', 'peal', 'grape', 'newpeach']
Copy after login

7. Delete

pop() will return the last element after deleting it by default

>>> 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']
Copy after login

8. Extend 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']
Copy after login

9. Copy copy()

['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
>>> fruits2 = fruits.copy()
>>> fruits2
['apple', 'watermelon', 'peal', 'grape', 'radish', 'cabbage', 'cucumber']
Copy after login

10. Statistics count()

 >>> fruits.count('apple')
 1
Copy after login

11. Sort sort() and flip 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']
Copy after login

12. Get the subscript index ()

['watermelon', 'radish', 'peal', 'grape', 'cucumber', 'cabbage', 'apple']
>>> fruits.index('apple')
6
# 只返回找到的第一个下标
Copy after login


For more python basics-list related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template