linknode
好處 插入只要O(1)
壞處 訪問要O(n)
full binary tree v.s. complete binary tree
一些公式/函數
sort
所有使用公式進行排列的時間複雜度 都是 O(nlogn)
1 2 3 4 5 6 7 8 9
| sorted_numbers = sorted(numbers)
sorted_numbers_desc = sorted(numbers, reverse=True)
numbers = [5, 2, 9, 1, 5, 6] numbers.sort() print(numbers)
|
pop
pop()
方法從列表中移除並返回指定位置的元素,如果不指定索引,則默認移除並返回列表的最後一個元素。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| person = {"name": "Alice", "age": 25, "city": "New York"}
age = person.pop("age") print(age) print(person)
city = person.pop("city", "Unknown") print(city)
country = person.pop("country", "Unknown") print(country)
|
append
append()
方法在列表的末尾添加一個元素。
1 2 3 4 5 6 7 8 9 10 11
| numbers = [10, 20, 30]
numbers.append(40) print(numbers)
fruits = ["apple", "banana"] fruits.append("cherry") print(fruits)
|
insert
insert()
方法在指定位置插入元素
1 2 3 4 5 6
| numbers = [10, 20, 30, 40, 50]
numbers.insert(2, 25) print(numbers)
|
remove
remove()
方法移除列表中第一次出現的指定元素。
1 2 3 4 5 6
| numbers = [10, 20, 30, 40, 50, 30]
numbers.remove(30) print(numbers)
|
len
len()
函數用來獲取序列(如列表、字典、字符串等)的長度。
1 2 3 4 5 6 7 8 9 10 11 12
| numbers = [10, 20, 30, 40, 50] print(len(numbers))
text = "hello" print(len(text))
person = {"name": "Alice", "age": 25, "city": "New York"} print(len(person))
|
set
列表轉換為集合
1 2 3 4 5 6 7 8
| numbers = [1, 2, 2, 3, 4, 4, 5]
set_numbers= set(numbers)
print(set_numbers)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| list1 = [1, 2, 3, 4, 5] list2 = [4, 5, 6, 7, 8]
set1 = set(list1) set2 = set(list2)
union_set = set1.union(set2) print(union_set)
intersection_set = set1.intersection(set2) print(intersection_set)
difference_set = set1.difference(set2) print(difference_set)
|
集合轉換回列表
1 2 3 4 5 6 7 8
| set_numbers = {1, 2, 3, 4, 5}
numbers_list = list(set_numbers)
print(numbers_list)
|
enumerate
使用 enumerate()
獲取索引和值
1 2 3 4 5 6 7 8
| fruits = ["apple", "banana", "cherry"]
for index, fruit in enumerate(fruits): print(f"Index {index} is {fruit}")
|
index
使用index()
獲取第一個的索引
1 2 3 4 5 6
| numbers = [10, 20, 30, 20, 40]
index_of_20 = numbers.index(20)
print(index_of_20)
|
zip
使用 zip()
合併列表
1 2 3 4 5 6 7 8 9
| names = ["Alice", "Bob", "Charlie"] ages = [25, 30, 35]
for name, age in zip(names, ages): print(f"{name} is {age} years old")
|
hashmap
其實python 要建立一個 hashmap 利用字典就可以
defaultdict
defaultdict
是 collections
模組中的一個子類,用於當鍵不存在時提供默認值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| from collections import defaultdict
dd = defaultdict(list) dd['a'].append(1) dd['b'].append(2) print(dd)
dd = defaultdict(int) dd['a'] += 1 dd['b'] += 2 print(dd)
|
heapq
heapq
模組提供了堆隊列算法,也稱為優先隊列算法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import heapq
heap = []
heapq.heappush(heap, 3) heapq.heappush(heap, 1) heapq.heappush(heap, 2) print(heap)
smallest = heapq.heappop(heap) print(smallest) print(heap)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| a = 10 b = 5 c = 3
add_result = a + b print(f"{a} + {b} = {add_result}")
sub_result = a - b print(f"{a} - {b} = {sub_result}")
mul_result = a * b print(f"{a} * {b} = {mul_result}")
div_result = a / b print(f"{a} / {b} = {div_result}")
int_div_result = a // c print(f"{a} // {c} = {int_div_result}")
mod_result = a % c print(f"{a} % {c} = {mod_result}")
|