Python数据类型(列表和元组)
1.3 List(列表)
列表由一系列按特定順序排列的元素組成。
在Python中,用方括號(hào)[ ]來(lái)表示列表,并用逗號(hào)來(lái)分隔其中的元素。
1.3.1 訪問(wèn)列表元素
在Python中,第一個(gè)列表元素的索引為0,而不是1。
>>> bicycles = ['trek', 'cannondale', 'redline', 'specialized'] >>> print(bicycles[0]) trek >>> print(bicycles[3]) specialized >>> print(bicycles[-1]) specialized注意:通過(guò)將索引指定為-1,可讓Python返回最后一個(gè)列表元素。索引-2返回倒數(shù)第二個(gè)列表元素,索引-3返回倒數(shù)第三個(gè)列表元素,以此類(lèi)推。
1.3.2 添加元素
>>> motorcycles = ['honda', 'yamaha', 'suzuki'] >>> print(motorcycles) ['honda', 'yamaha', 'suzuki'] >>> motorcycles.append('ducati') >>> print(motorcycles) ['honda', 'yamaha', 'suzuki', 'ducati']1.3.3 修改元素
>>> motorcycles = ['honda', 'yamaha', 'suzuki'] >>> print(motorcycles) ['honda', 'yamaha', 'suzuki'] >>> motorcycles[0] = 'ducati' >>> print(motorcycles) ['ducati', 'yamaha', 'suzuki']1.3.4 在列表中插入元素
>>> motorcycles = ['honda', 'yamaha', 'suzuki'] >>> motorcycles.insert(1, 'ducati') >>> print(motorcycles) ['honda', 'ducati', 'yamaha', 'suzuki']1.3.5 刪除元素
>>> motorcycles = ['honda', 'yamaha', 'suzuki'] >>> print(motorcycles) ['honda', 'yamaha', 'suzuki'] >>> del motorcycles[1] >>> print(motorcycles) ['honda', 'suzuki']彈出列表元素
>>> motorcycles = ['honda', 'yamaha', 'suzuki'] >>> popped_motorcycle = motorcycles.pop() >>> print(popped_motorcycle) suzuki >>> print(motorcycles) ['honda', 'yamaha'] >>> print(motorcycles.pop(0)) honda >>> print(motorcycles) ['yamaha']有時(shí)候,你不知道要從列表中刪除的值所處的位置。如果你只知道要?jiǎng)h除的元素的值,可使用方法remove()。
>>> motorcycles = ['honda', 'yamaha', 'suzuki'] >>> print(motorcycles) ['honda', 'yamaha', 'suzuki'] >>> motorcycles.remove('yamaha') >>> print(motorcycles) ['honda', 'suzuki']1.3.6 對(duì)列表排序
>>> cars = ['bmw', 'audi', 'toyota', 'subaru'] >>> cars.sort() >>> print(cars) ['audi', 'bmw', 'subaru', 'toyota'] >>> print(len(cars)) 41.3.7 遍歷列表
>>> magicians = ['alice', 'david', 'carolina'] >>> for magician in magicians:print(magician)alice david carolina >>>1.3.8 創(chuàng)建數(shù)值列表
>>> for value in range(1,5):print(value)1 2 3 4 >>> >>> numbers = list(range(1,6)) >>> print(numbers) [1, 2, 3, 4, 5] >>> even_numbers = list(range(2,11,2)) >>> print(even_numbers) [2, 4, 6, 8, 10]對(duì)數(shù)字列表執(zhí)行簡(jiǎn)單的統(tǒng)計(jì)計(jì)算
>>> digits = [1,2,3,4,5,6,7,8,9,0] >>> min(digits) 0 >>> max(digits) 9 >>> sum(digits) 451.3.9 列表生成式(List Comprehensions)
>>> squares = [value**2 for value in range(1,11)] >>> print(squares) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] >>> [x * x for x in range(1, 11) if x % 2 == 0] [4, 16, 36, 64, 100] >>> [m + n for m in 'ABC' for n in 'XYZ'] ['AX', 'AY', 'AZ', 'BX', 'BY', 'BZ', 'CX', 'CY', 'CZ'] >>> d = {'x': 'A', 'y': 'B', 'z': 'C' } >>> [k + '=' + v for k, v in d.items()] ['x=A', 'y=B', 'z=C'] >>> L = ['Hello', 'World', 'IBM', 'Apple'] >>> [s.lower() for s in L] ['hello', 'world', 'ibm', 'apple']1.3.10 使用列表的一部分
你可以處理列表的部分元素,Python稱之為切片。
要?jiǎng)?chuàng)建切片,可指定要使用的第一個(gè)元素和最后一個(gè)元素的索引。
復(fù)制列表
my_foods = ['pizza', 'falafel', 'carrot cake'] friend_foods = my_foods[:] # 使用[:]來(lái)復(fù)制列表my_foods.append('cannoli') friend_foods.append('ice cream')print("My favorite foods are:") print(my_foods)print("\nMy friend's favorite foods are:") print(friend_foods)# My favorite foods are: # ['pizza', 'falafel', 'carrot cake', 'cannoli'] # # My friend's favorite foods are: # ['pizza', 'falafel', 'carrot cake', 'ice cream']1.4 Tuple(元組)
Python將不能修改的值稱為不可變的,而不可變的列表稱為元組。
元組看起來(lái)猶如列表,但使用圓括號(hào)而不是方括號(hào)來(lái)標(biāo)識(shí)。
元組中只包含一個(gè)元素時(shí),需要在元素后面添加逗號(hào),否則括號(hào)會(huì)被當(dāng)作運(yùn)算符使用。
參考資料:
- Python3 教程 | 菜鳥(niǎo)教程
- Python教程 - 廖雪峰的官方網(wǎng)站
- 《Python編程從入門(mén)到實(shí)踐》——【美】Eric Matthes 著
轉(zhuǎn)載于:https://www.cnblogs.com/gzhjj/p/10658224.html
總結(jié)
以上是生活随笔為你收集整理的Python数据类型(列表和元组)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 关于ConcurrentHashMap在
- 下一篇: Linux基础命令--date