2023-09-24|閱讀時間 ‧ 約 11 分鐘

【筆記】Python的資料型態:list、tuple(下)

延續上一篇


常使用的函式

sort

對原始串列進行排序

numbers = [3, 1, 4, 2, 5]
numbers.sort()
print(numbers)

----結果----
[1, 2, 3, 4, 5]


跟它很相似函數 sorted,它不會修改原始串列,而是新增一個修改後的串列

numbers = [3, 1, 4, 2, 5]
sort_numbers = sorted(numbers)
print(sort_numbers)

----結果----
[1, 2, 3, 4, 5]


reverse

反轉中元素的順序

numbers = [3, 1, 4, 2, 5]
numbers.reverse()
print(numbers)

----結果----
[5, 2, 4, 1, 3]


如果不想要改變原本的資料,可以用下面這種方法

numbers = [3, 1, 4, 2, 5]
numbers_reverse = numbers[::-1]
print(numbers_reverse)

----結果----
[5, 2, 4, 1, 3]

第一個":"表示串列開頭

第二個":"表示串列結尾

"-1"代表從後面數過來

總體就會是串列從頭到尾,從後面數過來,就可以達到reverse的效果了


count

統計指定元素出現的次數,如果指定的元素不存在,則返回0

numbers = [3, 1, 4, 2, 5, 5, 5, 5, 5, 3, 2, 1, 4, 3, 5]
count_5 = numbers.count(5)
count_0 = numbers.count(0)

print(f"5總共出現:{count_5}次")
print(f"0總共出現:{count_0}次")

----結果----
5總共出現:6
0總共出現:0

因為沒有改變任何的資料結構,所以串列跟元組都可以用


len

用來獲取元素長度或個數

numbers = [3, 1, 4, 2, 5, 5, 5, 5, 5, 3, 2, 1, 4, 3, 5]
len_numbers = len(numbers)
print(len_numbers)

----結果----
15


index

用來查找指定元素或是子字串第一次出現的位置,如果指定的元素不存在,引發ValueError

numbers = [3, 1, 4, 2, 5, 5, 5, 5, 5, 3, 2, 1, 4, 3, 5]
numbers_index_1 = numbers.index(1)
numbers_index_9 = numbers.index(9)
print(f"1的位置在:第{numbers_index_1}")
print(f"9的位置在:第{numbers_index_9}")
----結果----
1的位置在:第1

----結果----
ValueError: 9 is not in list


如果要找出所有的"1",可以利用for迴圈跟enumerate函數,enumerate 用來遍歷可疊代對象,返回時除了會返回元素本身以外,還會返回位置

numbers = [3, 1, 4, 2, 5, 5, 5, 5, 5, 3, 2, 1, 4, 3, 5]

# 查找數值
target_value = 1

# 用來存放找到的索引位置
numbers_of_1 = []

for index, value in enumerate(numbers):
if value == target_value:
numbers_of_1.append(index)

#計算找到幾個1​
count_of_1 = len(numbers_of_1)

print(f"1的位置在第{numbers_of_1}個")
print(f"找到{count_of_1}個1")

----結果----
1的位置在第[1, 11]
找到21


copy

淺複製,創建一個已有對象的拷貝,比如串列、元組的資料型態,複製後是一個獨立的對象,修改這個對象不會對原始資料造成影響,對於嵌套的資料,如子串列,仍然是資訊共享

numbers = [3, 1, 4, 2, 5]
copy_numbers = numbers.copy()

print(numbers)
print(copy_numbers)

----結果----
[3, 1, 4, 2, 5]
[3, 1, 4, 2, 5]


修改嵌套的資料,會一起更動,因為copy是引用過去的

numbers = [3, 1, [4, 2], 5]
copy_numbers = numbers.copy()
numbers[2][1]= 100

print(numbers)
print(copy_numbers)

----結果----
[3, 1, [4, 100], 5]
[3, 1, [4, 100], 5]


這時可以使用deepcopy(深複製)解決這個問題

# 需要使用copy模組的 deepcopy​
import copy

numbers = [3, 1, [4, 2], 5]
copy_numbers = numbers.copy()
deep_copy_numbers = copy.deepcopy(numbers)
numbers[2][1]= 100

print(f"被修改後的原始資料:{numbers}")
print(f"同步更改的的淺複製資料:{copy_numbers}")
print(f"沒有更改的的深複製資料:{deep_copy_numbers}")

----結果----
被修改後的原始資料:[3, 1, [4, 100], 5]
同步更改的的淺複製資料:[3, 1, [4, 100], 5]
沒有更改的的深複製資料:[3, 1, [4, 2], 5]


sum

用來處理可疊代對象的數學計算,包含整數或是浮點數,字串的話會出現Type Error

numbers = [3, 1, 4, 2, 5]
total = sum(numbers)
print(total)

----結果----
15


1加到100

total = sum(range(1, 101))
print(total)

----結果----
5050


字串合在一起的話可以這樣處理

words = ["Hello", " ", "Pikachu"]
result = "".join(words)
print(result)

----結果----
Hello Pikachu


min跟max

用於在一個可疊代對象(如串列、元组、字串等)中找到最小值和最大值

numbers = [5, 1, 8, 2, 9, 3]

min_value = min(numbers)
max_value = max(numbers)

print(f"最小值:{min_value}")
print(f"最大值:{max_value}")

----結果----
最小值:1
最大值:9


從變數中,找到最小值跟最大值

A = 30
B = 85
C = 101

min_value = min(A ,B ,C)
max_value = max(A ,B ,C)

print(f"最小值變數:{min_value}")
print(f"最大值變數:{max_value}")

----結果----
最小值參數:30
最大值參數:101


字串的話,我們可以比長度

pokemon = ["Pikachu", "Charizard", "Squirtle", "Bulbasaur"]
longest_word = max(pokemon, key=len)
shortest_word = min(pokemon, key=len)

print(f"最短的是:{longest_word}")
print(f"最長的是:{shortest_word}")

----結果----
最短的是:Charizard
最長的是:Pikachu


any跟all

檢查可疊代函數中的內容是否為真值,回傳布林值

any:至少有一個數值為真,返回True,否則返回False

all:全部的數值為真,返回True,否則返回False

pokemon = ["Pikachu", "Charizard", "Squirtle", "Bulbasaur", ""]
pokemon_no_numbers = any(pokemon)
print(pokemon_no_numbers)

----結果----
True


如果是用all,在有一個內鬼的情況下就不行了

pokemon = ["Pikachu", "Charizard", "Squirtle", "Bulbasaur", ""]
pokemon_no_numbers = all(pokemon)
print(pokemon_no_numbers)

----結果----
False


增加條件式判斷

numbers = [1, 2, 3, 4, 5]
has_odd = any(x % 2 == 1 for x in numbers)
all_odd = all(x % 2 == 1 for x in numbers)
print(f"至少有一個奇數:{has_odd}")
print(f"全部都是奇數:{all_odd}")

----結果----
至少有一個奇數:True
全部都是奇數:False


如果要讓True跟False改成回文字的話,加入if else判斷式

numbers = [1, 2, 3, 4, 5]
has_odd = any(x % 2 == 1 for x in numbers)
all_odd = all(x % 2 == 1 for x in numbers)

has_odd_str = "是" if has_odd else "否"
all_odd_str = "是" if all_odd else "否"

print(f"至少有一個奇數:{has_odd_str}")
print(f"全部都是奇數:{all_odd_str}")

----結果----
至少有一個奇數:是
全部都是奇數:否
分享至
成為作者繼續創作的動力吧!
© 2024 vocus All rights reserved.