python里元组和列表的共同点和不同点_Python_列表,元组和字典的异同
1,列表:list
可變的數(shù)據(jù)類型,可以被改變,可以進(jìn)行嵌套處理,可在一個列表中存儲一個序列的項目
指明一個列表的方法是:使用方括號
代碼示例:
>>> 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') ###添加一個元素>>> fruit_list.del('apple') ###列表中刪除一個元素不能用.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)計列表的長度,而是查找列表中某個元素出現(xiàn)的次數(shù),Len(list)用于計算列表長度0
>>>print fruit_list
['pear', 'orange', 'banana', 'watermetton', 'strawberry']
2,元組
和列表類似,但是元組是不可修改的,可以進(jìn)行嵌套
指明一個元組的方法:使用圓括號,中間使用“ , ”將項目分隔
創(chuàng)建一個元組:使用逗號分隔,或者使用tuple()函數(shù) eg:1,2,3 ;tuple([1,2,3])
代碼示例:
>>> animal_tuple = ('fish', 'snake', 'wolf',('dog', 'cat', 'sheap'))>>>len(animal_tuple) ##求解元組的長度4
>>> 1,2,3 ##創(chuàng)建一個元組(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)號是00
3,字典
把鍵和值聯(lián)系在一起,其中鍵必須是唯一的,鍵和值之間用冒號:進(jìn)行分隔
字典的表示方法:使用{},鍵和值之間用冒號“:”隔開,項之間用逗號“,”隔開
代碼示例:
>>> phonebook_dict = {'Alice': '23456', 'Tom':'67890', 'Snowy': '67845'} ##創(chuàng)建一個字典>>>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:只能存儲同種數(shù)據(jù)類型的數(shù)據(jù),區(qū)別于list,相比較list使用的空間更小,通過 from array import array 可以使用array模塊
zip()是Python的一個內(nèi)建函數(shù),它接受一系列可迭代的對象作為參數(shù),將對象中對應(yīng)的元素打包成一個個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相反的功能,即將合并的序列拆成多個tuple。
總結(jié)
以上是生活随笔為你收集整理的python里元组和列表的共同点和不同点_Python_列表,元组和字典的异同的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: layui弹出层html,layui-弹
- 下一篇: react安装_前端大牛进阶---gt;