python里元组和列表的共同点和不同点_Python_列表,元组和字典的异同
1,列表:list
可變的數(shù)據(jù)類型,可以被改變,可以進(jìn)行嵌套處理,可在一個(gè)列表中存儲(chǔ)一個(gè)序列的項(xiàng)目
指明一個(gè)列表的方法是:使用方括號(hào)
代碼示例:
>>> fruit_list = ['apple', 'pear', 'orange', 'banana', 'watermetton', 'strawberry']>>> lenrth =len(fruit_list)>>>print lenrth6
>>> for items infruit_list:
print items,
apple pear orange banana watermetton strawberry>>> fruit_list.append('pawpaw') ###添加一個(gè)元素>>> fruit_list.del('apple') ###列表中刪除一個(gè)元素不能用.del(),而是.remove()
SyntaxError: invalid syntax>>> fruit_list.remove('aple')
Traceback (most recent call last):
File"", line 1, in fruit_list.remove('aple')
ValueError: list.remove(x): x notinlist>>> fruit_list.remove('apple')>>>fruit_list.pop()'pawpaw'
>>> fruit_list.count('apple') ###.count()函數(shù)不是統(tǒng)計(jì)列表的長(zhǎng)度,而是查找列表中某個(gè)元素出現(xiàn)的次數(shù),Len(list)用于計(jì)算列表長(zhǎng)度0
>>>print fruit_list
['pear', 'orange', 'banana', 'watermetton', 'strawberry']
2,元組
和列表類似,但是元組是不可修改的,可以進(jìn)行嵌套
指明一個(gè)元組的方法:使用圓括號(hào),中間使用“ , ”將項(xiàng)目分隔
創(chuàng)建一個(gè)元組:使用逗號(hào)分隔,或者使用tuple()函數(shù) eg:1,2,3 ;tuple([1,2,3])
代碼示例:
>>> animal_tuple = ('fish', 'snake', 'wolf',('dog', 'cat', 'sheap'))>>>len(animal_tuple) ##求解元組的長(zhǎng)度4
>>> 1,2,3 ##創(chuàng)建一個(gè)元組(1, 2, 3)>>> tuple([1,2,3])
(1, 2, 3)>>>
>>> animal_tuple.append('mouse') ##元組是不可修改的
Traceback (most recent call last):
File"", line 1, in animal_tuple.append('mouse')
AttributeError:'tuple' object has no attribute 'append'
>>> animal_tuple.index('dog') ##嵌套在元組內(nèi)的元組元素?zé)o法檢索到!!!!!
Traceback (most recent call last):
File"", line 1, in animal_tuple.index('dog')
ValueError: tuple.index(x): x notintuple>>> animal_tuple.index('fish') ##元組的開始元素的標(biāo)號(hào)是00
3,字典
把鍵和值聯(lián)系在一起,其中鍵必須是唯一的,鍵和值之間用冒號(hào):進(jìn)行分隔
字典的表示方法:使用{},鍵和值之間用冒號(hào)“:”隔開,項(xiàng)之間用逗號(hào)“,”隔開
代碼示例:
>>> phonebook_dict = {'Alice': '23456', 'Tom':'67890', 'Snowy': '67845'} ##創(chuàng)建一個(gè)字典>>>phonebook_dict
{'Snowy': '67845', 'Alice': '23456', 'Tom': '67890'}>>> items = [('Alice', '23456'), ('Tom','67890'), ('Snowy', '67845')] ##列表轉(zhuǎn)化為字典>>> items_dict =dict(items)>>>items_dict
{'Snowy': '67845', 'Alice': '23456', 'Tom': '67890'}>>>len(items_dict)3
>>> change_items = {'Robin': '55667'}>>>items_dict.update(change_items) ##.update()更新字典內(nèi)容>>>items_dict
{'Snowy': '67845', 'Alice': '23456', 'Robin': '55667', 'Tom': '67890'}>>>items_dict.items() ##字典轉(zhuǎn)化為列表.items()函數(shù)
[('Snowy', '67845'), ('Alice', '23456'), ('Robin', '55667'), ('Tom', '67890')]
4:總結(jié)
把序列轉(zhuǎn)化為列表:.list(seq) [1,2,3,4] >>>>.append() .remove() .pop() .index() .count() len()
把序列轉(zhuǎn)化為元組:.tuple(seq) (1,2,3,4) >>>>>.index() .count() len()
把序列轉(zhuǎn)化為字典: .dict(seq) {'1' : 'Snowy', '2':'Tom', '3': 'Alice'} >>>>.update() .pop() len()
字典轉(zhuǎn)化為列表: items(dict)
PS:string相當(dāng)于字符元組
array:只能存儲(chǔ)同種數(shù)據(jù)類型的數(shù)據(jù),區(qū)別于list,相比較list使用的空間更小,通過 from array import array 可以使用array模塊
zip()是Python的一個(gè)內(nèi)建函數(shù),它接受一系列可迭代的對(duì)象作為參數(shù),將對(duì)象中對(duì)應(yīng)的元素打包成一個(gè)個(gè)tuple(元組),然后返回由這些tuples組成的list(列表)
code:
>>> a = ['1', '2']>>> b = ['a', 'c', 'd']>>>zip(a,b)
[('1', 'a'), ('2', 'c')]>>> zip(*zip(a,b))
[('1', '2'), ('a', 'c')]>>> a = [[1,2,3],[4,5,6],[7,8,9]]>>>zip(a)
[([1, 2, 3],), ([4, 5, 6],), ([7, 8, 9],)]>>> print [row[0] for row ina]
[1, 4, 7]>>> zip(*a)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
(*)操作符與zip函數(shù)配合可以實(shí)現(xiàn)與zip相反的功能,即將合并的序列拆成多個(gè)tuple。
總結(jié)
以上是生活随笔為你收集整理的python里元组和列表的共同点和不同点_Python_列表,元组和字典的异同的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: layui弹出层html,layui-弹
- 下一篇: python制作简单网页_python