1. 程式人生 > 實用技巧 >操作列表

操作列表

  • Q:列印所有的魔術師 遍歷列表
  • A:使用for迴圈
#下面是一個演示
magicians = ['alice','david','carolina']
for magician in magicians:
    print(magician)
--------------------------------------------------------------------
alice
david
carolina
#下面深入的說一下迴圈
for magician in magicians:    # 這行程式碼讓python獲取列表magicians的第一個值 並將其儲存到變數magician中
    print(magician)           # 它讓python列印magician   鑑於列表中還包含其他值  python返回到迴圈的第一行   依次繼續列印下去

  • Q:在for迴圈中執行更多的操作
  • A:每個縮排的程式碼都是迴圈的一部分
magicians = ['alice','david','carolina']
for magician in magicians:
    print(magician.title() + ",that was a great trick!")
    print("I can't wait to see your next trick," + magician.title() +'.\n')
--------------------------------------------------------------------
Alice,that was a great trick!
I can't wait to see your next trick,Alice.


David,that was a great trick!
I can't wait to see your next trick,David.


Carolina,that was a great trick!
I can't wait to see your next trick,Carolina.

  • Q:在for迴圈結束後執行一些操作
  • A:沒有所進的程式碼都執行一次,不會重複操作
magicians = ['alice','david','carolina']
for magician in magicians:
    print(magician.title() + ",that was a great trick!")
    print("I can't wait to see your next trick," + magician.title() +'.\n')
print('Thank you,everyone. That was a great magic show!')

  • Q:縮排錯誤
  • A:
    1. 忘記縮排
    2. 忘記縮排額外的程式碼行
    3. 不必要的縮排
    4. 迴圈後不必要的縮排
    5. 遺漏了冒號
# 1忘記縮排
magicians = ['alice','david','carolina']
for magician in magicians:
print(magician.title() + ",that was a great trick!")

# 2忘記縮排額外的程式碼行
magicians = ['alice','david','carolina']
for magician in magicians:
    print(magician.title() + ",that was a great trick!")
print("I can't wait to see your next trick," + magician.title() +'.\n')

# 3不必要的縮排
magicians = ['alice','david','carolina']
    print(magicians[0])

# 4迴圈後不必要的縮排
magicians = ['alice','david','carolina']
for magician in magicians:
    print(magician.title() + ",that was a great trick!")
    print("I can't wait to see your next trick," + magician.title() +'.\n')
    print('Thank you,everyone. That was a great magic show!')

# 5遺漏了冒號
for magician in magicians
    print(magician.title() + ",that was a great trick!")

  • Q:建立數字列表
  • A:
    1. 使用函式range()”建立數字列表“
    2. 對列表進行統計運算   函式max() min() sum()
    3. 列表解析   將for迴圈和建立列表的程式碼合併為一行
# 下面是使用函式range()建立列表
print(list(range(1,5)))
# 注意range()函式是生成一些整數 這裡藉助list函式將這些整數排成一個列表

# 下面是對列表統計運算的例子
>>>number = [1,2,3,4,5,6]
>>>min(number)
1
>>>max(number)
6
>>>sum(number)
21

# 下面是解析列表的一行程式碼示例
squares = [value**2 for value in range(1,10)]
print(squares)

  • Q:列表切片
  • A:[0:0]
players = ['charles','martina','michael','florence','eli']
print(players[0:3]) # 前三
print(platyer[:4])   # 前四
print(platyer[2:])  # 第二位後面的
print(platyer[-3:])  # 後三個
print(platyer[:])    # 全部

Q:遍歷切片
A:和遍歷列表操做一樣

  • Q:複製列表
  • A:使用切片複製列表
my_foods = ['pizza','falafel','carrot']
friends_food = my_foods[:]
print(friends_foods)
# 下面我們核實一下
my_foods.append('cannoli')
friends_food.append('ice cream')
# 再分別列印即可

# 下面是一種直接的錯誤思想
my_foods = ['pizza','falafel','carrot']
friends_food = my_foods
my_foods.append('cannoli')
friends_food.append('ice cream')
print(my_foods)
print(friends_food)
# 事實上這裡讓python將新變數friends_foods關聯到包含在my_foods中的列表,兩個變數都指向一個列表