Python序列型別


在Python程式設計中,一些基本的序列型別類包括列表字串元組、Range等。這些資料結構儲存專案的排序集合。它們允許我們透過索引和迭代訪問其元素,還有其他序列型別物件,例如位元組序列。

Python中的序列型別

Python中的序列型別分為兩種:可變不可變序列。

可變序列型別

這些序列可以在建立後更改,我們還可以修改元素、新增新元素和刪除現有元素。

  • 列表:列表是一個可變的、有序的專案集合。
  • 位元組陣列:一個可變的位元組序列,用於處理二進位制資料。

  • 陣列:與列表類似的專案集合,但針對數值運算和儲存相同資料型別的元素進行了最佳化。

示例

import array

# 1.List example
# creating list
my_list = [10, 20, 30, 40, 50]

# Appending element
my_list.append(60)
print(my_list)

# Removing element
my_list.remove(10)
print(my_list)

# 2.Byte Array example
# Creating byte array
my_byte_array = bytearray([15, 16, 17, 18])

# Modifying a byte
my_byte_array[1] = 19
print(my_byte_array)

# Removing a byte
my_byte_array.pop(3)
print(my_byte_array) 

# 3.Array example
# Creating array of integers
my_array = array.array('i', [21, 22, 23, 24, 25])

# Modifying element
my_array[2] = 26
print(my_array)

my_array.append(26)
print(my_array)

輸出

[10, 20, 30, 40, 50, 60]
[20, 30, 40, 50, 60]
bytearray(b'\x0f\x13\x11\x12')
bytearray(b'\x0f\x13\x11')
array('i', [21, 22, 26, 24, 25])
array('i', [21, 22, 26, 24, 25, 26])

不可變序列型別

分類為不可變的序列在其建立後元素不可更改。一些不可變序列型別如下所示。

  • 元組:元組被稱為有序且不可變的物件集合。
  • 字串:用單引號('')或雙引號("")分隔的字元序列集合稱為字串。
  • 整數:整數定義為整數,包括正數和負數,是一種不可變的資料型別。

示例

# 1.Tuple example
# Creating a tuple
my_tuple = (10, 20, 30, 40, 50)

# Accessing element
print(my_tuple[3])

# Tuples Concatenation to create a new tuple
new_tuple = my_tuple + (60, 70)
print("New Tuple:", new_tuple)

# 2.String Example
# Creating a string
my_string = "Tutorials Point"

# Accessing a character
print(my_string[4])

# Strings can be concatenated to create a new string
new_string = my_string + " python"
print("String:", new_string)

# 3.Integer Example
# Creating an integer
my_integer = 30

# Performing operation to create new integer
new_integer = my_integer + 5
print("New Integer:", new_integer)
40
New Tuple: (10, 20, 30, 40, 50, 60, 70)
r
String: Tutorials Point python
New Integer: 35

成員運算子

在Python中,成員運算子(in, not in) 用於檢查物件中是否存在序列。

一些常見的內建方法和操作,對於序列型別物件,可用於可變和不可變序列,如下所示

操作 語法 描述
in x in seq 如果x在序列seq中找到,則返回True,否則返回False
not in x not in seq 如果x在序列seq中找到,則返回False,否則返回True

示例

lang = ['java', 'python', 'c++']
print('python' in lang) 
print('java' not in lang) 

輸出

True
False

連線和重複

Python中的連線和重複受可變(列表)和不可變(元組、字串)序列資料型別支援,但range物件等序列型別不支援此連線和重複操作。

操作 語法 描述
連線 sequence1 + sequence2 連線(組合)兩個序列。
重複 sequence * n 重複原始序列,直到給定次數(n)。

示例

list1 = [10, 20, 30]
list2 = [40, 50]

#Concatenation (+)
print(list1 + list2)  

#Repetition (*)
print(list1 * 3)  

輸出

[10, 20, 30, 40, 50]
[10, 20, 30, 10, 20, 30, 10, 20, 30]

索引和切片

索引是指使用其索引訪問序列中存在的元素的過程。Python序列中第一個元素的索引為0,第二個元素的索引或位置為1。

透過其起始和結束索引(起始、停止和步長值)訪問序列的子序列稱為切片。

操作 語法 描述
索引 sequence[index] 按其位置訪問序列中的元素。
切片 sequence[start : stop : step] 透過起始、停止和步長值訪問序列的子序列。

示例

my_list = ['a', 'b', 'c', 'd','e']

#indexing
print(my_list[2])  

#slicing (start,stop)
print(my_list[1:4])

#(start,stop,step)
#Slices the sequence from index i to j with a step k.
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers[1:8:2])

輸出

c
['b', 'c', 'd']
[1, 3, 5, 7]

長度、搜尋和計數

我們可以使用內建的len()函式確定序列的長度。它可以應用於各種型別的序列,例如字串、列表、元組等。

可以搜尋序列中的特定元素,具體取決於序列的型別(列表、字串、元組)。

Python的count()函式允許我們計算序列中出現的元素的次數。

操作 語法 描述
長度 len(sequence) 確定序列中元素的數量。
搜尋 sequence.index(item) 查詢序列中元素的第一次出現。
計數 sequence.count(item) 計算元素在序列中出現的次數。

示例

my_list = [1, 5, 4, 5, 3, 20, 12, 5, 43]

#Finding Length
print('length of sequence :',len(my_list))

#Searching an element
print('Element 12 at index :',my_list.index(12))

#counting the occurrences of an element
print('Num of times element 5 occured :',my_list.count(5))

輸出

length of sequence : 9
Element 12 at index : 6
Num of times element 5 occured : 3

追加、彈出、刪除

在Python中,追加、彈出和刪除函式通常與可變序列型別一起使用,例如陣列、列表和位元組陣列。

  • append()方法將向現有列表的末尾新增一個元素。

  • pop()方法刪除並返回列表中所需位置的元素。

  • remove()方法將刪除序列中元素的第一次出現。

不可變序列型別(如元組、字串和整數)不支援這些操作,因為它們在建立後無法更改。

操作 語法 描述
追加 list.append(element) 在列表末尾新增單個元素。
彈出 element = list.pop([index]) 刪除並返回序列中的元素。
刪除 list.remove(value) 刪除序列中元素的第一次出現。

示例

my_list = [10, 20, 30, 40, 50]

#append element
my_list.append(60)
print(my_list)

#popping element
my_list.pop(1)
print(my_list)

#remove element
my_list.remove(40)
print(my_list) 

輸出

[10, 20, 30, 40, 50, 60]
[10, 30, 40, 50, 60]
[10, 30, 50, 60]

更新於:2024年9月10日

7000+ 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.