Built-in use of Python lists, tuples, dictionaries, collections

Source: Internet
Author: User

List: is a tuple that can store multiple worthwhile data container contents that can store any data type that can store duplicate elements that are mutable (the ID is immutable when the content changes): It is also an element that can store multiple worthwhile data container tuples that cannot be modified        You can store any data type that can store duplicate elements that are immutable type elements and lists exactly who is fast? When adding and deleting elements, the list involves an operation that expands capacity and reduces capacity, and tuples do not have this process, so the tuple speed is slightly faster. For example, calling a pop method on a list and defining the tuple directly on a group: variable name = (        Value1,value,... N) Note: Once a tuple has been created, its contents are fixed. Index can find out the position of an element in a tuple if there is no direct error in the number of times the count statistic element appears in the tuple dictionary: What is a dictionary:         Data container Key-value pairs that can store multiple key-value pairs are: A pair of data in the form of key values must be immutable data type value can be any type dictionary is mutable (the content can be modified and the ID will not change)  Key cannot repeat value a repeatable dictionary is a way of unordered dictionary definitions: 1. Variable name = {Key:value} 2. Variable name = Dict () 3. Variable name = {} 4. Variable name = dict (key=value) store key value pairs into the dictionary at the same time you can specify to add a new key value pair [Key]=value Note: If key already exists, the corresponding value dictionary name will be updated [Key]=valu e dictionary name. Setdefault[key,value] Note: If the same key already exists in the dictionary, do nothing, otherwise add value: Remove value by Key Note: If the dictionary does not have the key to be taken, the program crashes the PRI    NT (dictionary [key] get takes out value by Key Note: If the dictionary does not have to get key, return none print (dictionary. Get (Key)) Loop value: Each time you fetch the key for variable name in dictionary: print (variable name) items are used to remove key-value pairs, which are packaged together for variable names in the dictionary. Items (): P Rint (variable name) keys is used to remove all key keys, and the type to be removed is the Dict_keys type print (dictionary. keys ()) values are used to take out the value that you have, the type that is removed is dict_values type print (word Code. VALUES ()) deletes a key-value pair: 1. del Generic Delete method, does not return the deleted value del dictionary [key] 2. Pop's own Delete method deletes the specified [Key:value] based on key, and returns the deleted value dictionary. Pop (key) 3.  Popitem Random Delete method randomly deletes a [key:value] and returns the deleted value dictionary. Popitem () Update: Update if dictionary A has a key in dictionary B, do not operate, if not, add [Key:value] Dictionary A Dictionary b Dictionary a.update (dictionary b) Collection: What is a collection: You can store multiple worthwhile data container features: elements are unordered and mutable and cannot store duplicate elements (    The same ID) can only store the definition of a collection of immutable data types: variable name = {value 1, value 2,.....}            Sets the intersection of scenes that are used to calculate the relationship between two sets: Notation 1: & Remove the same element in two sets print (set A & set B) 2: Intersection print (set a.intersection (set B)) asks for the intersection of two results and updates the set B print with the result (set A.intersection_update (collection B    ) is appended with _update (meaning that the result is copied to (set B), and the original value of collection B is all clear and set (closed):    Notation 1: | Merges all elements of the two collection (with duplicate elements showing only one element) print (set a | set B) 2:union print (set a.union (set B)) difference            Set: 1:-To find out the difference in the two sets of elements (note: The collection of multiple sets of reduced can be a travel set, if the difference set less difference sets return empty) print (set a-set B) 2:         Difference Print (set a.difference (set B)) to find the difference set of two results and update the set B print with the result (set A.difference_update (set B)) After adding _update (meaning to assign the result to (set B), the original value of set B clears the symmetric difference: notation 1: ^ Except for the intersection of elements are symmetric difference print (set a ^ set B        You can add a subset of _update print (collection a.symmetric_difference (set B)) after 2:symmetric_difference: Issubset Print (Collection A.issubset (collection B)) all the elements in collection a appear in collection B, and set A is a subset of collection B's parent set: Issuperset Print (Collection B. Issuperset (Collection A)) set B contains the elements in collection A, and set B is the parent set of set a without intersection: Isdisjoint print (set A.isdisjoint (collection B)) without intersection Back to Ture, there is a intersection return FALSE add element: Add print (collection. Add ()) update () adds multiple values at a time delete: Random delete:. Pop () deletes the specified element:. Remove ()        No element to delete is found. Discard () did not find the element to remove the program does not rush to remove the white space: strip () take the space on both sides of the string Lstrip () take out the space to the left of the string Rstrip () Take the space to the right of the string to add a parameter that specifies the string slice to be stripped: Split () The first parameter in the parentheses indicates what is used as the delimiter, the second argument indicates how many times rsplit () is cut from right to left, and no argument is replaced with a space as a separator string: replace () bracket The first parameter represents the old content, and the second parameter represents the new content case conversion: Upper () conversion to full capitalization lower () conversion to complete lowercase isupper () to determine whether all uppercase Islower () determines whether the full lowercase title () word is converted to uppercase is The title () determines whether the first letter of the word is capitalized capitalize () converts the first letter to uppercase Swapcase () the case of the beginning and end of the string: StartsWith () determines whether the string starts with a string endswith () Determines whether a string ends a string with a string: The Split () string is converted to a list ("A delimiter needs to be defined in parentheses") join () convert list to string "format: x=" | ". Join (list) format string: Format () {} Replace with placeholder {index} Replace with the specified index {name} is replaced by the specified name for example: My name is {} ' age is {} '. Format (parameter 1. ..., parameter n) print ("My name is%s-age is%s"% ("Yyh", "+")) print ("My name is {name} ' {age}". Format (name= "Yyh", age= "20")) Find: Find where a string appears in another string if there is no return -1rfind from right to left to find where one string appears in another string if there is no return -1index to find where a string appears in another string If there is no direct error, rindex from right to left to find a string that appears in another string if there is no error count to find the number of times a string appears in another string fill: center add a character of the same length around the content to center the content rjust to the right to the specified length ljust add a symbol to the left to reach the specified length Zfill add 0 to the left to reach the specified length is number series: IsDigit if all the characters in S are is a number and has at least one character in S, returns trueisdecimal true if there is only a decimal character in S, otherwise falseisnumeric returns true if there are only numeric characters in S, false otherwise. Append: Append is used to add new objects at the end of the list.

Built-in use of Python lists, tuples, dictionaries, collections

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.