List of fifth chapters of Beginner's mind Continental-----python treasure Book

Source: Internet
Author: User

Yesterday is the first day of the week, a brief review of yesterday's study of things, in a re-look at the previous blog it.


Well, then today's collection began to learn, I only learning not to enter the time will open my magical world, under normal circumstances, he should be hidden in my body.


List: The Bible probably means: The list is Python's labor, Python is beginner's mind mainland.

I do not know where the little P teacher where to find the small laborers, try I can not help.

The point is: The list is variable, you can change the contents of the list, the simple thing is that this small laborer no temper, I bully all the line.


Use a tool to beat him first, the list function is one of the tools.

Because Hello is a small laborer that cannot be modified, it is useful to create lists based on characters.

>>> list (' Hello ')

[' H ', ' e ', ' l ', ' l ', ' O ']


The following basic list operations are then mastered: for example, indexing, sharding, joins, and multiplication. The interesting thing is that the list can be modified.

Changing the list: assigning values to an element

I've been learning assignments before. For example: x = 2, it seems that there is no need to tell the story before.

To use an index tag to assign a value to a particular location-specific element:

Next Practice:


>>> x = [1,1,1]

>>> x

[1, 1, 1]

>>> X[1] =2 here because the beginning of the sequence is starting from 0 so here's 1, which is actually the second element.

>>> x

[1, 2, 1] x[1] has been assigned to 2 so the changes have taken place here.

>>>

Note: You cannot assign a value to an element that does not exist in a position, because he does not exist, and I assign values such as a list length of 2, you cannot assign a value to an index of 100, and if you do, you need to create a 101 or longer list.


Next, you'll learn to delete elements:

In the literal sense is to delete elements, remove elements, seemingly nothing to say, experiment it:

According to the experiment, there is nothing to say, which is to delete the element as the second one.

>>> names = [' Alice ', ' Beth ', ' Cecil ', ' Dee-dee ', ' Earl ']

>>> names

[' Alice ', ' Beth ', ' Cecil ', ' Dee-dee ', ' Earl ']

>>> del Names[2]

>>> names

[' Alice ', ' Beth ', ' Dee-dee ', ' Earl ']

>>>


Next, The Shard assignment:

In short, it is the first Shard, the selection of a piece of content to replace the assignment, this is it.

Test to see:

Use list to create the peal string as a list, and then use the Shard to assign the element after the second element to DD

>>> name = List (' Perl ')

>>> Name

[' P ', ' e ', ' r ', ' L ']

>>> name [2:] = list (' dd ')

>>> Name

[' P ', ' e ', ' d ', ' d ']

>>>

There is a better function, not just equivalent substitution, even if the sequence of the original sequence and not the length can be replaced by the Shard

Under test:

>>> name = List (' Peal ')

>>> name[1:] = List (' Ython ')

>>> Name

[' P ', ' y ', ' t ', ' h ', ' o ', ' n ']

>>>

There are new features, why so cool, what function ah,

Shard assignment can not only replace, but also insert new elements, you wait, I'll try again.

>>> numbers = [1, 5]

>>> numbers[1:1] = [2,3,4]

>>> numbers

[1, 2, 3, 4, 5]

>>>

In fact, the substitution was made using empty shards, so I can use empty shards to delete elements. The result is the same effect as Del.

Give it a try

>>> numbers = [1,2,3,4,5]

>>> Numbers[1:4] = []

>>> numbers

[1, 5]

>>>

A new concept, a list of methods,

A method is a function that is closely related to some object, which can be a list, a number, or a string or other type of object, which is typically called by a method.

Object. Method (Parameter)

A new list method: Append, which is used to append a new object to the end of the list:

>>> LST = [+]

>>> Lst.append (4)

>>> LST

[1, 2, 3, 4]

>>> Why does the variable name not list, but LST, if you use list as the variable name, you cannot call the list function.

Append he modifies the table, is directly modified by the original list.


The second list method is count, which is very obvious, count is counted.

For example: or the actual fencing demo:

The number of times the to appears in the statistics list.

>>> [' To ', ' is ', ' or ', ' not ', ' to ', ' is '].count (' to ')

2

>>>


Another new list of methods, extend, words not much, the experiment to start:

>>> a = [+ +]

>>> B = [4,5,6]

>>> A.extend (b)

>>> A

[1, 2, 3, 4, 5, 6]

>>>

A bit of a problem, the string concatenation seems to be nothing different. Is that really the case?

string concatenation, the inside of a finally still no change, and used extend word a is changed.

>>> A

[1, 2, 3]

>>> b

[4, 5, 6]

>>> A + b

[1, 2, 3, 4, 5, 6]

>>> A

[1, 2, 3]

>>>

Seemingly using a = a + B can also achieve the effect, that is, readability is not extend good

Under test:

>>> a = [+ +]

>>> B = [4,5,6]

>>> A

[1, 2, 3]

>>> b

[4, 5, 6]

>>> A = a + b

>>> A

[1, 2, 3, 4, 5, 6]

>>>


Another way: Index, role Description: The index method is used to find out a value from the list the first occurrence of a match at the indexed position:

>>> Knights = [' We ', ' is ', ' the ', ' knights ', ' who ', ' say ', ' ni ']

>>> Knights.index (' Who ')

4

>>> Knights = [' We ', ' is ', ' the ', ' knights ', ' who ', ' say ', ' ni ', ' ni ']

>>> knights.index (' ni ')

6

>>>

>>> Knights = [' We ', ' is ', ' the ', ' knights ', ' who ', ' say ', ' nvi ', ' ni ']

>>> knights.index (' ni ') in the invocation list of the contents if no exception is reported.

7

>>>


The Insert method, which is used to insert an object into the list, is the meaning of the insertion.

>>> numbers = [1,2,3,5,6,7]

>>> Numbers.insert (3,4)

>>> numbers

[1, 2, 3, 4, 5, 6, 7]

>>>

As with the Extend method, insert can also implement a shard assignment.

>>> numbers

[1, 2, 3, 4, 5, 6, 7]

>>> Numbers[4:4] = [' Four ']

>>> numbers

[1, 2, 3, 4, ' Four ', 5, 6, 7]

>>>


Speaking of a new function pop, the function is to remove an element from the list, the default is the last one, remove and show the removed element.

The Pop method is the only one that can modify the list and return the worthwhile list method.

>>> numbers

[1, 2, 3, 4, ' Four ', 5, 6, 7]

>>> Numbers.pop ()

7

>>> numbers

[1, 2, 3, 4, ' Four ', 5, 6]

>>> Numbers.pop (4)

' Four '

>>> numbers

[1, 2, 3, 4, 5, 6]

>>>


In accepting a new list method: Remove, remove it.

Test, feel good familiar with it. Removes the value on the first match in the list.

>>> x = [' To ', ' is ', ' or ', ' not ', ' to ', ' is ']

>>> x.remove (' be ')

>>> x

[' to ', ' or ', ' no ', ' to ', ' being ']

>>> x.remove (' to ')

>>> x

[' or ', ' not ', ' to ', ' being ']

>>>

In learning a new method. The reverse method, which stores the elements in the list in reverse.

Experiment it:

>>> x = [+ +]

>>> X.reverse ()

>>> x

[3, 2, 1]

>>>

If you need a sequence to reverse iterate, you can use reversed, which returns an iterator object instead of a list. It is also possible to use the list function to convert the returned object to a list.

>>> x

[1, 2, 3]

>>> list (reversed (x))

[3, 2, 1]

>>>

>>>


In learning a list method: Sort effect: Sorting the list in the original position is the original table of the change.

>>> x = [1,2,3,47,3,2,5]

>>> X.sort ()

>>> x

[1, 2, 2, 3, 3, 5, 47]

>>>

The sort method is highlighted below: Because he modifies the original list of data, I want a sort that does not modify the original list, to a list copy, is sorted well. How does this happen:

>>> x = [1,2,3,47,3,2,5]

>>> y = x[:]

>>> y

[1, 2, 3, 47, 3, 2, 5]

>>> x

[1, 2, 3, 47, 3, 2, 5]

>>> Y.sort ()

>>> y

[1, 2, 2, 3, 3, 5, 47]

>>> x

[1, 2, 3, 47, 3, 2, 5]

>>>

It's a bit heavy. Rest summarized under. Continue later

List of fifth chapters of Beginner's mind Continental-----python treasure Book

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.