Python BASICS (4): sequence operators, 2015 python

Source: Internet
Author: User

Python BASICS (4): sequence operators, 2015 python

A sequence is a type of sequential member arrangement that can be accessed by subscript offset. Python sequences include strings, lists, and metadata.
You can specify an offset for each element of the sequence, and multiple elements are obtained through slicing. The subscript offset starts from 0 and ends with-1.

Sequence Type Operator
These operators are applicable to all sequence types.

Sequence Operators Function
Seq [ind] Obtain the element whose subscript is ind.
Seq [ind1: ind2] Obtains the set of subscript elements from ind1 to ind2.
Seq * expr Repeated expr times
Seq1 + seq2 Connection sequence seq1 and seq2
Obj in seq Determine whether the obj element is in seq
Obj not in seq Determine whether the obj element is no longer in seq

 

Seq [ind] has the following code:

>>> Lst = [1, 3, 4, 5, 6] >>> exp = "abcdef" >>> tub = ("apple", "orange", "banana ", "watermelon") >>> print lst [2] # Elements marked as 2 in the print list 3 >>> print exp [0] # Three sequences are used in the same way a >>> print tub [3] watermelon>> print lst [-1] # negative index, starting from end 6 >>> print lst [6] # The index cannot exceed the Traceback (most recent call last): File "<pyshell #7>", line 1, in <module> print lst [6] # The index cannot exceed the sequence length IndexError: list index out of range >>> print, 5, 6] [0] # You can directly use 1> print 'abcde' [0] # You can directly use a instead of assigning values to sequences.

The preceding describes several solutions for using seq [ind. The number of positive index offsets is 0 to (total number of elements-1), starting from the first element index; the number of negative index offsets is-1 to the total number of negative elements, the index starts from the end element.

However, this index method can only index one element, and multiple element indexes are used.

sequence[start_index:end_index(:step)]

 

The following code is available:

>>> Lst = [1, 2, 3, 4, 5, 6] >>> print lst [:] # If two coordinates are omitted, [1, 2, 3, 4, 5, 6] >>> print lst [3:] # omitting the ending coordinate [4, 5, 6] >>> print lst [: 5] # omit the start coordinate [1, 2, 3, 4, 5] >>> print lst [: 2] # The step size is 2. Take one [1, 3, 5] >>> print lst [:-1] # reverse sequence [6, 5, 4, 3, 2, 1] >>> print lst [] # Take a [2, 4] From coordinate 1 to coordinate 5, separated by one >>>> print lst [-5: -3] [2, 3] >>> print lst [-] [6] >>> print lst [-6:-4] # negative index, not negative output [1, 2] >>> print lst [-] # When both positive and negative indexes exist, the coordinates only represent the positions, and the elements between the positions are intercepted [4, 5] >>> print lst [: 100] # The index can exceed the sequence length [1, 2, 3, 4, 5, 6] >>> print lst [-] # The index can exceed the sequence length [2, 3, 4, 5, 6]
There is another thing worth mentioning about slice operations. If negative indexes are used:
>>> lst = [1,2,3,4,5,6,7]>>> print lst[:-2][1, 2, 3, 4, 5]>>> print lst[:-1][1, 2, 3, 4, 5, 6]>>> print lst[:0][]

When the negative index is the tail coordinate, we will never be able to capture the last element, because-1 is the maximum value of the negative index. If 0 is used, it will be considered as a positive index.

In this case, we can use None to replace the 0 position.

>>> print lst[:None][1, 2, 3, 4, 5, 6, 7]

 

Join operator +

The join operator allows multiple sequences to be connected together, but can only connect the same object. All are lists, both are strings or are tuples

>>> num_lst = [1,2,3,4]>>> mixup_lst = [567,'abc',[123,'aaa']]>>> num_lst + mixup_lst[1, 2, 3, 4, 567, 'abc', [123, 'aaa']]>>> string = 'abcdef'>>> num_lst + stringTraceback (most recent call last):File "<pyshell#4>", line 1, in <module>num_lst + stringTypeError: can only concatenate list (not "str") to list

 

Repeated operator (*)

>>> mixup_lst = [567,'abc',[123,'aaa']]>>> string = 'abcdef'>>> mixup_lst * 2[567, 'abc', [123, 'aaa'], 567, 'abc', [123, 'aaa']]>>> string * 3'abcdefabcdefabcdef'>>> mixup_lst * mixup_lstTraceback (most recent call last):File "<pyshell#9>", line 1, in <module>mixup_lst * mixup_lstTypeError: can't multiply sequence by non-int of type 'list'

* Only numbers can be followed, indicating the number of repetitions. Otherwise, errors will occur.

In, not in

>>> mixup_list = ['a',123,['x',1.4,35]]>>> 'a' in mixup_listTrue>>> 'x' in mixup_listFalse>>> 'x' in mixup_list[2]True>>> string = 'abcdef'>>> 'a' in stringTrue>>> 'bcd' in stringTrue>>> 'abd' in stringFalse

The preceding operations are used to determine whether an element belongs to the sequence. If not in is used, the result is the opposite.

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.