Python-列表方法

时间:2020-02-23 14:42:55  来源:igfitidea点击:

在本教程中,我们将学习Python中的列表方法。

在上一个教程Python-List中,我们了解了列表。
随时检查一下。

快速回顾

  • 列表是项目的有序序列。

  • 我们使用[]在Python中创建列表。

  • 列表中的项目被索引。

  • 索引从0开始。

Python append

我们使用append方法将传递的对象附加到列表中。

在下面的Python程序中,我们将对象追加到列表中。

# list
myList = []

print("Starting", myList)

# append integer
myList.append(1)

# append float
myList.append(3.14)

# append string
myList.append("theitroad")

# append complex number
myList.append((2+3j))

# append boolean
myList.append(True)

# append list
myList.append([1,2,3])

# output
print("Final", myList)

上面的Python代码将使用以下输出。

Starting []
Final [1, 3.14, 'theitroad', (2+3j), True, [1, 2, 3]]

Python clear

我们使用" clear"方法从给定列表中删除所有项目。

在下面的Python代码中,我们从列表中删除了所有项目。

# list
myList = [1, 2, 3]

print("before", myList)

# clear
myList.clear()

# output
print("after", myList)

上面的代码将给出以下输出。

before [1, 2, 3]
after []

Python copy

我们使用copy方法来复制现有列表。

在下面的Python程序中,我们将创建给定列表的副本。

# list
myList = [1, 2, 3]

# copy
newList = myList.copy()

# output
print(newList)

Python count

我们使用count方法来查找给定对象出现在给定列表中的总次数。

在下面的Python程序中,我们正在检查给定项目出现在列表中的次数。

# list
myList = [1, 2, 3, 2, 4, "Hello", "Happy", "happy", "hello"]

# count
print(myList.count("unknown"))  # 0
print(myList.count("Hello"))    # 1
print(myList.count("happy"))    # 1
print(myList.count(2))          # 2

Python extend

我们使用extend方法将一系列项目附加到现有列表中。

它不返回任何值。
它只是追加序列。

在以下Python程序中,我们将fruitList追加到vegList

# list
vegList = ["Tomato", "Onion", "Potato", "Broccoli", "Lettuce"]

print("before", vegList)

# second list
fruitList = ["Apple", "Mango", "Orange", "Guava", "Watermelon"]

# extend
vegList.extend(fruitList)

# output
print("after", vegList)

对于上面的代码,我们得到以下输出。

before ['Tomato', 'Onion', 'Potato', 'Broccoli', 'Lettuce']
after ['Tomato', 'Onion', 'Potato', 'Broccoli', 'Lettuce', 'Apple', 'Mango', 'Orange', 'Guava', 'Watermelon']

Python index

我们使用index方法来获取给定值在列表中的首次出现。

成功后,此方法将返回项目的索引。
如果找不到该项目,则将给出异常。

在下面的Python程序中,我们在给定列表中找到6。

# list
myList = [1, 2, 5, 6, 4, 3, 6, 7, 9, 8]

# index
print(myList.index(6))     # 3

上面的代码将给我们3,因为数字6的第一个出现在索引3处。

注意!即使我们有6次出现在列表中两次,但是index方法将在第一次遇到6时返回索引。

插入

我们使用"插入"方法在列表中的特定索引处插入对象。

这是我们可以在列表中插入项目的可能方法。

  • 在开始处插入
  • 在末尾插入
  • 在开始和结束之间插入

在开始处插入

以下是在列表的开头(第0个索引)插入项目的语法。

list.insert(0, item)

其中," list"是保存列表的变量,而" item"是要从列表的开头(第0个索引)插入的项目。

注意!出现在特定索引处的项目不会被删除,而是会为新项目腾出空间。

在下面的Python程序中,我们在索引0(列表的开始)处插入字符串" Happy"。

# list
myList = [1, 2, 3, 4, 5]

print("before", myList)

# insert
myList.insert(0, "Hello")

# output
print("after", myList)

上面的代码将使用以下输出。

before [1, 2, 3, 4, 5]
after ['Hello', 1, 2, 3, 4, 5]

注意!在上面的输出中,我们可以看到项目1从索引0移动,并且其位置由插入的项目" Hello"占据。

在末尾插入

以下是在列表末尾插入项目的语法。

list.insert(len(list), item)

我们使用len方法获取列表中的项目总数。

在下面的Python程序中,我们在列表的末尾插入字符串" Happy"。

# list
myList = [1, 2, 3, 4, 5]

print("before", myList)

# insert
myList.insert(len(myList), "Hello")

# output
print("after", myList)

上面的代码将使用以下输出。

before [1, 2, 3, 4, 5]
after [1, 2, 3, 4, 5, 'Hello']

如果我们将插入索引的值设置为大于列表中总项的值,则新项将插入到末尾。

在下面的Python程序中,新项目插入到列表的末尾。

# list
myList = [1, 2, 3, 4, 5]

print("before", myList)

# insert
myList.insert(100, "Hello")

# output
print("after", myList)

我们将获得以下输出。

before [1, 2, 3, 4, 5]
after [1, 2, 3, 4, 5, 'Hello']

在开始和结束之间插入

如果我们提供介于列表的开始和结束索引之间的任何索引,则新项目将插入列表中的某个位置。

在下面的Python程序中,我们列出了5个数字,并将字符串" Hello"插入两者之间。

# list
myList = [1, 2, 3, 4, 5]

print("before", myList)

# insert
myList.insert(3, "Hello")

# output
print("after", myList)

上面的Python程序将为我们提供以下输出。

before [1, 2, 3, 4, 5]
after [1, 2, 3, 'Hello', 4, 5]

Python pop

我们使用pop方法从列表中弹出项目。

此方法返回弹出的项目。

如果我们不传递任何索引,那么pop方法将从列表中弹出最后一项。

在下面的Python程序中,我们从给定列表中弹出最后一项。

# list
myList = [1, 2, 3, 4, 5]

print("before pop:", myList)

# pop
x = myList.pop()

# output
print("Pop", x)
print("after pop:", myList)

我们得到以下输出。

before pop: [1, 2, 3, 4, 5]
Pop 5
after pop: [1, 2, 3, 4]

我们还可以使用以下语法从特定索引弹出项目。

list.pop(index)

在下面的Python程序中,我们弹出索引3的项。

# list
myList = [1, 2, 3, 4, 5]

print("before pop:", myList)

# pop
x = myList.pop(3)

# output
print("Pop", x)
print("after pop:", myList)

上面的Python程序将为我们提供以下输出。

before pop: [1, 2, 3, 4, 5]
Pop 4
after pop: [1, 2, 3, 5]

Python 删除

我们使用remove方法从列表中删除一个项目。

语法

list.remove(item)

在下面的Python程序中,我们从列表中删除3。

# list
myList = [1, 2, 3, 4, 5]

print("before", myList)

# remove
myList.remove(3)

# output
print("after", myList)
before [1, 2, 3, 4, 5]
after [1, 2, 4, 5]

Python reverse

我们使用reverse方法来反转列表中项目的顺序。

在下面的Python程序中,我们将反转列表中各项的顺序。

# list
myList = [1, 2, 3, 4, 5]

print("before", myList)

# reverse
myList.reverse()

# output
print("after", myList)

我们将获得以下输出。

before [1, 2, 3, 4, 5]
after [5, 4, 3, 2, 1]

Python sort

我们使用" sort"方法对列表中的项目进行升序和降序排序。

默认情况下,项目按升序排序。

在下面的Python程序中,我们以升序对列表中的项目进行排序。

# list
myList = [1, 10, 9, 6, 3, 8, 2, 5, 4, 7]

print("before", myList)

# sort
myList.sort()

# output
print("after", myList)

我们将获得以下输出。

before [1, 10, 9, 6, 3, 8, 2, 5, 4, 7]
after [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

为了按降序对项目进行排序,我们使用以下语法。

list.sort(reverse=True)

在下面的Python程序中,我们以降序对列表中的项目进行排序。

# list
myList = [1, 10, 9, 6, 3, 8, 2, 5, 4, 7]

print("before", myList)

# sort
myList.sort(reverse=True)

# output
print("after", myList)

上面的Python程序将使用以下输出。

before [1, 10, 9, 6, 3, 8, 2, 5, 4, 7]
after [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]