DAY 9
1

## list

list是在python中，非常常用的功能，他可以把許多相同性質的資料存放在一起，方便查詢。

``````money = ['100\$','200\$']
``````

``````money = ['100\$','200\$']
print(money) #輸出為 ['100\$', '200\$']
``````

``````money = ['100\$','200\$']
print(money[0]) #取出money陣列的第0(1)個值，輸出為 100\$
print(money[1]) #取出money陣列的第1(2)個值，輸出為 200\$
``````

``````money = ['100\$','200\$']
print(len(money)) #陣列長度為2 輸出為 2
``````

``````money = ['1500\$','1400\$','2100\$','3100\$','5400\$','2500\$','4200\$']
print(money[-1]) #取出money陣列的倒數第一個值，輸出為 4200\$
print(money[-2]) #取出money陣列的倒數第二個值，輸出為 2500\$
``````

``````money = ['1500\$','1400\$','2100\$','3100\$','5400\$','2500\$','4200\$']
print(money[1:3]) #取出1到2的所有值，輸出為 ['2100\$', '3100\$']
``````

``````money = ['1500\$','1400\$','2100\$','3100\$','5400\$','2500\$','4200\$']
money.insert(1, '6000\$') #在陣列的第1(2)個位置插入 6000\$ 這個字串元素
print(money) #，輸出為 ['1500\$', '6000\$', '1400\$', '2100\$', '3100\$', '5400\$', '2500\$', '4200\$']
``````

``````money = ['1500\$','1400\$','2100\$']
money.remove('1500\$') #移除money這個串列中的1500\$這個元素
print(money)
``````

``````money = ['1500\$','1400\$','2100\$']
money.pop(1) #移除第1(2)個位置的值(1400\$)
print(money)
``````

insert中要輸入插入元素的位置以及元素內容，但若要一次插入大量的值的話，我比較建議用合併串列的概念去做。

``````money = ['1500\$','1400\$','2100\$','3100\$','5400\$','2500\$','4200\$']
money2 = ['2000\$','4500\$','1000\$']
money = money + money2
print(money)
``````

• sort()：排序串列
• reverse()：反轉串列
``````money = ['1500\$','1400\$','2100\$']
money.sort() #利用ASCII碼的字元大小將三個字串元素排列
print(money) #輸出為 [''1400\$','1500\$','2100\$']

money2 = ['1500\$','1400\$','2100\$']
money2.reverse() #反轉串列順序
print(money2) #輸出為 ['2100\$','1400\$','1500\$']
``````