Some tips about Control Flow in Python


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 ... )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值