Published 12月 16, 2018 by with 0 comment

Automate The Boring Stuff With Python - Chapter 4 - 列表

今天是看這本書Automate the Boring Stuff with Python(Python編程快速上手--讓繁瑣工作自動化)
第4章節(Lists)所做的練習

這章主要講列表(List)與元組(Tuple)
列表的表示方式是方括號[]
元組用圓括號表示()
>>> ['aaa', 1, 'bb', 22, 'ccc']
['aaa', 1, 'bb', 22, 'ccc']
>>> 
>>> type(['aaa', 1, 'bb', 22, 'ccc'])

>>> 
>>> ('aaa', 1, 'bb', 22, 'ccc')
('aaa', 1, 'bb', 22, 'ccc')
>>> 
>>> type(('aaa', 1, 'bb', 22, 'ccc'))


可用下標來取得列表或元組中某個值, 或改變某個值(但元組不行改值).
>>> spam = ['aaa', 1, 'bb', 22, 'ccc']
>>> spam[2]
'bb'
>>> 
>>> spam = ['aaa', 1, 'bb', 22, 'ccc']
>>> spam[2] = 'BB'
>>> spam
['aaa', 1, 'BB', 22, 'ccc']
>>> 
>>> spam = ('aaa', 1, 'bb', 22, 'ccc')
>>> spam[2]
'bb'
>>> 
>>> spam = ('aaa', 1, 'bb', 22, 'ccc')
>>> spam[2] = 'BB'
Traceback (most recent call last):
  File "", line 1, in 
    spam[2] = 'BB'
TypeError: 'tuple' object does not support item assignment
>>> 


可用切片來取得列表中多個值
>>> spam = ['aaa', 1, 'bb', 22, 'ccc']
>>> spam [2:-1]
['bb', 22]


可以用len來取得列表或元組的長度
>>> spam = ['aaa', 1, 'bb', 22, 'ccc']
>>> len(spam)
5
>>> 
>>> spam = ('aaa', 1, 'bb', 22, 'ccc')
>>> len(spam)
5


可用+ & *號來合併或複製列表或元組
>>> spam0 = ['aaa','bbb']
>>> spam1 = [111, 222]
>>> spam0 + spam1
['aaa', 'bbb', 111, 222]
>>> 
>>> spam0 * 3
['aaa', 'bbb', 'aaa', 'bbb', 'aaa', 'bbb']
>>> 
>>> [111, 222] * 2
[111, 222, 111, 222]
>>> 
>>> spam0 = ('aaa','bbb')
>>> spam1 = (111, 222)
>>> spam0 + spam1
('aaa', 'bbb', 111, 222)
>>> 
>>> spam0 * 3
('aaa', 'bbb', 'aaa', 'bbb', 'aaa', 'bbb')
>>> 
>>> (111, 222) * 3
(111, 222, 111, 222, 111, 222)


可用del & remove來刪除列表中的值, 但元組都不行
>>> spam = ['aaa', 1, 'bb', 22, 'ccc']
>>> del spam[0]
>>> spam
[1, 'bb', 22, 'ccc']
>>> 
>>> spam = ['aaa', 1, 'bb', 22, 'ccc']
>>> spam.remove('bb')
>>> spam
['aaa', 1, 22, 'ccc']
>>> 
>>> spam = ('aaa', 1, 'bb', 22, 'ccc')
>>> del spam[0]
Traceback (most recent call last):
  File "", line 1, in 
    del spam[0]
TypeError: 'tuple' object doesn't support item deletion
>>> 
>>> spam = ('aaa', 1, 'bb', 22, 'ccc')
>>> spam.remove('bb')
Traceback (most recent call last):
  File "", line 1, in 
    spam.remove('bb')
AttributeError: 'tuple' object has no attribute 'remove'
>>> 


列表與循環的組合
>>> spam = ['aaa', 'bbb', 'ccc']
>>> for i in range(len(spam)):
    print('Inedx ' + str(i) + ' is: ' + spam[i])
   
Inedx 0 is: aaa
Inedx 1 is: bbb
Inedx 2 is: ccc
>>> 


可用index()來查列表或元組中某個值
>>> spam = ['aaa', 1, 'bb', 22, 'ccc']
>>> spam.index('bb')
2
>>> 
>>> spam = ('aaa', 1, 'bb', 22, 'ccc')
>>> spam.index(22)
3


可用append()和insert()來填加或插入列表, 但元組不行
>>> spam = ['aaa', 1, 'bb', 22, 'ccc']
>>> spam.append(333)
>>> spam
['aaa', 1, 'bb', 22, 'ccc', 333]
>>> 
>>> spam = ['aaa', 1, 'bb', 22, 'ccc']
>>> spam.insert(2, 'BBB')
>>> spam
['aaa', 1, 'BBB', 'bb', 22, 'ccc']
>>> 
>>> spam = ('aaa', 1, 'bb', 22, 'ccc')
>>> spam.append(333)
Traceback (most recent call last):
  File "", line 1, in 
    spam.append(333)
AttributeError: 'tuple' object has no attribute 'append'
>>> 
>>> spam = ('aaa', 1, 'bb', 22, 'ccc')
>>> spam.insert(2, 'BBB')
Traceback (most recent call last):
  File "", line 1, in 
    spam.insert(2, 'BBB')
AttributeError: 'tuple' object has no attribute 'insert'
>>> 


用sort照ASCII字符順序排列(先大寫英文, 後小寫英文), 但元組不行
用sort(key=srt.lower)來先小寫後大寫
>>> spam = ['aaa', 'BB', 'cCc', 'CcC']
>>> spam.sort()
>>> spam
['BB', 'CcC', 'aaa', 'cCc']
>>> 
>>> spam = ['aaa', 'BB', 'cCc', 'CcC']
>>> spam.sort(key=str.lower)
>>> spam
['aaa', 'BB', 'cCc', 'CcC']
>>> 
>>> spam = ('aaa', 'BB', 'cCc', 'CcC')
>>> spam.sort()
Traceback (most recent call last):
  File "", line 1, in 
    spam.sort()
AttributeError: 'tuple' object has no attribute 'sort'
>>> 


用list()跟tuple()來做互換類型
>>> spam0 = ('aaa','bbb')
>>> type(spam0)

>>> type(list(spam0))

>>> 
>>> spam1 = [111, 222]
>>> type(spam0)

>>> type(tuple(spam0))


如果要講兩個變量指向兩個不同的列表,
用copy(淺層拷貝)或deepcopy(深層拷貝)
>>> import copy
>>> spam0 = ['aaa','bbb']
>>> spam1 = copy.copy(spam0)
>>> spam1[0] = 'BBB'
>>> spam0
['aaa', 'bbb']
>>> spam1
['BBB', 'bbb']
>>> 
>>> import copy
>>> spam0 = [['aaa','bbb'],['ccc','ddd']]
>>> spam1 = copy.deepcopy(spam0)
>>> spam1[1][0] = 'CCC'
>>> spam0
[['aaa', 'bbb'], ['ccc', 'ddd']]
>>> spam1
[['aaa', 'bbb'], ['CCC', 'ddd']]


章節4習題, 用Notepad++打下列的程式碼,
另存為Chapter_4.py. 我附上中文注釋方便好讀.
# -*- coding: UTF-8 -*-
# http://juilin77.blogspot.com/
# v20181216
# Automate The Boring Stuff With Python - Chapter 4

# 一個橫心的列表, 每個列表有6(0-5)個元素, 長度為9(0-8)(由上到下共9層列表)
grid = [['.', '.', '.', '.', '.', '.'],
        ['.', '0', '0', '.', '.', '.'],
        ['0', '0', '0', '0', '.', '.'],
        ['0', '0', '0', '0', '0', '.'],
        ['.', '0', '0', '0', '0', '0'],
        ['0', '0', '0', '0', '0', '.'],
        ['0', '0', '0', '0', '.', '.'],
        ['.', '0', '0', '.', '.', '.'],
        ['.', '.', '.', '.', '.', '.']]

# 定義第1層
NewHeart = ''
# 取出每層列表的第1個元素, 並轉為字符
for i in range(len(grid)):
    NewHeart += str(grid[i][0])

# 定義第2層
NewHeart1 = ''
# 取出每層列表的第2個元素, 並轉為字符
for i in range(len(grid)):
    NewHeart1 += str(grid[i][1])

# 定義第3層
NewHeart2 = ''
# 取出每層列表的第3個元素, 並轉為字符
for i in range(len(grid)):
    NewHeart2 += str(grid[i][2])

# 定義第4層
NewHeart3 = ''
# 取出第i層列表的第4個值, 並轉為字符
for i in range(len(grid)):
    NewHeart3 += str(grid[i][3])

# 定義第5層
NewHeart4 = ''
# 取出每層列表的第5個元素, 並轉為字符
for i in range(len(grid)):
    NewHeart4 += str(grid[i][4])

# 定義第6層
NewHeart5 = ''
# 取出每層列表的第6個元素, 並轉為字符
for i in range(len(grid)):
    NewHeart5 += str(grid[i][5])

# 印出每一層分行印
print(NewHeart + '\n' + NewHeart1 + '\n' + NewHeart2 + '\n' + NewHeart3 + '\n' + NewHeart4 + '\n' + NewHeart5)


然後用Windows的cmd, 執行python.
D:\Tech\Python\Automate The Boring Stuff With Python>python Chapter_4.py
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....

D:\Tech\Python\Automate The Boring Stuff With Python>


完成

Reference:
Automate the Boring Stuff with Python
Python編程快速上手--讓繁瑣工作自動化
ISBN-10: B01N6B9BSA
https://www.amazon.com/Python%E7%BC%96%E7%A8%8B%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B-%E8%AE%A9%E7%B9%81%E7%90%90%E5%B7%A5%E4%BD%9C%E8%87%AA%E5%8A%A8%E5%8C%96-%E7%BE%8E-Al-Sweigart%EF%BC%88%E6%96%AF%E7%BB%B4%E5%8A%A0%E7%89%B9%EF%BC%89/dp/B01I0XN8XY/ref=sr_1_1?ie=UTF8&qid=1543814481&sr=8-1&keywords=9787115422699

官網:
https://automatetheboringstuff.com/

最初發表 / 最後更新: 2018.12.16 / 2018.12.16

0 comments:

張貼留言