Python 中 的list,string, tuple, set,dictionary幾種數(shù)據(jù)類型

lists can be modified but strings can not be modified. For example,

>>> My_idle_name = 'Lebron James'
>>> My_idle_name
'Lebron James'
>>> Name_list = list(My_idle_name)
>>> Name_list
['L', 'e', 'b', 'r', 'o', 'n', ' ', 'J', 'a', 'm', 'e', 's']
>>> My_idle_name[0]='C'
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    My_idle_name[0]='C'
TypeError: 'str' object does not support item assignment
>>> Name_list[0]='C'
>>> Name_list
['C', 'e', 'b', 'r', 'o', 'n', ' ', 'J', 'a', 'm', 'e', 's']

元組和列表一樣,它是有序的,可以包含多種數(shù)據(jù)類型,用()來表示。作為一個(gè)序列,元組也可以使用序列的操作,比如slice (:), * 等等,但是,元組中的元素不能改變。例如:

>>> MyTuple = ('a', 1, [1,2], True)
>>> MyTuple
('a', 1, [1, 2], True)
>>> MyTuple[0]='b'
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    MyTuple[0]='b'
TypeError: 'tuple' object does not support item assignment
>>> MyTuple*2
('a', 1, [1, 2], True, 'a', 1, [1, 2], True)
>>> MyTuple[0:-1]
('a', 1, [1, 2])

集合是無(wú)序的,用{ } 來表示。因?yàn)榧鲜菬o(wú)序的,所以不能用index 來索引。集合不能被看成是一個(gè)數(shù)列,但是它仍然支持一些運(yùn)算,如下表所示:



例子如下:

>>> MySet = {'a', 1, False}
>>> MySet
{False, 1, 'a'}
>>> MySet[1]
Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    MySet[1]
TypeError: 'set' object does not support indexing
>>> len(MySet)
3
>>> 'a' in MySet
True
>>> MySet2 = {1,'b',True}
>>> MySet|MySet2
{False, 1, 'b', 'a'}
>>> MySet
{False, 1, 'a'}
>>> MySet2
{1, 'b'}
>>> MySet2 = {1,2,3,'Kyrie Irving'}
>>> MySet|MySet2
{False, 1, 2, 3, 'a', 'Kyrie Irving'}
>>> MySet
{False, 1, 'a'}
>>> MySet2
{1, 2, 3, 'Kyrie Irving'}
>>> MySet&MySet2
{1}
>>> MySet-MySet2
{False, 'a'}
>>> MySet<=MySet2
False
>>> 

集合本身也有自己的運(yùn)算,子交并補(bǔ)是數(shù)學(xué)中最常見的集合運(yùn)算。

set methods.JPG

代碼如下:Note: 下面代碼為https://runestone.academy/runestone/static/pythonds/Introduction/GettingStartedwithData.html#tab-setmethods
中的代碼

>>> mySet
{False, 4.5, 3, 6, 'cat'}
>>> yourSet = {99,3,100}
>>> mySet.union(yourSet)
{False, 4.5, 3, 100, 6, 'cat', 99}
>>> mySet | yourSet
{False, 4.5, 3, 100, 6, 'cat', 99}
>>> mySet.intersection(yourSet)
{3}
>>> mySet & yourSet
{3}
>>> mySet.difference(yourSet)
{False, 4.5, 6, 'cat'}
>>> mySet - yourSet
{False, 4.5, 6, 'cat'}
>>> {3,100}.issubset(yourSet)
True
>>> {3,100}<=yourSet
True
>>> mySet.add("house")
>>> mySet
{False, 4.5, 3, 6, 'house', 'cat'}
>>> mySet.remove(4.5)
>>> mySet
{False, 3, 6, 'house', 'cat'}
>>> mySet.pop()
False
>>> mySet
{3, 6, 'house', 'cat'}
>>> mySet.clear()
>>> mySet
set()
>>>

字典有鍵和鍵值key-value.

teams = {'Cleveland':'Cavaliers','Houston':'Rockets'}
teams['Golden States']='Warriors'
teams
 {'Cleveland': 'Cavaliers', 'Houston': 'Rockets', 'Golden States': 'Warriors'}
teams['Cleveland']
 'Cavaliers'
for i in teams:
    print('The team of ', i, ' is ', teams[i])
    
The team of  Cleveland  is  Cavaliers
The team of  Houston  is  Rockets
The team of  Golden States  is  Warriors
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容