You don't have javascript enabled. Good luck! :(

This is a test

You’ll find this post in your _posts directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run jekyll serve, which launches a web server and auto-regenerates your site when a file is updated.

To add new posts, simply add a file in the _posts directory that follows the convention YYYY-MM-DD-name-of-post.ext and includes the necessary front matter. Take a look at the source for this post to get an idea about how it works.

Jekyll also offers powerful support for code snippets:

def print_hi(name)
  puts "Hi, #{name}"
end
print_hi('Tom')
#=> prints 'Hi, Tom' to STDOUT.

Check out the Jekyll docs for more info on how to get the most out of Jekyll. File all bugs/feature requests at Jekyll’s GitHub repo. If you have questions, you can ask them on Jekyll Talk.

  Jun 20, 2018     WenYuan     Python3  UPDATE:

[Python] 學習使用串列 (List)

串列 (List) 是一個儲存資料的容器, 如果你有學過 C/C++ Java 等其它語言的話, 你會發現他跟陣列 (array) 很類似, 只不過 Python 串列的功能更為強大, 可以同時存放多種不同類型的資料


練習使用一維串列

一維陣列的形式如下:

  • 宣告串列
list1 = [] # 建立空串列

# 下面的存放資料方式都是正確的
list2 = [1, 2, 3, 4, 5]
list3 = ['chris', 'peggy', 'mary']
list4 = [1, 2, 3, 'chris', 'peggy', 'mary']

print(list1)
print(list2)
print(list3)
print(list4)
  • 使用 index 印出特定項目
list1 = [] # 建立空串列

# 下面的存放資料方式都是正確的
list2 = [1, 2, 3, 4, 5]
list3 = ['chris', 'peggy', 'mary']
list4 = [1, 2, 3, 'chris', 'peggy', 'mary']

print(list1[0]) # 印出第 1 項
print(list1[3]) # 印出第 4 項
print(list1[0:4]) # 印出 1~4 項
print(list2[-1]) # 印出最後一項
print(list4[-2]) # 印出最後第二項
print(list4[2:]) # 從第三項印到最後一項
  • 在串列插入新項目
list1 = [] # 建立空串列

# 下面的存放資料方式都是正確的
list2 = [1, 2, 3, 4, 5]
list3 = ['chris', 'peggy', 'mary']
list4 = [1, 2, 3, 'chris', 'peggy', 'mary']

# 把 'new' 字串加到 list2 的最後一項
list2.append('new')

# 把 4 加到 list4 的 index=3 的地方
list4.insert(3, 4)

print(list2)
print(list4)
  • 從串列中刪除項目
list1 = [] # 建立空串列

# 下面的存放資料方式都是正確的
list2 = [1, 2, 3, 4, 5]
list3 = ['chris', 'peggy', 'mary']
list4 = [1, 2, 3, 'chris', 'peggy', 'mary']

# 刪除 list2 的最後一項
list2.pop()

# 刪除 list3 index=1 的項目
list3.pop(1)

# 刪除 list4 中, 值出現 'chris' 的第一項
list4.remove('chris')

print(list2)
print(list3)
print(list4)
  • 使用 count 與 index

count(value) 可以計算 value 在串列中出現的次數 ; 而 index(value) 可以回傳 value 在串列中的索引值

list1 = [] # 建立空串列

# 下面的存放資料方式都是正確的
list2 = [1, 2, 3, 4, 5]
list3 = ['chris', 'peggy', 'mary']
list4 = [1, 2, 3, 'chris', 'peggy', 'mary']
list5 = [1, 1, 1, 2, 2, 1, 3]

# 印出 list5 中出現 1 的次數
print(list5.count(1))

# 印出 list5 中, 值為 3 的索引值
print(list5.index(3))
  • 將串列內容做排序 or 反轉
list5 = [1, 1, 1, 2, 2, 1, 3]

# 將他由小到大排序好
list5.sort()

print(list5)

# 將他的順序反轉
list5.reverse()

print(list5)
  • 判斷項目是否存在於串列中
list5 = [1, 1, 1, 2, 2, 1, 3]

# 若 list5 中有 2 的話就回傳 True , 沒有就回傳 False
print(2 in list5)
print(2 not in list5)
  • 計算最大最小值以及總和
list5 = [1, 1, 1, 2, 2, 1, 3]

print(max(list5)) # 印出最大值
print(min(list5)) # 印出最小值
print(sum(list5)) # 印出總和
print(len(list5)) # 印出 list5 的長度
  • 串列的連結以及複製
list1 = [1, 2, 3]
list2 = [4, 5, 6]

# 將 list1 和 list2 兩串列合起來
list3 = list1 + list2
print(list3)

# 將 list1 多複製一次
list4 = list1 * 2
print(list4)
  • 使用 for 迴圈來印出串列
List = ['Peter', 'Kevin', 'George', 'Chris', 'Peggy', 'Mary']

for i in range(len(List)):
    print('List[%i] = %s' %(i, List[i]))


練習使用二維串列

多個一維串列所構成, 架構如下:

  • 宣告串列
# 宣告一個二維串列
List = [[1, 2, 3], [4, 5, 6]]

print(List)
print(List[0])
print(List[1])
  • 將新項目加到二維串列中

下面用數字以亂數產生

import random

rows = int(input('Enter the amount of row: '))
columns = int(input('Enter the amount of column: '))
List = [] # 宣告一個一維串列

for i in range(rows):
    List.append([]) # 令其轉為二維串列形式

    for j in range(columns):
        # 將亂數產生的值放入串列中
        List[i].append(random.randint(1, 100))

print(List)
  • 使用 for 迴圈來印出二維串列
import random

rows = int(input('Enter the amount of row: '))
columns = int(input('Enter the amount of column: '))
List = [] # 宣告一個一維串列

for i in range(rows):
    List.append([]) # 令其轉為二維串列形式

    for j in range(columns):
        # 將亂數產生的值放入串列中
        List[i].append(random.randint(1, 100))

# 使用 for 迴圈來印出串列
for i in range(len(List)):
    for j in range(len(List[i])):
        print('%5d' %(List[i][j]), end = '')
    print()