iT邦幫忙

2021 iThome 鐵人賽

DAY 3
0
自我挑戰組

Python資料分析學習地圖系列 第 3

Day 03 : Python 基礎觀念 (2)

昨天初步認識 python 怎麼設定變數的操作,今天我們要聚焦在串列、字典、字串的操作,東西有點多XD 大家實際把程式碼執行看看會更有感覺!

串列(List)

認識串列

串列可以一次儲存相當多的資料,每個資料我們稱為元素,元素可以是字串、整數、浮點數,甚至也可以是串列、字典哦!
串列這邊我們使用的是中括號 []

# integer
list_1 = [1, 2, 3, 4, 5]
# string
list_2 = ['1', '2', '3']
# 來點混搭風如何?
list_3 = [1, '1', '1.0']
# 串列的串列的串列?
list_4 = [1, [1, [1]]]
# 串列也可以是空的
list_5 = []

讀取串列

定義完之後我們要怎麽讀取它呢?我們可以用索引值的方式去做讀取。請記得索引值是從零開始(不是動漫啦)計算切片哦!

x = [1, 2, 3, 4, 5]
# 我要第一個數字
print(x[0])
# 我要最後一個數字
print(x[4])

啊難道我要傻傻地去數有幾個嗎?當然不用!倒過來數就好了XD

x = [1, 2, 3, 4, 5]
# 我要最後一個數字
print(x[-1])

當然你也可以知道串列長度之後減1(但應該沒有人這樣寫,有點冗XD)

x = [1, 2, 3, 4, 5]
# 我要最後一個數字
print(x[len(x)-1])

串列切片

接著我們只想取得串列中幾個元素,那要怎麼操作呢?
這邊大家實際操作會比較有心得,切記從零開始XD

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

# 取索引值前3,索引值是 0 1 2
x[:3]
>>> [1, 2, 3]

# 從索引值1開始到索引值4,索引值是1 2 3
x[1:4]
>>> [2, 3, 4]

# 取得串列最後第 n 個 
x[-2:]
>>> [4, 5]

# 每隔 step 從 start 開始到 end-1
# [start:end:step]
x[1:4:2]
>>> [2, 4]

搭配運算

可以使用 python 內建的函式,自己就不用額外寫哦~

# sum
x = [1, 2, 3, 4, 5]
sum(x)
>>> 15

# max
max(x)
>>> 5

# min
min(x)
>>> 1

也可以搭配 numpy 套件

import numpy as np

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

# 中位數
np.median(x)
>>> 3.0

# 平均數
np.mean(x)
>>> 3.0

增加或刪除元素

在最後加上一個元素

x = [1, 2, 3, 4, 5]
x.append(6)
print(x)
>>> [1, 2, 3, 4, 5, 6]

任意位置插入元素

x = [1, 2, 3, 4, 5]
x.insert(3, 6)
print(x)
>>> [1, 2, 3, 6, 4, 5]

刪除元素

x = [1, 2, 3, 4, 5]
x.pop()
print(x)
>>> [1, 2, 3, 4]

刪除指定元素

x = [1, 2, 3, 4, 5]
x.pop(3)
print(x)
>>> [1, 2, 3, 5]

當不知道元素索引值的時候,可使用這方法直接刪除元素

x = [1, 2, 3, 4, 5]
x.remove(3)
print(x)
>>> [1, 2, 4, 5]

顛倒排序

兩種方法都可以

x = [1, 2, 3, 4, 5]
print(x[::-1])
>>> [5, 4, 3, 2, 1]
x = [1, 2, 3, 4, 5]
x.reverse()
print(x)
>>> [5, 4, 3, 2, 1]

排序

遞增

x = [9, 4, 6, 10, 8]
x.sort()
print(x)
>>> [4, 6, 8, 9, 10]

遞減

x = [9, 4, 6, 10, 8]
x.sort(reverse=True)
print(x)
>>> [10, 9, 8, 6, 4]

請留意 sort 方法會造成該 list 順序永久更改,我們可以用 sorted 來賦值到新變數

x = [9, 4, 6, 10, 8]
y = sorted(x)
print('Before:', x)
print('After:', y)
>>> Before: [9, 4, 6, 10, 8]
>>> After: [4, 6, 8, 9, 10]

字典(Dict)

字典與串列不同,它無法給予索引值(0, 1, 2... ) 這些來取得,而是透過所謂的 key 對應到 value。
字典是使用大括號 {"key": "value"}

dict_1 = {}
dict_2 = {"banana": 10, "apple": 20, "lemon": 30}

回傳字典的值

fruit_price_dict = {"banana": 10, "apple": 20, "lemon": 30}
print(f"The price of banana is:", fruit_price_dict["banana"])
>>> The price of banana is: 10

加上新的元素

fruit_price_dict = {"banana": 10, "apple": 20, "lemon": 30}
fruit_price_dict["orange"] = 50
print(fruit_price_dict)
>>> {'banana': 10, 'apple': 20, 'lemon': 30, 'orange': 50}

更新 value

fruit_price_dict = {'banana': 10, 'apple': 20, 'lemon': 30, 'orange': 50}
fruit_price_dict["orange"] = 60
print(fruit_price_dict)
>>> {'banana': 10, 'apple': 20, 'lemon': 30, 'orange': 60}

刪除指定元素

fruit_price_dict = {'banana': 10, 'apple': 20, 'lemon': 30, 'orange': 50}
del fruit_price_dict["orange"]
print(fruit_price_dict)
>>> {'banana': 10, 'apple': 20, 'lemon': 30}

字串

回顧串列的觀念,我們教了串列有索引值,但其實也可以應用在字串當中哦!

切片處理

string = 'PythonGO!'
print(string[0])
>>> P
print(string[6:])
>>> GO!

轉成字串

string = 'PythonGO!'
list(string)
>>> ['P', 'y', 't', 'h', 'o', 'n', 'G', 'O', '!']

分隔處理

在處理資料常常會需要用到這招哦!

string = 'I want to play a game!'
string.split(' ')
>>> ['I', 'want', 'to', 'play', 'a', 'game!']

合併處理

string = ['I', 'want', 'to', 'play', 'a', 'game!']
' '.join(string)
>>> 'I want to play a game!'

上一篇
Day 02 : Python 基礎觀念 (1)
下一篇
Day 04 : Python 基礎觀念 (3)
系列文
Python資料分析學習地圖30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言