1. 程式人生 > >學習筆記-小甲魚Python3學習第十二講:一個打了激素的數組3

學習筆記-小甲魚Python3學習第十二講:一個打了激素的數組3

!= 查找 lse pre 表達 如果 eof ssh 連接

列表的一些常用操作符:

比較操作符、邏輯操作符、連接操作符、重復操作符、成員關系操作符

比較操作符:

>>> list1 = [123,456]

>>> list2 = [234,123]

>>> list1 > list2 #只比較第一個元素值的大小

False

>>> list3 = [123,456]

>>> (list1 < list2) and (list1 == list3)

True

>>> list4 = list1 + list2 #相當於append(),但不提倡這種方式,因為'+'是拼接符號,如果元素類型不一樣是會報錯的,建議如果追加元素使用append()

>>> list4

[123, 456, 234, 123]

>>> list3

[123, 456]

>>> list3 * 3 #把list3的元素打印3次,list3本身的值不會變

[123, 456, 123, 456, 123, 456]

>>> list3

[123, 456]

>>> list3 *= 3 相當於list3 = list3 * 3,把list3的元素3倍增加後再賦值給list3,list3的值會變化

>>> list3

[123, 456, 123, 456, 123, 456]


成員關系操作符:in 和 not in

>>> list5 = [123,['張三','李四'],456]

>>> 123 in list5

True

>>> '張三' in list5

False

>>> '張三' not in list5

True

>>> '張三' in list5[1] #如果要引用列表中的列表,需要在列表後加上索引值

True

>>> list5[1][1] #打印列表中的列表中的元素

'李四'


列表類型的內置函數

>>> dir(list)

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']


count:計算參數在列表中出現的次數

>>> list3

[123, 456, 123, 456, 123, 456]

>>> list3.count(123) #計算123在列表list3宗出現多少次

3


index:返回參數在列表中的位置

>>> list3.index(456)

1

>>> list3.index(123,1,4) #從索引值1開始到索引值3(=4-1)查找參數123所在的位置(索引)

2


reverse:將整個列表翻轉,是將列表中元素逆序排列

>>> list3.reverse()

>>> list3

[456, 123, 456, 123, 456, 123]


sort:用指定方式把列表元素進行排序

>>> list6 = [4,5,9,3,7,10,1,8]

>>> list6.sort() #默認是將列表中元素從小到大排序或按字母順序排序

>>> list6

[1, 3, 4, 5, 7, 8, 9, 10]

>>> list6.sort(reverse=True) #默認reverse=False,如果reverse=True,那麽元素按大小排列後,會再進行逆序排列

>>> list6

[10, 9, 8, 7, 5, 4, 3, 1]


使用分片創建列表的拷貝

>>> list6

[10, 9, 8, 7, 5, 4, 3, 1]

>>> list7 = list6[:] #把list6拷貝一份出來,賦值給list7

>>> list8 = list6 #變量list8指向list6的值,list8的值會隨著list6的變化而變化

>>> list6.sort()

>>> list6

[1, 3, 4, 5, 7, 8, 9, 10]

>>> list7

[10, 9, 8, 7, 5, 4, 3, 1]

>>> list8

[1, 3, 4, 5, 7, 8, 9, 10]


---------------------分割線,哈哈哈----------------------

二、課後作業:

0.註意,這道題跟上節課的那道題有點兒不同,回答完請上機實驗或參考答案。

old = [1, 2, 3, 4, 5]

new = old

old = [6]

print(new)

如果不上機操作,你覺得會打印什麽內容?

[1,2,3,4,5] 因為變量old是重新賦值,而不是修改原有的元素,new還是指向的原存儲區域


1.請問如何將下邊這個列表的'小甲魚'修改為'小魷魚'?

list1 = [1, [1, 2, ['小甲魚']], 3, 5, 8, 13, 18]

>>> list1[1][2][0] = '小魷魚'

>>> list1

[1, [1, 2, ['小魷魚']], 3, 5, 8, 13, 18]


2.要對一個列表進行順序排序,請問使用什麽方法?

假使列表名為list2,對list2進行排序,list2.sort()


3.要對一個列表進行逆序排序,請問使用什麽方法?

假使列表名為list2,對其進行逆序排序

list2.sort() 先正向排序

list2.reverse() 然後再逆向排序

或著

list2.sort(reverse=True) 一步到位


4.列表還有兩個內置方法沒給大家介紹,不過聰明的你應該可以自己摸索使用的門道吧:copy() 和 clear()

>>> list1

[1, [1, 2, ['小魷魚']], 3, 5, 8, 13, 18]

>>> list2 = list1.copy()

>>> list2

[1, [1, 2, ['小魷魚']], 3, 5, 8, 13, 18]

>>> list1.clear()

>>> list1

[]


5.你有聽說過列表推導式或列表解析嗎?

列表推導式(List comprehensions)也叫列表解析,靈感取自函數式編程語言 Haskell。Ta 是一個非常有用和靈活的工具,可以用來動態的創建列表,語法如:

[有關A的表達式 for A in B]

>>> list1 =[x**2 for x in range(10)]

>>> list1

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

相當於

>>> list1 = []

>>> for x in range(10):

list1.append(x**2)

>>> list1

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

問題:請先在 IDLE 中獲得下邊列表的結果,並按照上方例子把列表推導式還原出來。

>>> list1 = [(x, y) for x in range(10) for y in range(10) if x%2==0 if y%2!=0]

結果是

>>> list1

[(0, 1), (0, 3), (0, 5), (0, 7), (0, 9), (2, 1), (2, 3), (2, 5), (2, 7), (2, 9), (4, 1), (4, 3), (4, 5), (4, 7), (4, 9), (6, 1), (6, 3), (6, 5), (6, 7), (6, 9), (8, 1), (8, 3), (8, 5), (8, 7), (8, 9)]

推導式為:

>>> list1 =[]

>>> for x in range(10):

for y in range(10):

if x%2 == 0 and y%2 != 0:

list1.append((x,y))

>>> list1

[(0, 1), (0, 3), (0, 5), (0, 7), (0, 9), (2, 1), (2, 3), (2, 5), (2, 7), (2, 9), (4, 1), (4, 3), (4, 5), (4, 7), (4, 9), (6, 1), (6, 3), (6, 5), (6, 7), (6, 9), (8, 1), (8, 3), (8, 5), (8, 7), (8, 9)]


學習筆記-小甲魚Python3學習第十二講:一個打了激素的數組3