Generative dichotomy

A ternary expression

A triplet of expressions:
1. When a large return x when x, y big time when returns y
2. When a condition exists to do one thing and do another thing does not hold

Ternary expressions with a fixed expression:

  1. Values (1) Condition else IF value (2)
    condition is satisfied. (1)
    condition is not satisfied the value of (2)
    in the case 2. Recommended ternary expressions with only two options (yes or no)

Example 1:
Comparison of two values by conventional methods Size: 6 lines

def my_max(x, y):
    if x > y:
        return print(x)
    else:
        return print(y)
my_max(10, 20)

Examples (Triad expression) 1: 1 line Key Code

x = 1
y = 2
res = x if x > y else y  # 如果if后面的条件成立返回if前面的值,否则返回后面的值
print(res)

Example 2: toll free

is_free = input('请输入是否免费(y/n)>>>:')
is_free = '免费' if is_free == 'y' else '收费'
print(is_free)

Second, the algorithm - dichotomy

Algorithm: a highly efficient solution to the problem

1. Find an element is in a list

Common method for i in k:

k = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for i in k:
    if num == i:
        print("找到了:%s" % i)

2. dichotomy:

1. Digital container must have the order
2 and sequentially comparing the number of intermediate size, smaller than take the left and right take greater than
sort: sort () in ascending order, sort (reverse = Ture) descending, Reverse () inverted, reverse arrangement

l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
l.sort()  # 重新升序排序
def get_num(l, num):
    # 取列表中间数索引位置
    num_middle = len(l) // 2
    # 这个数不在列表中,切到后面就是空列表,空列表就直接返回
    if not l:
        return print("列表中找不到", num)
    print(l) # 打印每次切片后的列表
    # 判断大小
    if num > l[num_middle]:
        # 切片右半部
        right_num = l[num_middle + 1:]
        get_num(right_num, num)
    elif num < l[num_middle]:
        # 切去左半部
        left_num = l[:num_middle]
        get_num(left_num, num)
    else:
        print("列表中找到了", num)


get_num(l, 9)

Third, the list of formula

m = ['jeff','jiamin','nick','xiaomin]

Add to the list of suffixes: _sb
conventional methods: for looping through a method of adding printing append
list formula: [ '% s_sb'% name for name in m]

Example 1: List element add the suffix

第一种:普通for循环方法
m = ['jeff','jiamin','nick','xiaoming']
m1 = []
for name in m:
    m1.append('%s_sb'%name)
print(m1)

The second: a list generation approach

m = ['jeff','jiamin','nick','xiaoming']
res = ['%s_sb'%name for name in m]
print(res)

Example 2: commander is selected from a list of elements with _sb

k = ['jeff_sb', 'jiamin_sb', 'nick_sb', 'xiaoming_sb','xiaowang_NB']
res = [name for name in k if name.endswith("_sb")]  #endswith:字符串结束位置检测
print(res)

img

Fourth, the Dictionary of formula

Demand: The two lists into a dictionary
M1 = [ 'name', 'password', 'Age', 'Gender']
M2 = [ 'Jeff', '123', '18 is', 'M']

1.普通for循环方法
m1 = ['name', 'password', 'age', 'gender']
m2 = ['jeff', '123', '18', '男']
d = {}
for i, j in enumerate(m1):    # enumerate方法查看元素的索引和值,默认0开始
    print(i, j)  # 查看m1的索引和值,m1索引=i,m1的值=j
    d[j] = m2[i]
print(d)

img

2. Dictionary of formula:

d = {i: j for i, j in enumerate(m2)}
print(d)

img

Plus if Analyzing: removing j = 123

d = {i: j for i, j in enumerate(m2) if j != '123'}
print(d)

img

V. collection formula

Examples: 0-9 4 and removed

res = {i for i in range(10) if i != 4}
print(res)

img

Guess you like

Origin www.cnblogs.com/WQ577098649/p/11887439.html