<jupyter_start><jupyter_text>[如何写出优雅又地道的Python代码？](https://mp.weixin.qq.com/s?__biz=MjM5MTI3MzUwMA==&mid=2650009474&idx=1&sn=ada8f64c911405fd5e391276825d2825&chksm=bebf48f189c8c1e74a524bdc4b7c2167afc27d3b004d520629184a636e934af24671bd7cf0e9&mpshare=1&scene=1&srcid=1026jAFbn7iTrsYd9VfXeQC0&pass_ticket=dy<PASSWORD>fqfZs%2BrZHgh%2BNxMqHR106qft%2BAWLYfsn%2FvfdD4%3Drd)  遍历<jupyter_code>colors = ['red', 'green', 'blue', 'yellow']
names = ['raymond', 'rachel', 'matthew']
f = open(".gitignore", "r")<jupyter_output><empty_output><jupyter_text>反向遍历<jupyter_code>for color in reversed(colors):
    print(color)<jupyter_output>yellow
blue
green
red<jupyter_text>遍历下标及内容<jupyter_code>for i,color in enumerate(colors):
    print(i,color)<jupyter_output>0 red
1 green
2 blue
3 yellow<jupyter_text>遍历两个集合<jupyter_code>for name, color in zip(names, colors):
    print(name+":"+ color)<jupyter_output>raymond:red
rachel:green
matthew:blue<jupyter_text>有序遍历  正序/逆序<jupyter_code>for color in sorted(colors, reverse=True):
    print(color)<jupyter_output>yellow
red
green
blue<jupyter_text>自定义顺序<jupyter_code>for color in sorted(colors, key=len):
    print(color)<jupyter_output>red
blue
green
yellow<jupyter_text>遍历到Markpoint **`partial`**- 一个函数可以有多个参数，而在有的情况下有的参数先得到，有的参数需要在后面的情景中才能知道，python 给我们提供了partial函数用于携带部分参数生成一个新函数。Refer: [`partial` 函数](http://www.cnblogs.com/zkchen/p/4003570.html)**`iter`**- `iter(iterable)` -> iterator- `iter(callable, sentinel)` -> iterator    - Get an iterator from an object.    - In the first form, the argument mustsupply its own iterator, or be a sequence.    - In the second form, the callable is called until it returns the sentinel.<jupyter_code>from functools import partial
blocks=[]
for block in iter(partial(f.read,32),''):
    blocks.append(block)<jupyter_output><empty_output><jupyter_text>**`for ... else...`**- 当迭代的对象迭代完并为空时，位于else的子句将执行，而如果在for循环中含有break时则直接终止循环，并不会执行else子句。<jupyter_code>for i, value in enumerate(['a','b','c','d','e','f']):
    if value=="c":
        break
    else:
        print(-1)
    print(i)<jupyter_output>-1
0
-1
1<jupyter_text>字典  用key-value对构建字典<jupyter_code>d = dict(zip(names, colors))
d<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14207149404216315, 'fraction_numerical': 0.05453712190650779, 'mean_word_length': 8.020661157024794, 'pattern_counts': {'":': 2, '<': 32, '<?xml version=': 0, '>': 34, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '591062', 'license': 'MIT', 'n_tokens_mistral': 1083, 'n_tokens_neox': 1051, 'n_words': 128, 'path': 'pythonic_style.ipynb', 'repo_name': 'ds17/machine_learning', 'size': '2175'}