數(shù)組在 Python 中的工作原理 – 數(shù)組方法與代碼示例解釋
在本教程中,您將了解 Python 中的數(shù)組是什么。您還將學(xué)習(xí)一些向現(xiàn)有數(shù)組添加元素的可能方法。
在 Python 中,不需要對(duì)數(shù)組使用特定的數(shù)據(jù)類(lèi)型。您可以簡(jiǎn)單地使用包含數(shù)組所有屬性的列表。
如果要?jiǎng)?chuàng)建一個(gè)同時(shí)包含整數(shù)和浮點(diǎn)數(shù)的數(shù)組,可以使用 Python 的數(shù)組模塊。
什么是數(shù)組?
數(shù)組是一種獨(dú)特的變量類(lèi)型,能夠一次存儲(chǔ)多個(gè)值。
假設(shè)您有一個(gè)對(duì)象列表,例如國(guó)家/地區(qū)名稱(chēng)。您可以將國(guó)家/地區(qū)存儲(chǔ)在單獨(dú)的變量中,如下所示:
Country1 = "Germany";Country2 = "France";Country3 = "Denmark";
但是假設(shè)您想搜索所有國(guó)家/地區(qū)以發(fā)現(xiàn)某個(gè)國(guó)家/地區(qū)。如果您有 200 個(gè)國(guó)家/地區(qū)而不是只有 3 個(gè)國(guó)家/地區(qū)會(huì)怎樣?
另一種方法是將所有這些值存儲(chǔ)在數(shù)組中。
數(shù)組對(duì)于存儲(chǔ)和操作相同數(shù)據(jù)類(lèi)型的多個(gè)值很有用。它們的作用類(lèi)似于可以保存值集合的變量,所有這些值都是相同的類(lèi)型。這些值一起存儲(chǔ)在邊界內(nèi)存中。
Python 數(shù)組方法
使用列表和數(shù)組時(shí),可以使用各種內(nèi)置的 Python 方法。以下是您可以在 Python 中的數(shù)組和列表上使用的方法。
方法Append()
如果要將項(xiàng)目添加到列表的末尾,可以使用 append 方法。
例:
fruits = ['apple', 'banana', 'cherry']fruits.append('orange')print(fruits) ?# Output: ['apple', 'banana', 'cherry', 'orange']
The?method is used to add elements to the end of a list. In this case, 'orange' is appended to the?list, resulting in the list containing four elements: 'apple', 'banana', 'cherry', and 'orange'.append()
fruits
Here's another example for you:
Let's create an array containing the names of cars:
cars = ["Lexus", "Toyota", "Mercedez"]
You can use the?method to add a new element to an existing list/array just as seen below.append()
fruits = ["apple", "banana", "cherry"]fruits.append("orange")print(fruits)# Output: ['apple', 'banana', 'cherry', 'orange']
輸出:
["Lexus", "Toyota", "Mercedez", "Honda"]
方法Clear()
顧名思義,該方法會(huì)從列表中刪除所有元素。clear()
例:
下面是使用該方法的示例:clear()
cars = ["Lexus", "Toyota", "Mercedez"]cars.clear()print(cars)
輸出:
根據(jù)上面方法的解釋?zhuān)@個(gè)表達(dá)式的結(jié)果將是 [] 空,因?yàn)槲覀円呀?jīng)清除了整個(gè)列表。clear()
方法Copy()
此函數(shù)創(chuàng)建并返回原始列表的相同副本。
例:
fruits = ["apple", "banana", "cherry"]fruits_copy = fruits.copy()print(fruits_copy)# Output: ['apple', 'banana', 'cherry']
在上面的例子中,copy() 方法創(chuàng)建了一個(gè)名為 fruits_copy 的新數(shù)組,它是 fruit 數(shù)組的淺拷貝。修改fruits_copy數(shù)組不會(huì)影響原來(lái)的水果數(shù)組。
下面是使用該方法的另一個(gè)示例:copy()
cars = ["Lexus", "Toyota", "Mercedez"]x = cars.copy()print(x)# Output of the above using the copy () method will be:["Lexus", "Toyota", "Mercedez"]
方法Count()
此方法返回具有指定值的元素?cái)?shù)。
例:
fruits = ["apple", "banana", "cherry", "banana"]count = fruits.count("banana")print(count)# Output: 2
上面的代碼創(chuàng)建了一個(gè)名為水果的列表,其中包含四個(gè)元素:“蘋(píng)果”、“香蕉”、“櫻桃”和另一個(gè)“香蕉”。然后在列表中使用該方法來(lái)計(jì)算元素“banana”的出現(xiàn)次數(shù)。它返回計(jì)數(shù),在本例中為 2,因?yàn)椤癰anana”在列表中出現(xiàn)兩次。count()
fruits
最后,將計(jì)數(shù)值打印到控制臺(tái),產(chǎn)生輸出:2。
下面是使用該方法的另一個(gè)示例:count()
# Return the number of times the value "Lexus" appears in the car list.cars = ["Lexus", "Toyota", "Mercedez", "Lexus"]x = cars.count("Lexus")print(x)
輸出將返回 int “2” 作為結(jié)果,因?yàn)椤袄卓怂_斯”在汽車(chē)列表中出現(xiàn)了兩次。
方法Extend()
使用此方法,可以將列表(或任何可迭代對(duì)象)的元素添加到當(dāng)前列表的末尾。
例:
fruits = ["apple", "banana", "cherry"]additional_fruits = ["orange", "grape"]fruits.extend(additional_fruits)print(fruits)# Output: ['apple', 'banana', 'cherry', 'orange', 'grape']
在上面的示例中,該方法用于將列表中的元素添加到數(shù)組中。生成的數(shù)組包含兩個(gè)數(shù)組中的所有元素。extend()
additional_fruits
fruits
請(qǐng)注意,該方法就地修改原始數(shù)組,并且不返回新數(shù)組。extend()
方法index()
此方法返回具有指定值的第一個(gè)元素的索引。
fruits = ["apple", "banana", "cherry"]index = fruits.index("banana")print(index)# Output: 1
上面的代碼創(chuàng)建了一個(gè)包含“蘋(píng)果”、“香蕉”和“櫻桃”的水果列表。然后,它在列表中找到“banana”的索引位置,并將其分配給變量“index”。最后,它打印“index”的值,在本例中為 1。
方法insert()
此數(shù)組方法在指定位置添加一個(gè)元素。
numbers = [1, 2, 3, 5, 6]numbers.insert(3, 4)print(numbers)# Output: [1, 2, 3, 4, 5, 6]
從上面的代碼中,我們有一個(gè)帶有元素 [1, 2, 3, 5, 6] 的數(shù)組編號(hào)。我們想在索引 4 處插入數(shù)字 3(這是數(shù)組中的第四個(gè)位置,因?yàn)?Python 是 0 索引的)。通過(guò)調(diào)用 insert(3, 4),我們將元素 4 插入到指定的索引處,現(xiàn)有元素向右移動(dòng)以便為新元素騰出空間。生成的數(shù)組為 [1, 2, 3, 4, 5, 6]。
方法pop()
此方法刪除指定位置的元素。
示例:
# To delete an item from an array/list, you can utilize the pop() method.# Delete the second element of the car array:cars = ["Lexus", "Toyota", "Mercedez"]cars.pop(2)print(cars)
這是輸出:
['Lexus', 'Toyota']
上面的代碼使用 'pop()' 方法從 'cars' 數(shù)組中刪除第二個(gè)元素,然后打印更新的數(shù)組。
下面是另一個(gè)使用 pop() 方法的示例:
# To delete an item from an array/list, you can utilize the pop() method.# Delete the second element of the car array:cars = ["Lexus", "Toyota", "Mercedez"]cars.pop(2)print(cars)
輸出:
['Lexus', 'Toyota']
該代碼使用 'pop()' 方法從 'cars' 數(shù)組中刪除第二個(gè)元素,然后打印修改后的數(shù)組。生成的數(shù)組將僅包含第一個(gè)和第三個(gè)元素:['雷克薩斯', '豐田']。
方法remove()
此方法刪除具有指定值的第一項(xiàng)。
例:
fruits = ["apple", "banana", "cherry"]fruits.remove("banana")print(fruits)# Output Below:["apple", "cherry"]
上面的代碼創(chuàng)建了一個(gè)包含三個(gè)元素的列表:“蘋(píng)果”、“香蕉”和“櫻桃”。然后使用該方法從列表中刪除元素“banana”。fruits
remove()
刪除“香蕉”后,使用該功能打印更新的列表。輸出將為 ,因?yàn)榱斜碇胁辉俅嬖凇跋憬丁薄?code>print()['apple', 'cherry']
下面是使用該方法的另一個(gè)示例:remove()
cars = ["Lexus", "Toyota", "Mercedez"]cars.remove("Toyota")print(cars)
remove()?函數(shù)也可用于從數(shù)組中刪除元素,但應(yīng)該注意的是,它只從列表中刪除指定值的第一個(gè)實(shí)例。
方法reverse()
This method reverses the order of the list.
Example:
fruits = ["apple", "banana", "cherry"]fruits.reverse()print(fruits)# Output: ['cherry', 'banana', 'apple']
The code above creates a list called fruits with three elements: 'apple', 'banana', and 'cherry'. Then, the?method is called on the fruits list which reverses the order of its elements. Finally, the reversed list is printed using the print() function, resulting in the output ['cherry', 'banana', 'apple']. This means that the original order of the fruits list has been reversed.reverse()
The?method?sort()
This method sorts the list, just as the name implies.
例:
numbers = [4, 2, 1, 3]numbers.sort()print(numbers)# Output: [1, 2, 3, 4]
上面的代碼創(chuàng)建了一個(gè)使用元素調(diào)用的列表。然后,在列表中調(diào)用該方法,該方法按升序?qū)υ剡M(jìn)行排序。排序后,列表變?yōu)?。最后,使用 將排序后的列表打印到控制臺(tái),輸出 .numbers
[4, 2, 1, 3]
sort()
numbers
numbers
[1, 2, 3, 4]
print(numbers)
[1, 2, 3, 4]
默認(rèn)情況下,Python 中的方法按升序?qū)α斜淼脑剡M(jìn)行排序。如果要按降序?qū)α斜磉M(jìn)行排序,可以將參數(shù)傳遞給該方法。sort()
reverse=True
sort()
下面是如何按降序?qū)α斜磉M(jìn)行排序的示例:numbers
numbers = [4, 2, 1, 3]numbers.sort(reverse=True)print(numbers)# Output[4, 3, 2, 1]
通過(guò)作為參數(shù)傳遞給方法,列表按降序排序。reverse=True
sort()
結(jié)論
希望在閱讀本文后,您現(xiàn)在應(yīng)該對(duì) Python 中的數(shù)組有一個(gè)基本的了解。
您還應(yīng)該了解用于修改數(shù)組或列表的基本 Python 數(shù)組方法以及如何使用它們。