Python Basics Tutorial Learning Notes chapter II list and tuple _android

Source: Internet
Author: User
Tags extend

1, the index of the elements in the sequence:

The first element index is 0, the second is 1, incremented sequentially
The last element index is-1, the penultimate second is-2, descending sequentially

2. Type of sequence (Sequence):

Lists (list), tuples (tuple), strings (string), Unicode strings, buffer objects, xrange objects

3, list and tuple differences: The list can be modified, but the tuple is not

4, tuple often as a key to the dictionary

5, the sequence can do the following operations:

Indexes (indexing), slices (sliceing), add (adding), multiply (multiplying), and check whether the element belongs to the sequence (in), Maximum (max), Minimum (min), Length (len), iterator (iterator), and so on

6, the sequence can also contain other sequences

7, Index

7.1. The index of the first element is 0, the second is 1, the index of the last element is-1, and the second is-2, decreasing in descending order.

7.2, for the sequence can be directly indexed, rather than must be indexed by variables, such as: "Hello" [0], get h

7.3. If a function returns a sequence, the result can be indexed directly to get the specified element

8. Fragment

Accessing elements within a certain range

[Start:end:step]: Starts with the start index, takes an element every other step, but does not include the end element

[Start:end]: Starts with the start index, takes an element every 1 elements, but does not include the end element

[Start:]: From start index to end, take an element every 1 elements

[: end]: from 0, every 1 elements to take an element, but does not include the end element

[:]: Take all elements

[:: Step]: Starting from 0 to the end, each of the elements to take an element

[: End:step]: Starting from 0, every step of the element to take an element, to the end of the previous element

[Start::step]: Starting from start, take to the end, step steps

[Start:start]: Get start element

8.1, Step > 0 from the go after the take, step < 0 from the back forward; step cannot be 0

9, the same type of sequence can be added, the second sequence appended to the first sequence, generate a new copy of the sequence

A = [1,2,3,4,5]

b = [4,5,6,7,8]

The result of A + B is: [1, 2, 3, 4, 5, 4, 5, 6, 7, 8];a, b content does not change

10, the sequence multiplied by a number n, the size expands to now n times, the content repeats n times, like:

A = [1,2,3,4,5]

The results of A * 5 are: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5];a content does not change

11, you can use the in operator whether an object belongs to a sequence, return True or FALSE, for example:

Element1 = 1

Element2 = ' C '

Element3 = "Llo"

Sequence1 = [1,2,3,4,5]

Sequence2 = "ABCDEFG"

Sequence3 = "HELLOWORLD"

--------

>>> element1 in Sequence1
True

>>> Element2 in Sequence2
True

#下面这个操作在2. More than 3 versions are supported

>>> Element3 in Sequence3
True

12, the maximum, the minimum value, the length of the functions are: Max (), Min (), Len ()

13, List

13.1. The list function applies to all types of sequences, not just strings: LST = List ("Helloworld")

13.2, "". Join (LST) converts LST to a string

13.3, the element assignment: x=[1,2,3,4,5],x[0] = 5; The element to be assigned must exist or it will be an error; Assign a value and then modify the specified element

13.4, delete elements: names=["Alice", "Beth", "Ceil", "Dee-dee", "Earl"];d el names[2]; Deletes the element of the specified subscript; After deletion, it affects the list element

13.5, piecewise assignment: name = List ("Perl")

>>> name = List ("Perl")
>>> Name
[' P ', ' e ', ' r ', ' L ']
>>> name[2:] = "AR"
>>> Name
[' P ', ' e ', ' a ', ' R ']
>>> name[2:] = "HELLO World"
>>> Name
[' P ', ' e ', ' H ', ' e ', ' l ', ' l ', ' o ', ', ', ' W ', ' o ', ' R ', ' l ', ' D ']
>>> Name[1:1] = List ("12345")
>>> Name
[' P ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' e ', ' H ', ' e ', ' l ', ' l ', ' o ', ', ' W ', ' o ', ' R ', ' l ', ' D '
>>> name[5:] = []
>>> Name
[' P ', ' 1 ', ' 2 ', ' 3 ', ' 4 ']

13.6, List method: Object. Method (Parameter)

13.6.1, append (): Add elements to the end of the list you will modify the caller and return directly to

>>> a = List ("12345")
>>> A
[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']
>>> A.append (6)
>>> A
[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', 6]

13.6.2, COUNT (): Returns the number of specified elements in a list

>>> a = List ("HelloWorld")
>>> a.count (' l ')
3

13.6.3, Extend (): Append a list to the caller's back and modify the caller's content

>>> a = List ("Hello")
>>> B = List ("World")
>>> A.extend (b)
>>> A
[' H ', ' e ', ' l ', ' l ', ' o ', ', ' W ', ' o ', ' r ', ' L ', ' d ']

13.6.4, index (): Specifies the index for the first occurrence of an element in the list, and an error occurs if the element does not exist

>>> a = List ("Hello")
>>> A.index ("L")
2

13.6.5, insert (): Insert object into List

>>> a = List ("Hello")
>>> A.insert (3, "Xinye")
>>> A
[' H ', ' e ', ' l ', ' xinye ', ' l ', ' O ']

13.6.6, Pop (): The default pops up the last element, specifies a parameter, deletes the element of the specified index, and returns

>>> a = List ("Hello")
>>> A.pop ()
' O '
>>> A.pop (1)
E
>>> A
[' H ', ' l ', ' l ']

The Pop () method is the only way to modify a list and return a list element

13.6.7, common data structure: stack

Fifo:first Input First Output, the first out: Into the stack--insert (0, Element); Out stack--pop ()

Lifo:last Input first Output, back into the start: Into the stack--append ();--pop (0)

13.6.8, remove (): Deletes the first occurrence in the list, modifies the caller, but does not return a value, and if the deleted element does not exist, an error occurs

>>> a = List ("HelloWorld")
>>> A
[' H ', ' e ', ' l ', ' l ', ' o ', ' W ', ' o ', ' r ', ' L ', ' d ']
>>> A.remove ("a")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError:list.remove (x): X not in List
>>> a.remove ("E")
>>> A
[' H ', ' l ', ' l ', ' o ', ' W ', ' o ', ' r ', ' L ', ' d ']

13.6.9, reverse (): Reverse-store list, modify caller

>>> a = List ("Hello")
>>> A.reverse ()
>>> A
[' O ', ' l ', ' l ', ' e ', ' H ']

13.6.10, Reversed () function: Reverse store sequence, generate copy, not modify parameter content, but not return list, but return iterator, use list () function can convert iterator to list:list (reversed (list))

>>> a = List ("Hello")
>>> reversed (a)
<list_reverseiterator Object at 0x0000000002c2f0f0>
>>> A
[' H ', ' e ', ' l ', ' l ', ' O ']
>>> List (Reversed (a))
[' O ', ' l ', ' l ', ' e ', ' H ']

13.6.11, sort (): Change the package in the sorted, but not return the value

>>> a = [4,6,2,1,7,9]
>>> A.sort ()
>>> A
[1, 2, 4, 6, 7, 9]

If you do not want to change the content of a, just want a copy of the sort results, you need to be a copy of Mr. A, and then the copy to sort (the generation of replicas can not be directly assigned, because the assignment actually refers to the same object, only in the following way):

>>> a = list ("Hello World")
>>> B = a[:]
>>> B.sort ()
>>> A
[' H ', ' e ', ' l ', ' l ', ' o ', ', ' w ', ' O ', ' r ', ' L ', ' d ']
>>> b
[', ' d ', ' e ', ' h ', ' l ', ' l ', ' l ', ' o ', ' o ', ' R ', ' W ']

13.6.12, Sorted () function: Do not modify the contents of the parameter, return the sorted copy

>>> a = list ("Hello World")
>>> B = Sorted (a)
>>> A
[' H ', ' e ', ' l ', ' l ', ' o ', ', ' w ', ' O ', ' r ', ' L ', ' d ']
>>> b
[', ' d ', ' e ', ' h ', ' l ', ' l ', ' l ', ' o ', ' o ', ' R ', ' W ']

13.6.13, advanced sort: Sorting in a specific way

A. Sort by customizing a function in the form of compare (X,y), which returns 1 when X<y, and returns 1 when required to return to 0;x>y;

The default system provides a CMP function that is used for sorting, followed by the return of 1 at the time of x<y, the rule that returns 1 when required when returning 0;x>y (more than 3.0 no longer supports this built-in function)

The B.sort method supports two optional parameters key and reverse

Key makes a sort of function similar to CMP, but this function is not directly used to determine the size of an object, but rather to create a key for each element and then sort by key, for example, to sort by the length of an element, you can use the Len function to specify

r = ["Aardvark", "Abalone", "Acme", "Add", "aerate"]

R.sort (Key=len)

The contents of R after sort are: [' Add ', ' Acme ', ' aerate ', ' abalone ', ' aardvark ']

The value of reverse is true or false to indicate whether the order is reversed

>>> x = [2,8,4,6,5,7,9]
>>> X.sort (reverse=true)
>>> x
[9, 8, 7, 6, 5, 4, 2]

All of the above can be replaced by the sorted () function

14. Tuple: Immutable sequence. If you separate some values with commas, you create a tuple, which is generally enclosed in parentheses.

>>> 1,2,3,4,5
(1, 2, 3, 4, 5)
>>> A = 1,2,3
>>> A
(1, 2, 3)
>>> a = (1,)
>>> A
(1,)

If you want to create a non-empty tuple, you must use commas, even if there is only one element; To create an empty tuple, you use a ()

Tuple ([1,2,3]), tuple ("123"), Tuple ((1,2,3)) convert other sequences to tuples

The

Tuple can be the key to the map, but the list is not, which is also the main application of the tuple

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.