列表的比較,列表比較只比較第一個元素(下標為0的那個元素)
>>> list1=[123] >>> list2=[234] >>> list3=[345] >>> list1>list2 False >>> list1<list2 True >>>
當列表中有多個元素的時候,還是僅僅只比較了第一個元素。
>>> list1=[123] >>> list2=[234] >>> list3=[345] >>> list2.append(121) >>> list1>list2 False >>> list1<list2 True >>> list2 [234, 121] >>> list1.append(234) >>> list1>list2 False >>>
其實列表也是 可以像字符串那樣的進行拼接的,例如:我們有一個list=[1,2,3] list2 = [4,5,6] list3 = list + list2
>>> list=[1,2,3] >>> list2 = [4,5,6] >>> list3 = list + list2 >>> list3 [1, 2, 3, 4, 5, 6] >>>
由於“+”號兩邊需要是同一類型的元素,所以不可這樣操作:list+'你好'
>>> lisr4 = list+"你好" Traceback (most recent call last): File "<pyshell#91>", line 1, in <module> lisr4 = list+"你好" TypeError: can only concatenate list (not "str") to list >>>
既然可以用+號那么可不可以用*號呢,是的,可以的
>>> list [1, 2, 3] >>> list *3 [1, 2, 3, 1, 2, 3, 1, 2, 3] >>> list [1, 2, 3] >>> list *=3 >>> list [1, 2, 3, 1, 2, 3, 1, 2, 3] >>>
那么我們想知道一個元素是否在list內,該如何做呢,當元素以獨立個體存在列表內,那么我們通過 elment in list即可判斷,或者 elment not in list 來判斷,那么如果元素在list內部的list,這通過二維數組方式進行判斷
>>> list=[1,2,'a',['我','愛'],'c'] >>> >>> 'a' in list True >>> >>> 'b' not in list True >>> >>> '我' in list False >>> '我' in list[3] True >>>
count(elment),這個方法用來查詢元素出現在列表中的次數。
>>> list [1, 2, 'a', ['我', '愛'], 'c'] >>> >>> list.append(1) >>> list [1, 2, 'a', ['我', '愛'], 'c', 1] >>> >>> list.count(1) 2 >>> list.count(2) 1 >>>
index(elment),是獲取元素在列表中的位置。
