Study on Python crawler learning

Source: Internet
Author: User

June 4, 2017 21:08:34

The role of 1.dict is to establish a set of keys and a set of value mappings, Dict key can not be duplicated.
The 2.set holds a series of elements, which is much like the list, but the set elements are not duplicates and are unordered, which is similar to the Dict key.
3. Because set cannot contain duplicate elements, what happens when we pass in a list that contains repeating elements? The duplicate elements are removed. Set is actually a set in mathematics.
4. Because set stores unordered collections, we cannot access them by index.
5. Accessing an element in set actually determines whether an element is in set.
The internal structure of the 6.set is much like the dict, the only difference is that no value is stored, so it is very fast to determine whether an element is in set.
The set stored element is similar to the Dict key, and must be an immutable object, so any mutable object cannot be placed in the set.
The set stored elements are also in no order.
7. Since set is also a collection, traversing set is similar to traversing a list and can be implemented through a for loop.
8. Since set stores a set of unordered elements that are not duplicated, the update set mainly does two things:
One is to add the new element to the set, and the other is to remove the existing element from the set.
When adding an element, use the Add () method of Set:
9. When removing elements from set, use the Remove () method of Set:
-----------------Split Line--------------------
7th Chapter function
7-1 what is a function of Python
7-2 Python's Calling function
7-3 Python's writing function
7-4 return multi-value of Python function
7-5 python recursive function
7-6 python definition default parameters
7-7 python definition variable parameters
Notes:
1. A function is the most basic form of code abstraction.
2. Abstraction is a very common concept in mathematics.
3. To invoke a function, you need to know the name and parameters of the function, such as the absolute value of the function abs, which receives a parameter.
4. The comparison function cmp (x, y) requires two parameters, if x<y, returns 1, if x==y, returns 0, if X>y, returns 1:
The 5.Python built-in common functions also include data type conversion functions, such as the Int () function, which converts other data types to integers:
The 6.STR () function converts other types to str:
7. In Python, define a function to use the DEF statement, write down the function name, parentheses, the arguments in parentheses, and the colon: and then, in the indent block, write the function body, and the return value of the function is returned with a return statement.
8. When the statement inside the function body executes, once it executes to return, the function executes and returns the result. Therefore, it is possible to implement very complex logic within a function through conditional judgments and loops.
The 9.for loop can take out each element in the list.
A function of 10.Python returns a multivalued value that returns a tuple
1. Inside the function, other functions can be called. If a function calls itself internally, the function is a recursive function.
2. The advantage of recursive functions is that they are simple in definition and clear in logic. In theory, all recursive functions can be written in a circular way, but the logic of the loop is not as clear as recursion.
3. Use recursive functions to avoid stack overflow. In the computer, the function call is implemented through a stack (stack) of this data structure, each time into a function call, the stack will add a stack of frames, whenever the function returns, the stack will be reduced by a stack of frames.
Because the size of the stack is not infinite, there are too many recursive calls that can cause the stack to overflow. You can try to calculate fact (10000).
The second parameter of the 4.int () function is the conversion, if not passed, the default is decimal (base=10), if passed, use the passed parameters.
5.Python defines the default parameter, which is a default value for the parameter. Just use it right on the line.
6. If you want a function to accept any of the arguments, we can define a variable parameter:
7. The variable parameter has a * number in front of the name, we can pass 0, one or more parameters to the variable parameter:
------------------------Split Line--------------------------
Chapter 8th sectioning
8-1 slicing a list
8-2 Reverse sectioning
8-3 pairs of string slices
Notes:
1.l[0:3] Indicates that the fetch starts at index 0 until index 3, but does not include index 3. That is, index 0,1,2, which is exactly 3 elements.
2.l[:] Actually copied out a new list.
3. The slice operation can also specify a third parameter:
L[::2]
[' Adam ', ' Bart ']
The third parameter indicates that each n takes one, and the top L[::2] takes one out of every two elements, that is, one at a intervals.
Change the list to a tuple, the slice operation is exactly the same, but the result of the slice becomes a tuple.
4. For list, since Python supports l[-1] to take the first element of the countdown, it also supports the inverse slice
5. In many programming languages, there are many kinds of interception functions for strings, in fact, the purpose is to slice the string. Python does not have an intercept function for a string, it can be done simply by slicing one operation.
-------------------Split Line-------------------------------------
Chapter 9th Iteration
9-1 What is an iteration
9-2 Index iterations
9-3 Iteration Dict of value
9-4 Iteration dict Key and value
Notes:
1. In Python, given a list or tuple, we can traverse the list or tuple through a for loop, which iterates over us as an iteration (iteration).
2. The iterative operation is for a collection, regardless of whether the collection is ordered or unordered, we can always use the for loop to sequentially remove each element of the collection.
3. A collection is a data structure that contains a set of elements that we have covered:
1. Ordered set: List,tuple,str and Unicode;
2. Unordered collection: Set
3. Unordered collection and with Key-value pairs: dict
4. Iteration is a verb, which refers to an operation in Python, which is a for loop.
In 5.Python, iterations are always taken out of the element itself, not the index of the element.
6. For an ordered set, the element is indeed indexed. Sometimes we really want to get the index in the for loop, what do we do?
The method is to use the enumerate () function:
7. Using the enumerate () function, we can bind both index and element name in the For loop. However, this is not a special syntax for enumerate (). In fact, the enumerate () function puts:

[' Adam ', ' Lisa ', ' Bart ', ' Paul ']
Into something like this:

[(0, ' Adam '), (1, ' Lisa '), (2, ' Bart '), (3, ' Paul ')]
Therefore, each element of the iteration is actually a tuple:
8. The index iterations are also not really indexed, but are The enumerate () function automatically turns each element into a tuple (index, Element), and then iterates over it, acquiring both the index and the element itself. The
9.dict object itself is an iterative object that iterates directly over the dict with a for loop and can get a key for dict each time.
10. What should we do if we want to iterate over the value of the Dict object? The
Dict object has a values () method that converts dict to a list containing all value, so that we iterate over each of the Dict value:
1. What is the difference between these two methods?
1. The values () method actually converts a dict to a list containing value.
2. However, the Itervalues () method does not convert, and it takes value from dict in sequence during the iteration, so the Itervalues () method saves the memory needed to generate the list, compared to the values () method.
3. Print itervalues () finds that it returns a <dictionary-valueiterator> object, which shows that in Python, the For loop can function in more than LIST,TUPLE,STR iterations. Unicode,dict, and so on, any iterator object can be used for a for loop, and the inner iteration of how we usually don't care.
If an object says that it can iterate, then we iterate it directly with a for loop, which is an abstract data operation that does not require any data inside the iteration object.
4. Iteration dict Key and value
we learned how to iterate over the key and value of dict, so can we iterate both key and value in a for loop? The answer is yes. The
can see that the items () method converts the Dict object to a list that contains a tuple, and we iterate over the list to get both key and value:

>>> for key, value in D.items ():
... print key, ': ', value
...
Lisa:85
Adam:95
bart:59
Like the values () has a itervalues (), items () also have a corresponding iteritems (), Iteritems () does not convert dict to list, but in the iterative process is given a tuple, so, Iteritems () No additional memory is consumed.
-------------------Split Line--------------------
The 10th chapter list generation
10-1 Build List
10-2 Complex expressions
10-3 Conditional filtering
10-4 Multi-layer expressions
Notes:
1. Complex expression: the iteration that uses the For loop can not only iterate the normal list, but also iterate over the Dict.
2. After the list-generated for loop, you can also add an if judgment.
------------------------Split Line----------------------------have finished the basic tutorial

Study on Python crawler learning

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.