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 ... )
- Some tips about Control Flow in Python
- Some tips about Class in Python
- some tips about python
- some tips about python One
- some tips about python Two
- some tips about python Three
- some tips about python Four
- some tips about python Six
- Some tips about python Seven
- some tips about python default value....
- Some Tips about Interbase
- some tips about jsp
- some tips about jdbc
- Some tips about layout
- About GNIT some tips
- Some Tips About C++
- some tips about shell variables
- Some tips about looping statement
- Android结合PHP开发前后端
- 奇怪的信
- Flash Player 设置管理器_hanCSDN_20130722
- windows2003的MIME设置(解决rmvb及flv不能下载问题)
- 都是我不好梦忧的shooow
- Some tips about Control Flow in Python
- linux系统日志编程
- HDU 1800 Flying to the Mars(trie)
- 二叉树的操作(后续更新)
- Java数据库编程的部分代码
- 就会大事化小的shooow
- log4j配置日志文件log4j.appender.R.File相对路径方法
- 以上情况的shooow
- 做事如此认真的shooow