Python learning notes (1) - some basic concepts

xiaoxiao2021-03-06  92

(1) Dictionary, list, sequence

Dictionary: Keyword-value form storage, disorder

Use {"key": "value", "key

2"

, "Value

2"

,….}form

List: Value Ordered Store, index starts from 0

Form in [Value0, Value1 ...]

The list index can be negative, -1 represents the last element

Split index list constitutes a new list

Li = ["A", "B", "C", "D", "E"]

Then: Li [0, 3] is ['A', 'B', 'C'] (from 0 to 3, but does not contain Li [3])

Li [1, -1] is ['b', 'c', 'd'] (from Li [1] to Li [4], excluding Li [4] ---- because Li [4] = Li [-1])

(Or understand the first index specifying the first element you want, the second index specifies the first element that does not want, the middle is the fragmentation index result)

Sequence: Unstrenified list, can not be modified in any way once you created

Define how the same list is used, but use () including

Note: (From in-depth learning python) So what is the benefit of the sequence?

The sequence is faster than the list. If you define a value collection constant, and the only thing to do is constantly traversing it, use the sequence instead of the list.

Dictionary keywords can be integers, strings, and "several other types", and sequences are one of those types. Sequences can be used as a keyword in the dictionary, but the list is not.

Sequence can be used in string formatting

The sequence can be converted to a list and vice versa. The built-in Tuple function receives a list and returns a sequence with the same elements. The LIST function receives a sequence and returns a list. From the effect, TUPLE is frozen a list, and the List is thawed a sequence.

(B) variable

a) "=" assignment, "==" judgment is equivalent

(3) formatted strings

a) Basic usage, similar to C's grammar, formatted by% S,% D

b) " " is an operator that connects the string

c) Connect and split string operations with Join and Split function (string only, can not force type conversion)

String.split (Delimiter, 1) Split the first element in the list

(4) Mapping list and filtration list

a) Mapping is a list of traverses by looping and applies a function for each element, and then returns a new list containing the calculated value (list mapping does not change the mapping list)

b) EG:

>>> UL = {"UID": 5, "Username": "Sofoo", "PWD": "Sofoopwd", "Email": "Sofool@gmail.com"}

You can perform the following layouts: (string formatting of Key and Value)

>>> ["% s =% S"% (K, UL ​​[K]) for k in ul.keys ()]

c) Filter list

[mapping-expression for element in source-list if filter-expression]

The filter expression can be any expression of true and false values, which calculates the element of the true value for the filter expression will be included in the mapping.

转载请注明原文地址:https://www.9cbs.com/read-106090.html

New Post(0)