Python数据结构与算法——数据入门

    技术2022-07-11  86

    数据入门

    原子数据类型

    >>> True True >>> False False >>> False or True True >>> not (False or True) False >>> True and True True

    >>> theSum=0 >>> theSum 0 >>> theSum=theSum+1 >>> theSum 1 >>> theSum=True >>> theSum True

    赋值语句theSum=0创建一个变量theSum并让它持有对数据对象0的引用。通常对语句右侧进行求值,并将对结果数据对象的引用"赋值"给左侧名称。如果数据的类型发生变化,那么变量的类型也是如此。赋值语句改变了变量所持有的引用。

    内置集合数据类型

    >>> [1,3,True,6.5] [1, 3, True, 6.5] >>> mylist=[1,3,True,6.5] >>> mylist [1, 3, True, 6.5]

    通过重复操作初始化一个列表

    >>> myList=[0]*6 >>> myList [0, 0, 0, 0, 0, 0] >>> myList=[1,2,3,4] >>> A=[myList]*3 >>> print(A) [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]] >>> myList[2]=45 >>> print(A) [[1, 2, 45, 4], [1, 2, 45, 4], [1, 2, 45, 4]]

    >>> myList=[1024,3,True,6.5] >>> myList.append(False) >>> print(myList) [1024, 3, True, 6.5, False] >>> myList.insert(2,4.5) >>> print(myList) [1024, 3, 4.5, True, 6.5, False] >>> print(myList.pop()) False >>> print(myList) [1024, 3, 4.5, True, 6.5] >>> print(myList.pop(1)) 3 >>> print(myList) [1024, 4.5, True, 6.5] >>> print(myList.pop(2)) True >>> print(myList) [1024, 4.5, 6.5] >>> myList.sort() >>> print(myList) [4.5, 6.5, 1024] >>> myList.reverse() >>> print(myList) [1024, 6.5, 4.5] >>> print(myList.count(6.5)) 1 >>> print(myList.index(4.5)) 2 >>> myList.remove(6.5) >>> print(myList) [1024, 4.5] >>> del myList[0] >>> print(myList) [4.5]

    range函数

    >>> range(10) range(0, 10) >>> list(range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> range(5,10) range(5, 10) >>> list(range(5,10)) [5, 6, 7, 8, 9] >>> list(range(5,10,2)) [5, 7, 9] >>> list(range(10,1,-1)) [10, 9, 8, 7, 6, 5, 4, 3, 2]

    字符串是由零个或多个字母、数字和其他符号组成的序列集合。我们称之为字母,数字和其他符号字符。通过使用引号(单引号或双引号)将中文字符串值与标识符区分开。

    >>> "David" 'David' >>> myName="David" >>> myName[3] 'i' >>> myName*2 'DavidDavid' >>> len(myName) 5

    字符串的其他方法

    >>> myName 'David' >>> myName.upper() 'DAVID' >>> myName.center(10) ' David ' >>> myName.find('v') 2 >>> myName.split('v') ['Da', 'id']

    列表与字符串之间的主要区别是,列表可以被修改,而字符串不能。

    >>> myList [1, 3, True, 6.5] >>> myList[0]=2**10 >>> myList [1024, 3, True, 6.5] >>> myName 'David' >>> myName[0]='X' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment

    元组与列表非常相似,但元组不可变。

    >>> myTuple=(2,True,4.96) >>> myTuple (2, True, 4.96) >>> len(myTuple) 3 >>> myTuple[0] 2 >>> myTuple*3 (2, True, 4.96, 2, True, 4.96, 2, True, 4.96) >>> myTuple[0:2] (2, True)

    修改元组之后的报错信息

    >>> myTuple[1]=False Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment >>>

    set是一个无序的,为空或是更多不可变Python数据对象集合。集合中的值不允许重复,以逗号分割,写在大括号中。空集合用set()表示。集合是异构的并且可以被分配给一个变量。

    >>> {3,6,"cat",4.5,False} {False, 3, 4.5, 'cat', 6} >>> mySet={3,6,"cat",4.5,False} >>> mySet {False, 3, 4.5, 'cat', 6}

    集合支持的操作

    >>> mySet {False, 3, 4.5, 'cat', 6} >>> len(mySet) 5 >>> False in mySet True >>> "dog" in mySet False

    集合的方法

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

    最后一个无序的Python集合——字典

    >>> capitals={'Iowa':'DesMoines','Wisconsin':'Madison'} >>> capitals {'Iowa': 'DesMoines', 'Wisconsin': 'Madison'} >>>

    通过键来访问值

    >>> capitals={'Iowa':'DesMoines','Wisconsin':'Madison'} >>> capitals {'Iowa': 'DesMoines', 'Wisconsin': 'Madison'} >>> capitals={'Iowa':'DesMoines','Wisconsin':'Madison'} >>> print(capitals['Iowa']) DesMoines >>> capitals['Utah']='SaltLakeCity' >>> print(capitals) {'Iowa': 'DesMoines', 'Wisconsin': 'Madison', 'Utah': 'SaltLakeCity'} >>> capitals['California']='Sacramento' >>> print(len(capitals)) 4 >>> for k in capitals: ... print(capitals[k]," is the capital of ",k) ... DesMoines is the capital of Iowa Madison is the capital of Wisconsin SaltLakeCity is the capital of Utah Sacramento is the capital of California

    字典上没有特定的顺序

    >>> phoneext={'david':1410,'brad':1137} >>> phoneext {'david': 1410, 'brad': 1137} >>> phoneext.keys() dict_keys(['david', 'brad']) >>> list(phoneext.keys()) ['david', 'brad'] >>> phoneext.values() dict_values([1410, 1137]) >>> list(phoneext.values()) [1410, 1137] >>> phoneext.items() dict_items([('david', 1410), ('brad', 1137)]) >>> list(phoneext.items()) [('david', 1410), ('brad', 1137)] >>> phoneext.get("kent") >>> phoneext.get("kent","NO ENTRY") 'NO ENTRY'

    Processed: 0.011, SQL: 9