python06 dictionary collection string

dictionary

basic concepts

A dictionary is an unordered collection of objects, using key-value storage, with extremely fast search speed. The key must use an immutable type. The keys in the same dictionary must be unique.
Each key value of the dictionary is separated by a colon:. The keys of each key-value pair are separated by commas. The entire dictionary is enclosed in curly braces {}.
dic={key1:value1,key2:value2}

Common method

The functions in this all need to use the d. function to use
d.keys() to return a list of keys in the dictionary.
d.values() returns a list of values ​​in the dictionary.
d.items() returns a list of tuples, each tuple (tuple) is composed of dictionary keys and corresponding values.
d.clear() deletes all entries in the dictionary.
d.copy() returns a copy of the high-level structure of the dictionary, but does not copy the embedded structures, but only copies the references to those structures.
d.updata(x) Update the contents of the dictionary with the key-value pairs in the dictionary x.
d.get(x[,y]) returns the value corresponding to the key x. If the key is not found, none is returned. If y is provided, y is returned if x is not found.
d.pop (k [, default]) key k exists, returns the corresponding value, which delete key-value pairs, otherwise the default value
d.popitem () on random fetching a key, returns in the form of tuples
dictionaries The key of is generally unique. If the last key-value pair is repeated, the previous value will be replaced, and the value does not need to be unique.

  • Note: The square brackets here indicate dispensation, and there will be similar expressions in future articles
    Insert picture description here

Creation method

Create a dictionary:

1. Basic syntax method The
value in the dictionary can take any data type, but the key must be immutable, such as string, number or tuple.
dict={'A':'2341','Cvxc':'46'}
2. Create a dictionary: dict function
Use the dict function. Create a dictionary through other mappings, such as dictionaries or key-value sequence pairs.
items= [('dfs','sdf'),('age',12)] There are two tuples in the list
d=dict(items). The
dict function can also create a dictionary through keyword arguments.
d=dict(name='sdf',age=42) The left side of the equal sign is the key, and the right side is the value.
3. Access to dictionary elements
Suppose d is a dictionary.
list(d) Convert all keys to lists.
list(d.values()) converts all values ​​into lists.
list(d.items()) converts all elements into lists. Each key-value pair forms a tuple.

Modification and deletion of dictionary elements

1. When assigning a value to a dictionary element by specifying a "key" as a subscript, there are two meanings.
If the key exists, it means to modify the value of the key.
Two if it does not exist. Indicates to add a new element.
2. Use the update() method of the dictionary object to add another dictionary element all at once to the current dictionary object. If the same key exists in the two dictionaries, the current dictionary will be updated based on the value in the other dictionary .
3. You can use the pop() of the dictionary object to delete the element corresponding to the specified key and return the corresponding value.
4. popitem() is used to delete a key-value pair of the dictionary and return a tuple containing two elements, where the two elements are the key and value of the dictionary element. What is deleted is the last key-value pair in the dictionary.
5. You can also use del to delete the element corresponding to the specified key. Such as del d['df']
Insert picture description here
Insert picture description here

Main usage

Insert picture description here

set

Overview

Python collections are unordered and variable container objects. All elements are placed in a pair of braces, and the elements are separated by commas. Each element in the same collection is unique and no repetition is allowed .
In the collectionCan only contain immutable data types such as numeric strings and tuples. It cannot contain variable types of data such as lists, dictionaries, and collections. Tuples containing variable types of data such as lists cannot be used as collection elements.
The elements in the set are unordered, and the storage order of the elements is not consistent with the order in which they are added. Index slicing operations are not available.
The collection does not support the use of subscripts to directly access elements at a specific location, nor does it support the use of the choice function in random to randomly select elements from the set, but supports the use of the sample function in the random module to randomly select some elements.
Create an empty set, use the set() function
Insert picture description here

Common method

s=add(x): Add the element x to the set s, if the element already exists, do nothing.
s=update(x): add the elements of x to the set s, x can be a list, tuple, dictionary, etc.
s.pop(x): randomly delete an element in the set. If it is empty, return the exception
s.remove(x): remove the element x from the set, if the element does not exist, an error will occur.
s.discard(x): Remove the element x from the set. If the element does not exist, no error will occur.
s.clear(): clear all elements
Insert picture description here

Set operations

Insert picture description here
Enhanced operator
Insert picture description here

Collection application scenarios

1. Relationship comparison
2. Data deduplication
Insert picture description here

String

A string is one or more characters enclosed in double quotation marks or single quotation marks. The triple quotes can support long strings of multiple lines.
The string can be stored in a variable, or it can exist alone.
Strings are immutable objects. All methods return a new string or byte string after processing , without any modification to the original string.
You can use the type function to test the type.
To output a quoted string, you can use escape characters.
Backslashes can be used to escape, using r can make backslashes not escaped. . If r"this is a line with \n", \n will be displayed instead of a new line.

String manipulation

A single index assists in accessing a specific location in the string. The format is s[integer] It is assumed that s is a string.
x in s Judge the substring. It returns true if it is a substring, otherwise it returns false.
str() can directly convert most data types into strings.
for var in string loops through each character in the string.
Insert picture description here

Common method

s.index(x), s.rindex(x) detects whether x is included in the string and returns the corresponding index value for the first occurrence, and returns an exception if it does not exist. The index is searched from the left. The other starts from the right.
s.count(x) returns the number of occurrences of x (x can be a substring.) in the string.
s.replace(str1,str2,max) replaces the string str1 with str2, max specifies the maximum number of replacements
maketrans(): creates a conversion table for character mapping.
translate(str): Convert string characters according to the mapping conversion table given by str.
ljust(width,fillchar) returns a new string that is left aligned with the original string and filled to the length of width with fillchar. fillchar is a space by default. rjust() center() is similar to centering.

s.split(str="xxxx",num=string.count(str)), where string.count(str) intercepts the string with str as the separator. If num (the latter parameter) has a specified value, only num is intercepted +1 substring, similar to rsplit(), intercepted from the right. By default, spaces are used for separation.
"Xxx".join(seq): Use the specified string xxx as the delimiter to merge all the elements (the string representation) in seq into a new string.
Insert picture description here

Insert picture description hereInsert picture description here
Insert picture description here

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/bj_zhb/article/details/104646613