Python 的主要資料結構是什麼?

Python 程式面試題實作練習

Kirill Smirnov

Data Science Consultant, Altran

資料結構

資料結構:用來組織與儲存資料的專用格式。

 

Python 主要資料結構

  • list(列表)
  • tuple(元組)
  • set(集合)
  • dictionary(字典)
Python 程式面試題實作練習

List(列表)

list:有序且可變的序列(如數字、字串等)。

my_list = [1, 2, 3, 4, 5]
print(my_list)
[1, 2, 3, 4, 5]
Python 程式面試題實作練習

List:存取元素

my_list = [1, 2, 3, 4, 5]
print(my_list[2])
3
print(my_list[-1])
5
print(my_list[1:4])
[2, 3, 4]
print(my_list[2:])
[3, 4, 5]
Python 程式面試題實作練習

List:修改元素

my_list = [1, 2, 3, 4, 5]
my_list[2] = 30
print(my_list)
[1, 2, 30, 4, 5]
my_list[:2] = [10, 20]
print(my_list)
[10, 20, 30, 4, 5]
Python 程式面試題實作練習

List:方法

my_list = [10, 20, 30, 40, 50]
my_list.append(60)
print(my_list)
[10, 20, 30, 40, 50, 60]
my_list.remove(60)
print(my_list)
[10, 20, 30, 40, 50]
Python 程式面試題實作練習

List:方法

my_list = [10, 20, 30, 40, 50]
my_list.pop()
50
print(my_list)
[10, 20, 30, 40]
my_list.count(40)
1
Python 程式面試題實作練習

Tuple(元組)

tuple:有序且不可變的序列(如數字、字串等)。

my_tuple = (1, 'apple', 2, 'banana')
print(my_tuple)
(1, 'apple', 2, 'banana')
my_tuple = 1, 'apple', 2, 'banana'
print(my_tuple)
(1, 'apple', 2, 'banana')
Python 程式面試題實作練習

Tuple:修改值

無法修改 tuple 中的元素。

my_tuple[0] = 10
TypeError
Python 程式面試題實作練習

Set(集合)

set:無序且不重複的集合(如數字、字串等)。

my_set = set([1, 2, 3, 4, 5])
print(my_set)
{1, 2, 3, 4, 5}
my_set = set([1, 1, 1, 2, 3, 4, 5, 5, 5])
print(my_set)
{1, 2, 3, 4, 5}
Python 程式面試題實作練習

Set:方法

my_set1 = set([1, 2, 3, 4, 5])
my_set2 = set([3, 4, 5, 6, 7])
my_set1.add(6)
print(my_set1)
{1, 2, 3, 4, 5, 6}
my_set1.remove(6)
print(my_set1)
{1, 2, 3, 4, 5}
my_set1.union(my_set2)
{1, 2, 3, 4, 5, 6, 7}
my_set1.intersection(my_set2)
{3, 4, 5}
my_set1.difference(my_set2)
{1, 2}
Python 程式面試題實作練習

Dictionary(字典)

dictionary:由鍵值對組成,鍵需唯一且不可變。

$\text{key} \rightarrow \text{value}$

fruits = {'apple': 10, 'orange': 6, 'banana': 9}
print(fruits)
{'apple': 10, 'banana': 9, 'orange': 6}
fruits = dict([('apple', 10), ('orange', 6), ('banana', 9)])
print(fruits)
{'apple': 10, 'banana': 9, 'orange': 6}
Python 程式面試題實作練習

Dictionary:存取值

依鍵存取值:

fruits = {'apple': 10, 'orange': 6, 'banana': 9}
fruits['apple']
10
fruits['grapefruit']
KeyError: 'grapefruit'
Python 程式面試題實作練習

Dictionary:修改值

fruits['apple'] = 20
print(fruits)
{'apple': 20, 'orange': 6, 'banana': 9}
fruits['grapefruit'] = 11
print(fruits)
{'apple': 20, 'orange': 6, 'banana': 9, 'grapefruit': 11}
Python 程式面試題實作練習

Dictionary:方法

fruits = {'apple': 10, 'orange': 6, 'banana': 9}
fruits.items()
dict_items([('apple', 10), ('orange', 6), ('banana', 9)])
Python 程式面試題實作練習

Dictionary:方法

fruits = {'apple': 10, 'orange': 6, 'banana': 9}
list(fruits.items())
[('apple', 10), ('orange', 6), ('banana', 9)]
Python 程式面試題實作練習

Dictionary:方法

fruits = {'apple': 10, 'orange': 6, 'banana': 9}
fruits.keys()
dict_keys(['apple', 'orange', 'banana'])
fruits.values()
dict_values([10, 6, 9])
Python 程式面試題實作練習

Dictionary:方法

fruits = {'apple': 10, 'orange': 6, 'banana': 9}
list(fruits.keys())
['apple', 'orange', 'banana']
list(fruits.values())
[10, 6, 9]
Python 程式面試題實作練習

Dictionary:方法

fruits = {'apple': 10, 'orange': 6, 'banana': 9}
fruits.popitem()
9
print(fruits)
{'apple': 10, 'orange': 6}
Python 程式面試題實作練習

List、Tuple、Set、Dictionary 的操作

my_list = [1, 2, 3, 4, 5]
len(my_list)
5
my_tuple = (1, 2, 3, 4, 5)
len(my_tuple)
5
my_set = set([1, 2, 3, 4])
len(my_set)
4
my_dict = {'a': 1, 'b': 2, 'c': 3}
len(my_dict)
3
Python 程式面試題實作練習

List、Tuple、Set、Dictionary 的操作

my_list = [1, 2, 3, 4, 5]
2 in my_list
True
my_tuple = (1, 2, 3, 4, 5)
2 in my_tuple
True
my_set = set([1, 2, 3, 4])
5 in my_set
False
my_dict = {'a': 1, 'b': 2, 'c': 3}
'b' in my_dict
True
Python 程式面試題實作練習

一起來練習吧!

Python 程式面試題實作練習

Preparing Video For Download...