Detailed explanations of Python programming sequence operation examples and python programming examples

Source: Internet
Author: User

Detailed explanations of Python programming sequence operation examples and python programming examples

This article describes the sequence operations of Python programming. We will share this with you for your reference. The details are as follows:

# Coding = utf8''' the sequence type has the same access mode: each element of the sequence can be obtained by specifying an offset. You can obtain multiple elements at a time through the slicing operation. The subscript offset of the sequence is from 0 to the end of the total number of elements. Standard type operators can generally be used with all sequence types. Sequence Type Operator: sequence operator function ----------------------------------------------------------------------------- seq [ind] obtains the seq [ind1: ind2] obtain the Element Set seq * expr sequence repeated expr times seq1 + seq2 connection sequence seq1 and seq2obj in seq between ind1 and ind2 to determine whether the obj element is included in seq obj not in seq determines whether the obj element is not included in seq ----------------------------------------------------------------------------- slice operation The prototype of the operator in Python is [start: stop: step], that is, [start index: End index: step value] start index: same as other languages, starting from 0. In the sequence from left to right, the index of the first value is 0, and the last index is-1. The Slice operator takes the index until it does not contain the index value. Step Size: The default value is one after another. If the value is 2, it indicates performing the next fetch operation. When the step size is positive, it indicates taking from left to right. If it is negative, it indicates taking from right to left. The step size cannot be 0. The syntax of the slice index is much more flexible than that of a simple single element index. The start and end index values can exceed the string length. Function prototype: range (start, end, scan): parameter meaning: start: Count starts from start. The default value is from 0. For example, range (5) is equivalent to range (0, 5); end: the end of the count, but not the end. for example, range (0, 5) is [0, 1, 2, 3, 4] Without 5 scan: interval of each hop. The default value is 1. For example, range (0, 5) is equivalent to range (0, 5, 1) sequence type conversion factory function: struct function meaning struct list (iter) convert an iteratable object to a list of str (obj) convert an obj object to a string (String Representation of the object) unicode (obj) convert the object to a Unicode string (using the default encoding) basestring () Abstract Factory function, which is st The r and unicode parent classes cannot be instantiated. You cannot call tuple (iter) to convert an iteratable object into a tuples Object Sequence type. Available built-in functions: alias function name function alias --------------------------------------------------------------------------------------------- -------------------------- Enumerate (iter) accepts the iteratable object as the parameter and returns an enumerate object, which generates a tuple consisting of the index value and item value of each element of iter. Len (seq) returns the seq length max (iter, key = None) ormax (arg0, arg1 ...., key = None) returns iter or (arg0, arg1 ,...) if the key is specified, the key must be a callback function min (iter, key = None) ormin (arg0, arg1 ...., key = None) returns iter or (arg0, arg1 ,...) if the key is specified, the key must be a value that can be passed to the sort () method. The callback function for comparison reversed (seq) accepts a sequence as a parameter, returns an iterator sorted (iter, func = None, key = None, reverse = False) that is accessed in reverse order. Accepts an iteratable object as a parameter and returns an ordered list; description of the optional func, key, and reverse parameters and list. parameters of sort () built-in functions The number is the same. Sum (seq, init = 0) returns the sum of seq and the optional parameter init, equivalent to reduce (operator. add, seq, init) zip ([it0, it1 ,... itN]) returns a list with the first element it0, it1 ,... the first element of these elements forms a tuples, the second... similarly, '''class sequenceClass (object): def _ init _ (self): ''' defines an integer sequence table ''' self. intSeq = [95,456,236,458, 64, 77] ''' defines a floating point sequence table ''' self. floatSeq = [0.33, 2.56, 45.23, 45.33, 46.789, 23.00] ''' defines a string sequence table ''' self. strSeq = ["hello", "double", "floatNumer", "ewang"] # output the initialization sequence content def outInitData (self): print "initialized integer sequence table :", self. intSeq print "initialize floating point sequence table:", self. floatSeq print "initialize string sequence table:", self. strSeq # Use the sequence type operator def sequenceTypeOper (self): # obtain the element values in the sequence, the following table prints "the value of element % d in the integer list starting from 0 is % d" % (3, self. intSeq [2]) # obtain the set of elements from the nth to nth in the sequence (from left to right) print "floating point list % d -- % d element list: % r "% (1, 5, self. floatSeq []) # data set between countdown % d --- % d (from right to left) print "floating point list % d -- % d element list: % r "% (-1,-5, self. floatSeq [-5:-1]) # Use slice to print the reverse sequence "output the reverse string sequence:", self. strSeq [:-1] # Use slice to obtain the odd digit data list print "output integer list odd digit element list:", self. intSeq [: 2] # Use slice to obtain the even-bit data list print "output integer list even-bit element list:", self. intSeq [1:-] # print "string sequence repeated twice and output:", self. strSeq * 2 # connect the integer sequence and the floating-point sequence print "Connect the integer sequence and the floating-point sequence", self. intSeq + self. floatSeq # determine whether an element will be included in the sequence if 5 in self. intSeq: print "output sequence intSeq:", self. intSeq # judge if "home" not in self. strSeq: print "output sequence strSeq:", self. strSeq def sliceIndexUse (self): # the start and end index values of the slice index can exceed the length of the sequence print "output strSeq sequence:", self. intSeq [-100:100] # Use the range Function to operate the string # Delete the last character each time # The str string ind =-1 strg = self cannot be output. strSeq [2] print "the string to be operated (strg) is:", strg for ind in range (-1,-len (strg),-1): print strg [: ind], # using None as the index value, you can traverse from the first to the last element s = self. strSeq [-1] print "\ n the string to be operated (s) is:", s for I in [None] + range (-1,-len (s ), -1): print s [: I], def useTypeConver (self): iterObj = "hello ewang" inter = 45454 print "\ n iterObj type before using the type conversion function: ", type (iterObj) print" Use the inter type before the type conversion function: ", type (inter) # use the list type conversion function print" Use the variable type after the list conversion function: ", type (list (iterObj) # Use str to convert the object to the string type print" use str function to convert the variable inter to the string type: ", type (str (inter )) # use unicode function to convert to Unicode string print "use unicode function to convert variable iterObj to Unicode type:", type (str (iterObj )) # Use the tuple function to convert the variable iterObj to the tuple type: ", type (tuple (iterObj) def useSequencBIF (self ): # Call the enumerate built-in function print "use enumerate function:", enumerate (self. strSeq) # Call len function print "use len function:", len (self. strSeq) # Call max to calculate the maximum value print "Use the max function to calculate the maximum value of the sequence:", max (self. intSeq) print "Use the max function to calculate the maximum value of the parameter:", max (95,456,) # Call the min to calculate the minimum value print "Use the min function to calculate the minimum value of the sequence :", min (self. floatSeq) print "Use the min function to calculate the minimum value of the parameter:", min (0.33, 2.56, 45.23) # Call the reversed function to print the sequence in reverse order "Use the reversed function to reverse the sequence intSeq: ", reversed (self. intSeq) # Call the sorted function to sort the sequence print "Call the sorted function to sort the sequence strSeq:", sorted (self. strSeq) # Call sum to sum the sequence print "Call sum function to sum the sequence intSeq", sum (self. intSeq) # Call zip to return a list of tuples print "Call the zip function to return a list of tuples for the list Operation:", zip (self. strSeq, self. strSeq) def run (self): self. outInitData () self. sequenceTypeOper () self. sliceIndexUse () self. useTypeConver () self. useSequencBIF () def test (): # create an object instance seq = sequenceClass () seq. run () if _ name __= = "_ main _": test ()

Running result:

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.