Common Python modules

Source: Internet
Author: User

Search for help

Dir (string) # You can view all the member variables and functions of the module.

# The following code separates variables and functions into a list.
For FV in Dir (string ):
Name = "string. % s" % Fv
If callable (eval (name )):
Funorc. append (Fv)
Else:
Vars. append (Fv)
There are two functions to describe,
Eval is used to execute a string generation statement. For example, Eval ('string. Strip () ') can convert a string to a real function object.
With Eval, You can dynamically execute any user input statements in the code, which is very powerful, but there are also some dangers. You can use the following parameters to limit executable statements.
Callable is used to determine whether an object is a function.

Help (string. capwords). You can see the description of each function and member.

List

L. append (VAR) # append Element
L. insert (index, VAR)
L. Pop (VAR) # Return the last element and delete it from the list
L. Remove (VAR) # Delete this element that appears for the first time
L. Count (VAR) # number of elements in the list
L. Index (VAR) # position of the element. If no value exists, an exception is thrown.
L. Extend (list) # append list, that is, merge list to L
L. Sort () # Sort

Sort is complicated. Some examples are provided.
L = [2, 3, 4]
L. Sort () # [1, 2, 3, 4]
L. Sort (reverse = true) # [4, 3, 2, 1]

L = [('B', 2), ('A', 1), ('C', 3), ('D', 4)]
L. Sort (CMP = Lambda X, Y: CMP (X [1], Y [1]) # Compare the CMP functions of two items
L. Sort (Key = Lambda X: X [1]) # returns the function for extracting and comparing the feature value Key.
L. Sort (Key = Lambda x :( X [1], X [0]) # compare multiple keywords. First compare X [1], same ratio x [0]

0: the first element,-1: The last element,
-Len: the first element, the last element of the len-1.

L = range () # That is, L = [,], excluding the last element
L = range (1, 10, 2) # That is, L = [1, 3, 5, 7, 9]

List Comprehension
[<Expr1> for K in L if <expr2>]

L1 = L # the alias where L1 is L. in C, the pointer address is the same, and the L1 operation is the L operation. Function parameters are passed in this way.
L1 = L [:] # cloning when L1 is l, that is, another copy

String

S. Find (substring, [start [, end]) # searches for substrings in the specified range and returns the index value. Otherwise,-1 is returned.
S. rfind (substring, [start [, end]) # Reverse Lookup
S. Index (substring, [start [, end]) # Same as find, but the valueerror is not found.
S. rindex (substring, [start [, end]) # reverse lookup as above
S. Count (substring, [start [, end]) # returns the number of substrings found.
String. Strip (s) removes spaces around string s
String. lstrip (s), String. rstrip (s)

S. isalpha () # whether it is all letters with at least one character
S. isdigit () # whether it is all numbers with at least one character
S. isspace () # whether it is all blank characters with at least one character
S. islower () # Are all lowercase letters in S?
S. isupper () # Is the uppercase letter in S?
S. istitle () # whether s is capitalized

S. capitalize () # uppercase letters
S. Lower () # lower case
S. Upper () # convert to uppercase
S. swapcase () # case-insensitive swap

S. Split ('') # convert string to list and split with spaces
'+'. Join (list) # convert list to string and connect with the plus sign

Oat (STR) # returns a floating point number. Float ("1e-1") returns 0.1.
INT (STR) # converts it to an integer. INT ("12") returns 12.
INT (STR, base) # converts it to base-based integer number. The result of INT ("11", 2) is 3.

Maketrans and translate. These two functions are complicated in many places. In fact, they are used to create a replacement table with maketrans and replace it with translate.
String. maketrans (intab, outtab) --> This method returns a translation table that maps each character in the intab string into the character at the same position in the outtab string.
String. maketrans ('','') # No ing, which is actually retained by the original characters
String. maketrans ('abc', 'abc') # Replace lowercase letters with uppercase letters.

String. translate (Table [, deletechars]) --> return a copy of the string s, where all characters occurring in the optional argument deletechars are removed, and the remaining characters have been mapped through the given translation table. to put it bluntly, delete the deletechars and replace the others according to the replace table.

Dictionary

Dict = {'ob1': 'computer ', 'ob2': 'mouse', 'ob3': 'printer '}
Each element is pair, which contains two parts: Key and value. Key is of the integer or string type, and value is of any type.

D. Get (Key, 0) # Same as dict [Key]. If no key is added, the default value is returned. [] If no, an exception is thrown.
D. has_key (key) # returns true if the key is available; otherwise, false
D. Keys () # Return the dictionary Key List
D. Values ()
D. Items ()

Dict1 = dict # Alias
Dict2 = dict. Copy () # clone, that is, another copy.

Random

Random. Random () is used to generate a random number of points 0 to 1: 0 <= n <1.0
Random. Uniform (A, B) is used to generate a random number of characters in a specified range. One of the two parameters is the upper limit and the other is the lower limit.
Random. randint (A, B) is used to generate an integer in the specified range.
Random. randrange ([start], stop [, step]), obtains a random number from the set that increments by the specified base number within the specified range.
Random. Shuffle (X [, random]) is used to disrupt the elements in a list.
Random. Choice obtains a random element from the sequence (list, tuple ).
Random. Sample (sequence, k), random get the specified length from the specified sequence. The sample function does not modify the original sequence.

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.