DAY 9
0

## [Day_9]資料儲存容器 (2) - 串列(list)_(2)

### 串接兩個串列

``````#input
todo_list_1 = ['打球','掃地','煮飯']
todo_list_2 = ['洗澡','出門']
todo_list_all = todo_list_1 + todo_list_2
print(todo_list_all)

#output
['打球', '掃地', '煮飯', '洗澡', '出門']
``````

### 產生串列

#### 1.使用「函式list」產生串列，函式list可以輸入字串或tuple

``````#input
ithelp_1 = list('ithelp')
print(ithelp_1)
tuple_1 = ('a','b',1,2)
ithelp_2 = list(tuple_1)
print(ithelp_2)

#output
['i', 't', 'h', 'e', 'l', 'p']
['a', 'b', 1, 2]
``````

#### 2.使用「函式split」也會回傳串列

``````#input
ithelp = '2021/9/23'.split('/')
print(ithelp)

#output
['2021', '9', '23']
``````

### 使用「[開始:結束:間隔]」存取串列

#### 1.list[:]

``````#input
a = list('abcdefghijklmn')
print(a[:])

#output
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
``````

#### 2.list[開始:] , list[:結束] , list[開始:結束」

``````#input
a = list('abcdefghijklmn')
print(a[2:])
print(a[:7])
print(a[2:7])
print(a[-2:])
print(a[:-7])
print(a[-7:-2])

#output
['c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
['a', 'b', 'c', 'd', 'e', 'f', 'g']
['c', 'd', 'e', 'f', 'g']
['m', 'n']
['a', 'b', 'c', 'd', 'e', 'f', 'g']
['h', 'i', 'j', 'k', 'l']
``````

#### 3.list[開始:結束:間隔]

``````#input
a = list('abcdefghijklmn')
print(len(a))
print(a[0:14:2])
print(a[-1:-15:-3])

#output
14
['a', 'c', 'e', 'g', 'i', 'k', 'm']
['n', 'k', 'h', 'e', 'b']
``````

#### 4.list[::-1]

``````#input
a = list('abcdefghijklmn')
print(a[::-1])

#output
['n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']
``````

### 拷貝串列

``````#input
list_1 = [1,2,3,4]
list_2 = list_1
print(list_1)
print(list_2)
list_1[2]=500
print(list_1)
print(list_2)
list_2[2]=600
print(list_1)
print(list_2)

#output
[1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 500, 4]
[1, 2, 500, 4]
[1, 2, 600, 4]
[1, 2, 600, 4]
``````

``````#input
list_1 = [1,2,3,4]
list_3 = list_1[:]
print(list_1)
print(list_3)
list_1[2]=500
print(list_1)
print(list_3)
list_3[2]=600
print(list_1)
print(list_3)

#output
[1, 2, 3, 4]
[1, 2, 3, 4]
[1, 2, 500, 4]
[1, 2, 3, 4]
[1, 2, 500, 4]
[1, 2, 600, 4]
``````

Python淺顯易懂的小教室30