python基礎-list


# 列表由一系列按特定順序排列的元素組成。可以將任何東西加入列表中。python用[]表示列表.
# 列表是有序集合
# 鑑於列表通常包含多個元素,給列表指定一個表示複數的名稱(如letters、 digits或names).

bicycles = ['trek', 'giant', 'media']
print(bicycles)


# 1.訪問列表元素
print(bicycles[0])  # 通過索引訪問
print(bicycles[-1])  # 索引爲-1時,訪問最後一個元素,-2爲倒數第二個元素

# P33動手試一試
# 3-1
names = ['A', 'B', 'C']
for name in names:
    print(name)
# 3-2
for name in names:
    print('hello %s' % name)
# 3-3
ways = ['Moto', 'Car', 'Bus']
for way in ways:
    print('I would like to own a %s' % way)


# 2.修改、添加和刪除元素
# 2.1修改列表元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print('修改前',motorcycles)
motorcycles[0] = 'ducati'
print('修改後', motorcycles)

# 2.2在列表中添加元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print('添加前:', motorcycles)
motorcycles.append('ducati')
print('在末尾添加後:', motorcycles)

# 2.2.2在列表中插入元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print('添加前:', motorcycles)
motorcycles.insert(0, 'ducati')
print('插入後', motorcycles)


# 2.3從列表中刪除元素
# 2.3.1使用del語句刪除元素,從列表中刪除後,就無法再訪問它了
motorcycles = ['honda', 'yamaha', 'suzuki']
print('刪除前:', motorcycles)
del motorcycles[0]
print('使用del刪除後:', motorcycles)

# 2.3.2使用方法pop()刪除元素,會刪除最後一個元素,依然能夠訪問被刪除的值
motorcycles = ['honda', 'yamaha', 'suzuki']
print('刪除(彈出)前:', motorcycles)
popped_motorcycle = motorcycles.pop()
print('被pop()刪除的元素', popped_motorcycle)
print('使用pop()刪除後:', motorcycles)
# 2.3.3彈出列表中任何位置的元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print('刪除(彈出)前:', motorcycles)
first_owned = motorcycles.pop(0)
print('刪除(彈出)後:', motorcycles)

# 2.3.4根據值刪除元素,使用remove()方法
'''
方法remove()只刪除第一個指定的值。如果要刪除的值可能在列表中出現多次,
就需要使用循環來判斷是否刪除了所有這樣的值。
'''
motorcycles = ['honda', 'yamaha', 'suzuki']
print('刪除前:', motorcycles)
motorcycles.remove('honda')
print('刪除後:', motorcycles)

# P38動手試一試
# 3-4
famouse_men = ['Newton', 'Jack', 'Polato']
for man in famouse_men:
    print(man)
print("Jack can't join the party.")
famouse_men[1] = 'Mary'
for man in famouse_men:
    print(man)

# 3-6
print('找到了一個更大的餐桌')
famouse_men.insert(0, 'Trump')
famouse_men.insert(2, 'Lucy')
famouse_men.append('LeCunn')
for man in famouse_men:
    print('Hi,%s I hopy you can join the party' % man)

# 3-7
print('I can invite only two men')
men1 = famouse_men.pop()
print('I am so sorry %s, you can not join the party.' % men1)
men2 = famouse_men.pop()
print('I am so sorry %s, you can not join the party.' % men2)
men3 = famouse_men.pop()
print('I am so sorry %s, you can not join the party.' % men3)
men4 = famouse_men.pop()
print('I am so sorry %s, you can not join the party.' % men4)

for man in famouse_men:
    print('%s,you are still invited' % man)

del famouse_men[0]
del famouse_men[0]
print(famouse_men)


# 3.組織列表
# 3.1使用方法sort()對列表進行永久性排序
cars = ['bmw', 'telsa', 'audi', ' toyata']
print('排序前', cars)
cars.sort() # 永久性地修改了列表元素的排列順序,sort(reverse=True)
print('排序後', cars)

# 3.2使用函數sorted()對列表進行臨時排序
cars = ['bmw', 'telsa', 'audi', ' toyata']
print('臨時排序前', cars)
print('臨時排序後', sorted(cars)) # sorted(cars, reverse=True)
print('順序依舊不變', cars)

# 3.3倒着打印列表,使用方法reverse(),永久性改變
cars = ['bmw', 'telsa', 'audi', ' toyata']
print('倒着排序前', cars)
cars.reverse()
print('倒着排序後', cars)

# 3.4確定列表的長度
print('長度爲', len(cars))

# P41動手試一試
# 3-8
dests = ['American', 'German', 'Austurlia', 'England', 'French']
print('我想去的目的地', dests)
print('用sorted()排序後', sorted(dests))
print('用sorted()反轉排序後', sorted(dests, reverse=True))
dests.reverse()
print('使用reverse()反轉', dests)
dests.reverse()
print('再次使用reverse()反轉', dests)
dests.sort()
print('使用sort()排序後', dests)
dests.sort(reverse=True)
print('使用sort(reverse=True)排序後', dests)

# 3-9
print(len(dests))

# 3-10
animals = ['cat', 'frog', 'dog', 'mice']
print(animals)
animals.append('tiger')
animals.insert(0, 'lion')
animals.pop()
animals[0] = 'pig'
animals.sort()
animals.reverse()
sorted(animals)
print(animals)
animals.remove('cat') ## 記住這個方法,不是animals.remove['cat'],不是animals.remove[0]
del animals[0]
print(animals)


# 3.4使用列表時避免索引錯誤
motorcycles = ['honda', 'yamaha', 'suzuki']
#print(motorcycles[3])
# 始終使用-1訪問列表的最後一個元素,僅當列表爲空時,纔出錯
print(motorcycles[-1])
# 發生索引錯誤卻找不到解決辦法時,請嘗試將列表或其長度打印出來。



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章