10. Sequence in python, 10. python Sequence

Source: Internet
Author: User

10. Sequence in python, 10. python Sequence

After talking about strings, numbers, and boolean values, we should continue to talk about the ancestor, list, and so on. However, both the ancestor and the list are sequences, so it is necessary to first talk about the sequence of python.

Sequence is the most basic data structure in Python. Each element in the sequence is assignedNumber-Its location, or index. The first index is 0, the second index is 1, and so on. Each index corresponds to oneElement.

Python contains 6 built-in sequences, including lists, tuples, strings, Unicode strings, buffer objects, and xrange objects.

You can perform the following operations on sequences:

1. Index

2. Slice

3. Add

4. Multiplication

5. member check

6. Calculate the sequence length

7. obtain the maximum and minimum values in the sequence.

 

1. Index

The so-called index indicates that each element is numbered. Note that the number starts from 0.

We can use indexes to obtain corresponding elements:

For strings:

a = 'scolia'b = a[3]print bprint type(b)

 

  Note: For a string, the retrieved element is still of the string type.

For the ancestor:

a = ('scolia', 123, True,(),[])b = a[0]c = a[1]print bprint type(b)print cprint type(c)

 

What is the original type of the retrieved element? Why? Let's look at the following example:

  It turns out to be the same object. That is to say, variable l and variable B point to the same memory space..

def text():    print 'scolia'a = ('scolia', 123, True, (), text)b = a[4]b()

 

The function can also be put in it. What about this?

L = [] a = ('scolia ', 123, True, (), l) B = a [4] B. append (1) # append () is a list method. It adds an element to the end of the list and operates on the original list. However, the returned value is Noneprint lprint B.

 

We performed operations on the retrieved elements, which actually affected the original list.

This shows that:

    Yuanzu is actually a container that can store any object, and the original object is obtained when we retrieve it through the index. So I can see whether the index can be considered as a special variable, which also points to the memory space. Of course, this statement is slightly twisted in the string, we can only assume that a string contains one memory space for each character. Each character is of the string type, the variable gets the entire string, and the index is the variable for each character. Of course, this is just an assumption. I don't know how to deal with python internally. Maybe one day I should read the python source code.

 

 

2. Slice

The so-called slice is to take out multiple objects at a time, and the index can only take out one object at a time.

a = ('scolia', 123, True)b = a[0:2]print b

 

The returned result is still a ancestor. Pay attention to the problem that a [2] is True, but we didn't get the result.Slice does not include the index of the rear limit..

In addition, I can write only one of them:

a = ('scolia', 123, True)b = a[1:]print b

 

Indicates that an index starts from (including) and the result is retrieved to the end.

 

a = ('scolia', 123, True)b = a[:2]print b

 

Indicates the index obtained from the beginning, to the end (not included ).

 

You can also use a negative number:

a = ('scolia', 123, True)b = a[-2:]print b

 

A positive number indicates that the index is calculated from left to right, and a negative number indicates the reciprocal. For example,-1 indicates the first reciprocal.

However, the direction of the object is from left to right. Pay attention to the following.

  

But what if I write it like this:

a = ('scolia', 123, True)b = a[-1:-3]print b

 

Can an index at the beginning be retrieved from the right of the index at the end?

The answer is:

No. The result is null. It seems thatThe object must be retrieved from left to right..

Also, what if I have given more indexes?

a = ('scolia', 123, True)b = a[0:100]print b

 

Python can automatically handle the index exceeding, which is also elegant for python.

Note:

a = ('scolia', 123, True)b = a[10:100]print b

 

In this way, the data cannot be obtained.

Finally, there is another trick: although the slice is a ancestor, We can find several elements in the slice. What if I use multiple variables to undertake it?

A = ('scolia ', 123, True) B, c, d = a [:] # The original sequence can be obtained here. In addition, it also involves the issue of copying in depth, I will explain print bprint cprint d later

 

In this way, I can obtain independent objects.

 

3. Add

The addition operation on the sequence returns a new object.

a = ('scolia', 123, True)b = ('good', 456)c = a + bprint aprint bprint c

 

The original sequence is not affected.

So what about the addition of different types of sequences?

a = ('scolia', 123, True)b = ['good', 456]c = a + bprint aprint bprint c

 

No.

  The addition of the list is the same.

In addition, it is the same as the plus sign of the string.

4. Multiplication

Multiplication and addition are similar, but only a few identical sequences are added, which is the same as multiplication in mathematics.

a = ('scolia', 123, True)b = a*2print aprint b

 

The original sequence is not affected.

 

5. member check

Here we mainly use the in and not in operators.

b = 3000print id(b)a = ('scolia', 3000, True)print b in aprint id(a[1]), id(b)

 

The result here is very strange. If you still remember the cache pool in python (as mentioned in the previous article), it doesn't mean that the number is only between-5 and ~ Will 257 be used for caching? It seems that python has made special improvements to this situation internally, achieving the effect of saving memory.

The same applies to values.

a = ('scolia', 3000, True)b = 3000print b in aprint id(a[1]), id(b)

 

  It seems that there is a hidden behavior when assigning values to variables. python checks whether the same object exists in the ancestor and list. If so, it does not re-create the value but references it again.

 

6. element length calculation

Actually, it is called by the built-in len () function, which returns the number of elements in the container.

a = ('scolia', 3000, True)print len(a)

 

The calculation starts from 1 and is different from the index.

Also, although it is written in a sequence, this method is also applicable to the dictionary, and the dictionary isSpecialIs a 'sequence '.

a = {'a':1,'b':2}print len(a)

 

7. obtain the maximum and minimum values in the sequence.

Here is actually the use of built-in functions max () and max (), which will return the maximum and minimum values in the sequence.

For comparison of different data, see: stamp here

Or refer to my previous blog post on python numbers.

a = ('scolia', 3000, True)print max(a)

The content of the sequence is written so much for the time being that it will be supplemented in the future if necessary.

 

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.