Simple code sharing for the implementation of sequence tables in Python, and python Sequence

Source: Internet
Author: User

Simple code sharing for the implementation of sequence tables in Python, and python Sequence

Implementation of sequence table python (some functions are not implemented)

Result Display:

Sample Code:

#! /Usr/bin/env python #-*-coding: UTF-8-*-class SeqList (object): def _ init _ (self, max = 8): self. max = max # The default value is 8 self. num = 0 self. date = [None] * self. max # list () creates a list of eight elements by default, num = 0, and has a link # using list to implement list is ridiculous. It is all about exercise # self. last = len (self. date) # when the list is full, def is_empty (self): return self. num is 0 def is_full (self): return self. num is self. max # obtain the element def _ getitem _ (self, key): if not isinstance (key, int): raise TypeError if 0 <= key <self. num: return self. date [key] else: # An index error occurs when the table is empty or the index is out of range. raise IndexError # Set the element def _ setitem _ (self, key, value) at a location ): if not isinstance (key, int): raise TypeError # Only the existing elements in the list can be accessed. when num = 0, none of them can be accessed, self. when num = 1, only 0 if 0 <= key <self. num: self. date [key] = value # The error else: raise IndexError def clear (self): self. _ init _ () def count (self): return self. num def _ len _ (self): return self. num # append () and insert () def append (self, value): if self. is_full (): # print ("list is full") return else: self. date [self. num] = value self. num + = 1 def insert (self, key, value): if not isinstance (key, int): raise TypeError if key <0: # raise IndexError is not considered for the moment. # if the key is greater than the number of elements, if key> = self is inserted at the end by default. num: self. append (value) else: # The element after moving the key for I in range (self. num, key,-1): self. date [I] = self. date [I-1] # assign a value to self. date [key] = value self. num + = 1 # operation to delete an element def pop (self, key =-1): if not isinstance (key, int): raise TypeError if self. num-1 <0: raise IndexError ("pop from empty list") elif key =-1: # The original number is still there, but the list does not recognize him self. num-= 1 else: for I in range (key, self. num-1): self. date [I] = self. date [I + 1] self. num-= 1 def index (self, value, start = 0): for I in range (start, self. num): if self. date [I] = value: return I # raise ValueError ("% d is not in the list" % value) not found # list inversion def reverse (self): I, j = 0, self. num-1 while I <j: self. date [I], self. date [j] = self. date [j], self. date [I] I, j = I + 1, j-1if _ name __= = "_ main _": a = SeqList () print (. date) # num = 0 print (. is_empty (). append (0). append (1). append (2) print (. date) print (. num) print (. max). insert (1, 6) print (. date) a [1] = 5 print (. date) print (. count () print ("index with a returned value of 2 (first appearance):",. index (2, 1) print ("=") t = 1 if t:. pop (1) print (. date) print (. num) else:. pop () print (. date) print (. num) print ("=========") print (len (a). reverse () print (. date) "print (. is_full (). clear () print (. date) print (. count ())"""

For the implementation of sequence tables in Python data structures, you can refer to another article on this site.Python Data Structure-sequence table implementation code example"The sequence table is described in detail. I have not fully understood this knowledge point and will continue to study it later.

The above is all about the simple code implementation of the sequence table in Python. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

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.