Some tips about Control Flow in Python

来源:互联网 发布:产品设计软件 编辑:程序博客网 时间:2024/04/28 05:04

1.
You can use tuple unpacking to make some for loops cleaner:

Bad:
somelist = [(1, 2), (3, 7), (9, 5)]
result = 0
for t in somelist:
    result = result + (t[0] * t[1])

Good:
somelist = [(1, 2), (3, 7), (9, 5)]
result = 0
for x, y in somelist:
    result = result + (x * y)

2.
The enumerate function:

x = [1, 3, -7, 4, 9, -5, 4]
for i, n in enumerate(x):
    if n < 0:
        print("Found a negative number at index ", i)

The enumerate function returns tuples of (index, item).
You can access the item without the index.
The index is also available.

3.
The zip function:

Sometimes it’s useful to combine two or more iterables before looping over them.
The zip function will take the corresponding elements from one or more iterables and combine them into tuples until it reaches the end of the shortest iterable:

>>> x = [1, 2, 3, 4]
>>> y = ['a', 'b', 'c']
>>> z = zip(x, y)
>>> list(z)
[(1, 'a'), (2, 'b'), (3, 'c')]

4.
List and dictionary comprehensions:

The pattern of using a for loop to iterate through a list, modify or select individual elements, and create a new list or dictionary is very common. Such loops often look a lot like the following:

>>> x = [1, 2, 3, 4]
>>> x_squared = []
>>> for item in x:
... x_squared.append(item * item)
...
>>> x_squared
[1, 4, 9, 16]


This sort of situation is so common that Python has a special shortcut for such operations, called a comprehension.

The pattern of a list comprehension is as follows:
new_list = [expression for variable in old_list if expression]

And a dictionary comprehension looks like this:
new_dict = {expression:expression for variable in list if expression}

In both cases, the heart of the expression is similar to the beginning of a for loop—for variable in list—with some expression using that variable to create a new key or value and an optional conditional expression using the value of the variable to select whether it’s included in the new list or dictionary. For example, the following code does exactly the same thing as the previous code but is a list comprehension:

>>> x = [1, 2, 3, 4]
>>> x_squared = [item * item for item in x]
>>> x_squared
[1, 4, 9, 16]


>>> x = [1, 2, 3, 4]
>>> x_squared = [item * item for item in x if item > 2]
>>> x_squared
[9, 16]

we can use a dictionary comprehension, like so:

>>> x = [1, 2, 3, 4]
>>> x_squared_dict = {item: item * item for item in x}
>>> x_squared_dict
{1: 1, 2: 4, 3: 9, 4: 16}



5.

Most Python objects can be used as Booleans:

 The numbers 0, 0.0, and 0+0j are all False; any other number is True.
 The empty string "" is False; any other string is True.
 The empty list [] is False; any other list is True.
 The empty dictionary {} is False; any other dictionary is True.
 The empty set set() is False; any other set is True.
 The special Python value None is always False.


6.
if 0 < x and x < 10:
    ...

you can write it as you would in a math paper:
if 0 < x < 10:
    ...


(continue ... )

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝牙疼怎么办4岁 3岁宝宝龋齿牙疼怎么办 2岁宝宝不肯刷牙怎么办 3岁宝宝不爱刷牙怎么办 2岁宝宝不刷牙怎么办 二岁宝宝牙不好怎么办 小孩一刷牙就吐怎么办 孩子一刷牙就吐怎么办 两岁宝宝闹人怎么办 3岁宝宝不愿意刷牙怎么办 孩子牙没掉长出新牙来了怎么办 大孩子不洗澡怎么办啊 2岁宝宝不爱洗澡怎么办 手指画颜料变干怎么办 刮画纸画错了怎么办 电脑绘的图不能扩大怎么办 华腾同步课堂忘记密码怎么办 被缝纫机针扎了怎么办 大小孩抢了孩子玩具怎么办 无锡天一初中考不进天一高中怎么办 校考一个都没过怎么办 拼音会拼不会写怎么办 20岁出头很迷茫怎么办 出了社会很迷茫怎么办 2018年现在会计工作难找怎么办 开广告店没生意怎么办 淘宝没有7天退怎么办 吃了松香的鸭子怎么办 理科生考电影专业研究生怎么办 pr导出视频很慢怎么办 8岁儿童头发稀少怎么办 八岁儿童版头发怎么办 小孩子有一块不长头发怎么办 小孩子头发上长癣怎么办 一岁多头发少怎么办 孩子头发长得慢怎么办 小孩头发长得慢怎么办 头发出油不洗头怎么办 青少年掉头发很厉害怎么办 洗了冷水头头痛怎么办 头发烫染后干枯毛躁怎么办