Python Basic Tutorial series: five, sequence

Source: Internet
Author: User
Tags first string

lists are also one of the sequences, so the actions for the list apply to the list as well. You can also access one or a sub-list by subscript or slice operations. So what's the difference between a list and a string? The first string must be composed only of characters, even if there is a number in the string, that is the number of the character type, not directly for the mathematical calculation, and secondly, the characters in the string are immutable. But a list is a flexible container that can hold any number of Python objects, like a large box, where objects can be placed inside. A list has element variability, and in a sequence, a list is the only data structure that supports element variability. The list is formally similar to the array in C and Java, but it is very different from the array. Arrays must require that the elements of an array class have a unique data type, either a number or a character.

the list is defined by square brackets []. For example: Alist = [123, ' abc ', 4.56, [' Inner ', ' list '], 7-9j] A list contains integer types, string types, floating-point types, sub-lists, and multiple data types of complex numbers.

list (' foo ')---->[' f ', ' o ', ' o ', convert the string type to a list type.

How do I access the elements in the list? ALIST[0]--->123, alist[3][1]---> ' list ', a bit of a two-dimensional array of sensations. Alist[:3]--->[123, ' abc ', 4.56]

alist[3]---->[' inner ', ' list ']

now that the elements in the list can be changed, how do we update the elements in the list? In fact, we can directly re-assign the value according to the array operation, for example, alist[2]=5.67--->alist = [123, ' abc ', 5.67, [' Inner ', ' list '], 7-9j], can also be appended operation append () , for instance, Alist.append ("Hello python!")---->alist = [123, ' abc ', 4.56, [' Inner ', ' list '], 7-9j, "Hello python! "]

If the list is mutable, then you can delete it by appending the modification, delete the main two methods, either know what this element is, delete it according to the content, or delete it according to the subscript, delete the form is Alist.remove (123), Del alist[0]=====> alist = [' abc ', 4.56, [' Inner ', ' list '], 7-9j]

We can also apply a for loop to the list, [i * 2 for i in [8,-2,5]]----->[16,-4,10], [i-I in range (8) if I% 2 = = 0]---->[0, 2,4,6], where range (8) =0,1,2,3,4,5,6,7, does not contain 8

Here are some of the operation functions,

CMP (), for comparison, however, because the list of element data type is different, so the comparison size is relatively complex, the actual use of relatively complex place less, the specific usage before mentioned to do not say, the comparison rules simply say, we understand the OK,

    1. Compares elements of two lists.
    2. If the element being compared is of the same type, the value is compared and the result is returned.
    3. If two elements are not of the same type, check whether they are numbers.
        a. If it is a number, perform the necessary number to force the type conversion, and then compare.
        b. If one of the elements is a number, the other party's element is "large" (The number is " The smallest ")
        c. otherwise, The comparison is made by the alphabetical order of the type name.
    4. If one list first reaches the end, the other long list is "large".
    5. If we run out of two list elements and all the elements are equal, then the result is a draw, which is is said to return a 0.

similar to CMP () and Max ()/min (), because of inconsistent data types, its accuracy decreases as the complexity increases.

len () for length, nothing to say, reversed () flip list, sorted (), sort of, here again, the so-called sort is the dictionary order, or you can understand the ASCII order, T<s, Because the ASCII code of the capital letter T is in front of the lowercase letter S.

summation function sum (), mainly spring numeric type, a = [4,5,6],sum (a) = 15. SUM (a,5) =

List () and tuple (), and so on we finish the tuple, we will focus on this, the list and the meta-group conversion between the use of high frequency, for example, you need to convert an existing tuple into a list type (then you can modify its elements), or the other way around.

in the textbook there are two examples of the use of lists to simulate the queue and the stack of the two data structures, mainly the list of append and delete operations, I do not speak here, you can be a test to try their own, learn the data structure of the students can see clearly.

Python Basic Tutorial series: five, sequence

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.