Python|列表

列表(list)是由⼀系列按特定顺序排列的元素组成。在 Python 中,用方括号[]表⽰列表,⽤逗号分隔其中的元素。

一个:索引

  • 示例
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # 输出:apple
print(fruits[1]) # 输出:banana
print(fruits[2]) # 输出:cherry

一段:切片

  • 示例
fruits = ["apple", "banana", "cherry"]
print(fruits[0:2]) # 输出:['apple', 'banana']

遍历 :for 循环

  • 示例
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
# 输出:apple banana cherry

append(x)

append()方法用于在列表的末尾添加一个新元素。

  • 示例
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits) # 输出: ['apple', 'banana', 'cherry', 'orange']

extend(iterable)

extend()方法用于在列表的末尾一次性追加另一个可迭代对象(列表、元组、字符串、集合、字典等)中的所有元素,它会将可迭代对象中的元素逐个添加到列表中。

  • 示例
fruits = ["apple", "banana", "cherry"]
fruits.insert(1,"orange")
print(fruits) # 输出: ['apple', 'orange', 'banana', 'cherry']

insert(i,x)

insert()方法用于在指定的索引位置插入一个新元素。

  • 示例
fruits = ["apple", "banana", "cherry"]
fruits.extend("pear")
print(fruits) # 输出: ['apple', 'banana', 'cherry', 'p', 'e', 'a', 'r']

del 语句

del用于删除列表中指定位置的元素,也可以删除整个列表。

  • 示例
fruits = ["apple", "banana", "cherry"]
del fruits[0]
print(fruits) # 输出: ['banana', 'cherry']

pop([i])

pop()方法用于移除列表中的一个元素(默认是最后一个元素),并且返回该元素的值。这个方法会永久改变列表的内容,即列表的长度会减 1。

  • 示例
fruits = ["apple", "banana", "cherry"]
removed_fruit = fruits.pop()
print(removed_fruit) # 输出: cherry
print(fruits) # 输出: ['apple', 'banana']

fruits = ["apple", "banana", "cherry"]
removed_fruit = fruits.pop(1)
print(removed_fruit) # 输出: banana
print(fruits) # 输出: ['apple', 'cherry']

remove(x)

remove()方法用于移除列表中第一个匹配的指定元素。

  • 示例
fruits = ["apple", "banana", "cherry"]
fruits.remove("apple")
print(fruits) # 输出: ['banana', 'cherry']

clear()

clear()方法移除列表中的所有项,等同于删除所有元素。

  • 示例
fruits = ['apple', 'banana', 'cherry']
fruits.clear()
print(fruits) # 输出: []

## 改

### 索引

– 示例

“`python
fruits = [“apple”, “banana”, “cherry”]
fruits[1] = “orange”
print(fruits) # 输出: [‘apple’, ‘orange’, ‘cherry’]
“`

### 切片

– 示例

“`python
fruits = [“apple”, “banana”, “cherry”]
fruits[0:2] = “pear”
print(fruits) # 输出: [‘p’, ‘e’, ‘a’, ‘r’, ‘cherry’]
“`

– 示例

“`python
fruits = [“apple”, “banana”, “cherry”]
fruits[0] = “orange”
print(fruits) # 输出: [‘orange’, ‘banana’, ‘cherry’]
“`

## 排序

### sort()

`sort()`⽅法用于永久修改列表元素的排列顺序。

– 示例

“`python
fruits = [“pear”,”banana”, “apple”, “orange”]
# 正向排序
fruits.sort()
print(fruits) # 输出: [‘apple’, ‘banana’, ‘orange’, ‘pear’]
# 逆向排序
fruits.sort(reverse=True)
print(fruits) # 输出: [‘pear’, ‘orange’, ‘banana’, ‘apple’]
“`

### sorted()

`sorted()`函数可以对列表进行临时排序,返回一个新的列表,不修改原始列表。

– 示例

“`python
fruits = [“banana”, “apple”, “cherry”]
sorted_fruits = sorted(fruits)
print(fruits) # 输出: [‘banana’, ‘apple’, ‘cherry’]
print(sorted_fruits) # 输出: [‘apple’, ‘banana’, ‘cherry’]
“`

### reverse()

`reverse()`方法可以反转列表元素的排列顺序,会永久修改列表元素的排列顺序。

– 示例

“`python
fruits = [“banana”, “apple”, “cherry”]
fruits.reverse()
print(fruits) # 输出: [‘cherry’, ‘apple’, ‘banana’]
“`

## 长度

### len()

`len()`函数可快速获悉列表的⻓度。Python 在计算列表元素数时从`1`开始。

– 示例

“`python
fruits = [“banana”, “apple”, “cherry”]
len(fruits) # 输出:3
print(f”这个列表的长度为:{len(fruits)}”) # 输出: 这个列表的长度为:3
“`

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容