Python dictionary, list, tuple __python

Source: Internet
Author: User

Basic operations:

######################### #列表 #############################
alist = [1,2,3,4,5]

#python list is a mutable type. Modifications like the following are legal

#增
alist.append (6)


#删
alist.pop ()

#改
alist[0] =


#查
print (alist[ 0] for

v in alist:
    print (v)


############# #元组 ##########################
atuple = (1,2,3,4,5)
#在python tuples are immutable types and cannot be modified, as the following operation is illegal

# atuple[0] = 2 #查 for
v in atuple:
    print (v)

####### ######## #字典 #########################
adict = {"Name": "Zhengjinwei", "Age":, "Sex": "Male"}

#在python The Chinese dictionary can be modified
#增
adict["Index" = 1

#删
adict.pop ("index")

adict.clear ()

#改
adict[ "Name"]= "Bloodchilde"

#查 for
index,k in Enumerate (adict):
    print Index,k,adict[k]

advanced operations, implementation stack:

#python List Implementation Stack

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.__stack

stack = Cstack () print (Stack.getstackdata ()) print (
stack.pop ())

Stack.push (1)
Stack.push ("ZJW") Print (Stack.getstackdata ()) print (Stack.pop ()) print (Stack.pop ())




advanced Operations, implementing queues:

#python List Implementation Queue


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.__queue

queue = Cqueue ()

print (queue.view_queue ())

# Enter the queue
queue.enter_queue (1)
Queue.enter_queue (2)

print (Queue.view_queue ())

#out the queue
print (Queue.del_queue ()) #show 1
print (Queue.del_queue ()) #show 2
print ( Queue.del_queue ()) #will show None




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.