Python built-in data structures

Source: Internet
Author: User
Tags shallow copy

Depth Copy

    • = = Compares the contents of the data, if it is true that the content is the same, the reference is different
    • = After the operation is executed, the reference address of the front and back two elements is the same
      Shallow copy

Copy () List

    • Returns a new list
    • Reference types only copy reference addresses

Deep copy

    • Full copy. Include reference types
      From copy import deepcopy
      Lst2=deepcopy. (LST)

Random number

import random

Randint (A, B)

    • Returns an integer between [a, b], closed interval
      choice (seq)

    • Pick an element randomly from an element of a non-empty sequence, such as
      Random.choice (range)
      , randomly pick an integer from 0 to 9.
      Random.choice ([1,3,5,7])
      Randrange ([Start,] stop [, step])

    • The
    • gets a random number in the collection that increments by the specified cardinality from within the specified range, and the base default value is 1.
      Random.randrange (1,7,2)
    • Before closing
      Random.shuffle (list)->none

    • In-place scrambled list elements
      sample (population, k)

    • Randomly remove k different elements from the sample space or population (sequence or collection type) to return a new list
      tuple

    • Ordered set of elements
      Initialize:

      T=tuple ()
      t= ()
      t= (1,)

    • tuple queries are similar to lists
    • Tuple elements cannot be modified
      Note:
      for tuples that contain complex objects, their reference addresses to complex objects cannot be changed, but the data contents of complex objects can be changed
      Namedtuple

      from collections Import Namedtuple
      Point=namedtuple (' point ', [' X ', ' y '])
      #Point The second notation of the =namedtuple (' point ', ' x y ')
      P1=point (11,12)
      Print (P1.X,P1.Y)
      Part of the source code:

      If isinstance (Field_names, str):
      Field_names = Field_names.replace (', ', '). Split ()
      Field_names = list (map (str, field_names))
      TypeName = str (typename)
      String

String object is not mutable
Sequence of characters surrounded by "," "," "
Example
S1 = ' String '
S2 = "string2"
S3 = ' This ' s a "String" "
s4 = ' Hello \ magedu.com '
S5 = r "Hello \ n magedu.com"
S6 = ' C:\windows\nt '
S7 = R "C:\windows\nt"
S8 = ' C:\windows\nt '
sql = "" "Select * from user where name= ' Tom '" ""
String support using indexed access
Can iterate
Join connection

"String". Join (iterable), str
Concatenate each element of an iterative object with a string
Returns a new String object
+

Connect two strings
Returns a new String object
String segmentation

Split (Sep=none, Maxsplit=-1), List of strings
From left to right
Sep Specifies a split string, by default a blank string as a delimiter
MAXSPLIT Specifies the number of splits, 1 means traversing the entire string
Rsplit (Sep=none, maxsplit=-1), List of strings
From right to left
Splitlines ([keepends]), List of strings
To slice a string by line
Keepends refers to whether row separators are preserved
Line separators include \ n, \ r \ r, etc.
Partition (Sep) (head, Sep, tail)
From left to right, encounter the delimiter to split the string into two parts, return the head, delimiter, tail three parts of the ternary group;
If no delimiter is found, returns the ternary group of the header, 2 empty elements
Sep splits a string that must be specified
String case

Upper ()
All caps
Lower ()
All lowercase
case, use when making judgments
Swapcase ()
Interactive case
String typesetting

Title (), str
Each word in the title is capitalized
Capitalize () str
First word capitalization
Center (width[, Fillchar]) str
Width print widths
Fillchar-filled characters
Zfill (width), str
Width print widths, right, left with 0 padding
Ljust (width[, Fillchar]) str
Align Left
Rjust (width[, Fillchar]) str
Align Right
String modification

Replace (old, new[, Count]) str
Match is found in string with new substring, return new string
Count means replacing a few times, not specifying is replacing all
Strip ([chars]), str
Removes all characters from the specified character set chars from both ends of the string
If chars is not specified, remove whitespace characters from both ends
Lstrip ([chars]), str
Start from the left
Rstrip ([chars]), str
Start from the right
Find (sub[, start[, end]), int
Finds the sub-string sub in the specified interval [start, end], left to right. Returned index not found, returned-1
RFind (sub[, start[, end]), int
Finds the sub-string sub in the specified interval [start, end], right to left. Returned index not found, returned-1
Index (sub[, start[, end]), int
Finds the sub-string sub in the specified interval [start, end], left to right. The returned index was found, and the thrown exception was not found ValueError
Rindex (sub[, start[, end]), int
Finds the sub-string sub in the specified interval [start, end], left to right. The returned index was found, and the thrown exception was not found ValueError
Count (sub[, start[, end]), int
Count the number of sub-string sub occurrences in the specified interval [start, end], left to right
String judgments

EndsWith (suffix[, start[, end]), BOOL
In the specified interval [start, end], whether the string is suffix end
StartsWith (prefix[, start[, end]), BOOL
In the specified interval [start, end], whether the string is prefix start
Is series

Isalnum (), BOOL
Whether it's a letter or a number
Isalpha ()
Whether it is a letter
Isdecimal ()
Whether to include only decimal digits
IsDigit ()
Whether all numbers (0~9)
Isidentifier ()
is not the letter and the underscore start, the others are letters, numbers, underscores
Islower ()
are all lowercase
Isupper ()
are all uppercase
Isspace ()
Whether to include only white space characters

Python built-in data structures

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.