Python code abbreviation (if else for in)

Python code abbreviation (if else for in)

Modify from https://www.sfk8.com/python/256.html.

C= A if a> B else b//If a> B returns a, otherwise it returns b.

A= 1
B= 2
C= A if a> B else b
C.

... a if a> B else b//If a> B returns a, otherwise it returns b.

A= 1
B= 2
C= A if a> B else b
C.

C= [i for i in a if i% 2== 0]//Traverse a and return when i is even.

A= [1.2,3]
C= [i for i in a if i% 2== 0]
C.

Note that when you generate a list using the above method but only as a temporary parameter without referencing it, to reduce memory overhead, you should change sum ([i for i in a if i% 2=�]) to sum (i for i in a insert code snippet if i% 2== 0).

Generate set:
C= {i for i in a if i% 2== 0}//Traverse a and return when i is even.

Generate Dictionary:
C= {i: i * * i for i in a if i% 2== 0}//Traverse a and return when i is even.

Generate 2D:
C= [i+ j for i in a for j in b if i% 2= 0 and j% 2== 0]//Nest and traverse lists a and b simultaneously, returning the sum when i and j are both even numbers. Among them, for in a belongs to the outer layer nesting, and for in b belongs to the inner layer.

A= [1,2,3]
B= [4,5,6]
C= [i+ j for i in a for j in b if i% 2== 0 and j% 2== 0]
C.

C= [i if i% 2== 0 else 1 for i in a]//Traverse a, return directly if i is even, otherwise return 1 directly. If written before for here requires an else item.

A= [1,2,3]
C= [i if i% 2== 0 else 1 for i in a]
C.

a. B= b. A//Exchange data.

A= 1
B= 2
a. B= b. A.

. coin ['1 ',' 2 ']//Connection of character list.

C Join (['1 ',' 2 ']).

Derived formula It can be divided into three types: list deduction, dictionary deduction, and set deduction.

1. List derivation, also known as list parsing.

Function: It provides a convenient method for creating lists, so list parsing returns a list.

Format: Enclosed in square brackets, with a for statement in the middle, followed by an if statement for parsing, and passing the list that meets the conditions to the front of the for statement for building the list first.

[x * * 2 for item in item_list if item> 2].

Example:

Li= [i * 2 for i in range (10) if i% 2== 0]
Print li
[0, 4, 8, 12, 16].

It is obvious that the best way for list parsing is to perform the same operation on the entire list separately and return a new list, which does not directly return and needs to be assigned to variables.

2. Dictionary parsing, which is similar to the use of list parsing, except that the parentheses should be changed to curly braces, as the dictionary itself uses curly braces.

Example:

#Quickly convert dictionary key value positions.

MCA; {"a": 1, "b": 2, "c": 3, "d": 4}
Dicts; {v: k for k, v in mca. items()}
Print dicts
{1: 'a', 2: 'b', 3: 'c', 4:'d '}.

3. Set derivation formula.

Function: Set derivation is similar to list derivation, both of which perform the same operation on all elements of a list.

But a set is a non repetitive and unordered sequence.

Difference: The difference from push to list is: 1. Do not use square brackets, use curly braces; 2. There are no duplicates in the results; 3. The result is a set() set, which contains a sequence.

Squared; {i * 2 for i in [1.1,2]}
Print squared Set ([2,4]).

original text.

Related articles