Sequence in 10.python

Source: Internet
Author: User

After the string, the number, the Boolean value, you should continue to talk about Ganso, lists and the like. But Ganso and lists are all sequences, so it's important to first tell what the Python sequence is.

First, the sequence is the most basic data structure in Python. Each element in the sequence is assigned a number -its position, or index, the first index is 0, the second index is 1, and so on. Each index corresponds to an element .

Python contains a sequence of 6 built-in, including lists, tuples, strings, Unicode strings, buffer objects, and Xrange objects.

For a sequence, you can use the following actions:

1. Index

2. Slicing

3. Add

4. Multiply

5. Member Check

6. Calculating the length of a sequence

7. Take the maximum and minimum values in the sequence

1. Index

The so-called index is the number of each element, note that the number is starting from 0.

We can use the index to get its corresponding elements:

For strings:

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

  Note: For strings, the extracted element remains a string type.

For Ganso:

A = ('scolia', 123== a[1]print  bPrint  type (b)print  cprint type (c)

What is the type of the element that was taken out, and why this is the case, see the following example:

  is the same object, that is, the variable l and the variable B point to the same memory space .

def text ():     Print ' Scolia '  = ('scolia', 123= a[4]b ()

It's also possible to put a function inside, so what?

L == ('scolia', 123= a[4]b.append (1) #append () is a method of a list that adds an element to the end of the list, operates on the original list, but returns a value of Noneprint  lprint b

We manipulated the extracted elements, which affected the original list.

This illustrates the following:

    Ganso is actually a container, which can put any object, and when we go through the index to fetch the original object. Then I think it is possible to consider the index as a special variable, it points to the memory space, of course, this statement is set to a slight twist in the string, we can only assume that the string is the existence of each character in a memory space, each character is a string type, The variable gets the entire string, and the index is the variable for each character. Of course, this is just a hypothesis, how to deal with the specific python inside I do not know, maybe one day I should read the source of Python.

2. Slicing

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

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

Return is still a meta-ancestor, here to pay attention to a problem, that is a[2] is True, but the result we did not take, that the slice is not included in the index of the post-boundary .

In addition, I can write only one of them:

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

Represents the beginning (including) of an index, taking it to the end.

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

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

In addition, you can use negative numbers:

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

A positive number indicates that the index is computed from left to right, while negative numbers represent reciprocal values, such as 1, which means the countdown is the first.

However, the direction of the object is left-to-right, which should be noted here.

  

But if I write like this:

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

The beginning of the index at the end of the index to the right, is it possible to take the upside down?

The answer is:

No, you can get empty. It appears that you must take the object from left to right .

Also, when I give the index beyond it?

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

Python can automatically handle the problem of index overruns, which is where Python is elegant.

But be aware that:

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

This will not be taken.

Finally, there is a small trick, although the slice gets a meta-ancestor, but can find us slices to get a few elements, if at this time I use multiple variables to undertake it?

A = ('scolia', 123= a[:]    # Here you can get the original sequence, in addition to the issue of the depth of the copy, later to explain  print  bprint  cprint D

So I can get an independent object.

3. Add

The addition operation of the sequence returns a new object.

A = ('scolia', 123= ('good', 456= a + b
    Print  aprint  bprint C

does not affect the original sequence.

So what are the different types of sequences added?

A = ('scolia', 123= ['good', 456= a + b  Print  aprint  bprint C

No way.

  The addition of the list is the same, there is no repetition of examples here.

In addition, and the string of the + number stitching is the same.

4. Multiply

Multiplication and addition are similar, just a few of the same sequence is added, this is the same as the multiplication of mathematics.

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

Also does not affect the original sequence

5. Member Check

This is primarily the use of in and not-in operators.

b =print= ('scolia', +, True)printin  aprint ID (a[1]), ID (b)

The result here is very strange, if you remember the cache pool in Python (said in the previous article), not to say that the number is only -5~257 in the cache behavior? It seems that Python has been particularly optimized for this situation and has achieved a memory-saving effect.

Even if the assignment behaves as follows.

A = ('scolia', "=" = "print in a"print ID (a) [1]), ID (b)

  It appears that there is a hidden behavior when assigning a variable, and Python checks to see if the tuple and the list already have the same object, and if so, the value is not re-created, but the reference is repeated.

6. Element length Calculation

is actually a call to the Len () built-in function that returns the number of elements in the container.

A = ('scolia', +, True)print len (a)

The calculation is starting from 1, and the index is different, be careful to distinguish.

Also, although this is written in the sequence, this method applies to the dictionary, which is a special ' sequence '.

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

7. Take the maximum and minimum values in the sequence

This is actually the use of the built-in function max () and Max () , which returns the maximum and minimum values in the sequence.

For a comparison of different data please refer to: poke here

or refer to my previous blog post on Python's numbers.

A = ('scolia', +, True)print max (a)

The contents of the sequence are so much written for the time being that they will continue to be supplemented later if necessary.

Sequence in 10.python

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.