python 字典,列表,元組__python

來源:互聯網
上載者:User

基礎操作:

##########################列表#############################aList = [1,2,3,4,5]#python 中 list 是可變類型,像下面這樣修改是合法的#增aList.append(6)#刪aList.pop()#改aList[0] = 100#查print(aList[0])for v in aList:    print(v)##############元組##########################aTuple = (1,2,3,4,5)#在python 元組是不可變類型,不可修改,像下面的操作時非法的# aTuple[0] = 2#查for v in aTuple:    print(v)################字典#########################aDict = {"name":"zhengjinwei","age":23,"sex":"male"}#在python 中字典是可以修改的#增aDict["index"] = 1#刪aDict.pop("index")aDict.clear()#改aDict["name"]="bloodchilde"#查for index,k in enumerate(aDict):    print index,k,aDict[k]

進階操作,實現棧:

#python 中list 實現棧class CStack:    def __init__(self):        self.__len = 0        self.__stack = []    def __del__(self):        del self.__stack    def push(self,obj):        self.__stack.append(obj)        self.__len += 1    def pop(self):        if not self.__len:            return None        else:            self.__len -= 1            return self.__stack.pop()    def getStackLen(self):        return self.__len    def getStackData(self):        return self.__stackstack = CStack()print(stack.getStackData())print(stack.pop())stack.push(1)stack.push("zjw")print(stack.getStackData())print(stack.pop())print(stack.pop())

進階操作,實現隊列:

#python list 實現隊列class CQueue:    def __init__(self):        self.__queue = []        self.__len = 0    def __del__(self):        del self.__queue    def enter_queue(self,obj):        self.__queue.append(obj)        self.__len += 1    def del_queue(self):        if not self.__len:            return None        else:            self.__len -= 1            return self.__queue.pop(0)    def view_queue(self):        return self.__queuequeue = CQueue()print(queue.view_queue())# enter the queuequeue.enter_queue(1)queue.enter_queue(2)print(queue.view_queue())#out the queueprint(queue.del_queue())#show 1print(queue.del_queue())#show 2print(queue.del_queue()) #will show None




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.