This article mainly introduced the Python programming sequence operation, combined with the example form analysis Python sequence function, the correlation function and the concrete use skill, the need friend can refer to the next
The examples in this article describe the sequence operations of Python programming. Share to everyone for your reference, as follows:
#coding =utf8 "' sequence type has the same access pattern: each of its elements can be obtained by specifying an offset. You can get multiple elements at once using a slice operation. The subscript offset for a sequence is from 0 to the total number of elements minus one end. Standard type operators are generally able to experiment with all sequence types. Sequence type operator:---------------------------------------------------------------------------sequence operator-------------------- -------------------------------------------------------Seq[ind] obtained subscript as an IND element Seq[ind1:ind2] get subscript from Ind1 to Ind2 The element collection seq * expr sequence repeats expr times SEQ1+SEQ2 connection sequence seq1 and seq2obj in SEQ to determine if the obj element is contained in SEQ, obj not in seq judgment If the obj element is not included in the SEQ---------------------------------------------------------------------------the prototype of the slice operator in Python is: [ Start:stop:step] that is: [Start index: End index: Step value] Start index: As in other languages, starting with 0. In the left-to-right direction of the sequence, the index of the first value is 0, and the last is the 1 end index: The slice operator will fetch the index and not contain the value of the index. Step value: The default is one after the other, if it is 2, it means to take one operation. When the step value is positive, it is taken from left to right and, if negative, from right to left. The step value cannot be 0. The syntax of a slice index is much more flexible than a simple single-element index. The start and end index values can exceed the length of the string. Function prototypes: Range (start, end, scan): Parameter meaning: Start: Count starts from start. The default is starting from 0. For example, range (5) is equivalent to range (0, 5); End: Count to end, but not end. For example: Range (0, 5) is [0, 1, 2, 3, 4] without 5 scan: The default is 1 for each hop. For example: Range (0, 5) is equivalent to Range (0, 5, 1) sequence type conversion factory function:--------------------------------------------------------------------------------------- --------------------------------function Meaning---------------------------------------------------------- -------------------------------------------------------------List (ITER) converts an iterative object to a list of str (obj) Converts an Obj object to a string (object's string notation) Unicode (obj) converts an object to a Unicode string (using the default encoding) basestring () Abstract factory functions, which are the parent classes of STR and Unicode, cannot be instantiated without invoking a tuple (ITER) to convert an iterative object into a tuple object-------------------------------------- Built-in functions available---------------------------------------------------------------------------------sequence types:------------------ Name of-----------------------------------------------------------------------------------------------------function function---------------------------------------------------------------------------------------------------------------- -------Enumerate (ITER) accepts an iterative objectAs a parameter, returns a enumerate object that generates a tuple of index values and item values for each element of ITER. Len (SEQ) returns the length of seq Max (Iter,key=none) Ormax (Arg0,arg1....,key=none) back to ITER or (Arg0,arg1,... ), if the specified Key,key must be a callback function that can be passed to the sort () method for comparison, Min (iter,key=none) ormin (Arg0,arg1....,key=none) Return to ITER or (Arg0,arg1,... ), if the specified Key,key must be one that can be passed to the sort () method, the callback function used for comparison reversed (seq) takes a sequence as a parameter and returns an The iterator sorted (Iter,func=none,key=none,reverse=false) accepts an iterative object as a parameter, returning an ordered list; optional parameters Func,key and re The meaning of verse is the same as the parameters of the List.sort () built-in function. SUM (seq,init=0) returns the sum of the SEQ and optional parameter init, with the effect equivalent to reduce (operator.add,seq,init) zip ([it0,it1,... ItN]) returns a column Table, whose first element is It0,it1, ... The first element of these elements consists of a tuple, the second one ... Analogy----------------------------------------------------------------------------------------------------------------------- ' Class Sequenceclass (object): Def __init__ (self): ' "' defines an integer sequenceTable ' self.intseq=[1,2,5,8,95,456,236,458,95,62,63,64,77] ' ' defines a floating-point sequence table ' ' self.floatseq=[0.33,2.56,45.23,45.33,4 6.789,23.00] "" Defines a string sequence table "' self.strseq=[" Hello "," Double "," Floatnumer "," Ewang "] #输出初始化序列内容 def outinitdata (self ): print "Initialize integer sequence table:", self.intseq print "initialize floating-point sequence table:", Self.floatseq print "Initialize string sequence table:", Self.strseq #使用序列类型操作符 def Sequencetypeoper (self): #获取序列中第几个元素值, the following table starts from 0 print "integer list%d element value is%d"% (3,self.intseq[2]) #获取序列中第几个到第几个的之间元素的集合 (left right) print list of elements between floating-point list%d--%d:%r "% (1,5,self.floatseq[1:5]) #倒数%d---%d data collection (right-to-left) print" list of elements between floating-point list%d--%d:%r "% ( -1,-5,SELF.FLOATSEQ[-5:-1]) #使用切片对序列逆序 print "Output sequence of strings in reverse order:", Self.strseq[::-1] #使用切片获取奇数位数据列表 print "Output integer list odd digits List of elements: ", Self.intseq[::2] #使用切片获取偶数位数据列表 print" Output integer list of even digit element list: ", Self.intseq[1:-1:2] #序列重复2次 print" string sequence repeats 2 times and outputs: ", self.strseq*2 #连接整数序列和浮点数序列 print" concatenate integer sequence and floating-point sequence ", Self.intseq+self.floatseq #判断某个元素会否包含在序列中 if 5 in self.ints Eq:print "Output sequence INTsEQ: ", self.intseq #判断某个元素不包含在序列中 if" home "not in Self.strSeq:print" Output sequence Strseq: ", Self.strseq def Sliceindexus E (self): #切片索引的开始和结束索引值可以超过序列的长度 print "Output strseq sequence:", self.intseq[-100:100] #使用range函数操作字符串 #每次都把位于最后一个字符删掉 #无法输出str这个字符串 ind=-1 strg=self.strseq[2] print "The string to manipulate (STRG) is:", STRG for IND in range ( -1,-len (STRG),-1): Print Strg[:ind], #使用None作为索引值, can be traversed from the first to the last element s=self.strseq[-1] print "\ n the string to manipulate (s) is:", s for i in [none]+r Ange ( -1,-len (s), -1): Print s[:i], def usetypeconver (self): iterobj= "Hello Ewang" inter=45454 print "\ n use Type to go Iterobj type before swapping function: ", type (iterobj) print" Inter type before using type conversion function: ", type (inter) #使用list类型转换函数 print" Variable type after using list conversion function: ", t Ype (List (iterobj)) #使用str把对象转换为字符串类型 print "Use the STR function to convert a variable inter to a String type:", type (str (inter)) #使用unicode函数转换为Unicode字符串 Print "Convert variable iterobj to Unicode type using Unicode function:", type (str (iterobj)) #使用tuple函数转换为一个元组对象 print "Convert variable iterobj to tuples using the tuple function Type: ", type (tuple (iterobj)) def usesequencbif (self): #调用enumerate内建函数 print "Using enumerate function:", Enumerate (SELF.STRSEQ) #调用len函数 print "Using l The EN function: ", Len (self.strseq) #调用max求最大值 print" uses the Max function to find the maximum value of the sequence: ", Max (SELF.INTSEQ) print" uses the Max function to find the maximum value of the parameter: ", Max (1,2,5,8,9 5,456) #调用min求最小值 print "Use min function to find the minimum value of the sequence:", min (self.floatseq) print "Use min function to find the minimum value of the parameter:", Min (0.33,2.56,45.23) #调用r The eversed function uses the reversed function to reverse-order the sequence Intseq: ", Reversed (SELF.INTSEQ) #调用sorted函数对序列进行排序 print" calls the sorted function to the sequence St Rseq to sort: ", sorted (self.strseq) #调用sum对序列进行求和 print" Calls the SUM function to sum the sequence intseq, sum (self.intseq) #调用zip返回一个元组列表 Prin T "call the ZIP function to return a tuple list to the list operation:", Zip (SELF.STRSEQ,SELF.STRSEQ) def run (self): Self.outinitdata () Self.sequencetypeoper () Self.sliceindexuse () Self.usetypeconver () self.usesequencbif () def test (): #创建一个对象实例 seq=sequenceclass () seq.ru N () If __name__== "__main__": Test ()
Operation Result: