Everything should be made as simple as possible, but no simpler

Python小技巧5:需要动态生成列表的时候,考虑使用生成器替代

典型代码:def mygen(): i = 0 while i < 100: yield i i += 1 if __name__ == '__main__': gen = mygen() for item ...

2018-07-07 19:02:08

阅读数 1092

评论数 0

Python小技巧 4:利用字典的默认行为

典型代码1: from collections import defaultdict if __name__ == '__main__': data = defaultdict(int) data[0] += 1 print(data) 输出1: defaultdict(, {0: 1}...

2016-11-06 00:01:17

阅读数 2348

评论数 0

Python小技巧 3:列表项的排序

典型代码1: data_list = [6, 9, 1, 3, 0, 10, 100, -100] data_list.sort() print(data_list) 输出1: [-100, 0, 1, 3, 6, 9, 10, 100] 典型代码2: data_list = [6, 9, 1, ...

2016-10-24 13:53:18

阅读数 3910

评论数 0

Python小技巧 2:列表项的推导式和过滤操作

典型代码1: data_list = [1, 2, 3, 4, 0, -1, -2, 6, 8, -9] data_list_copy = [item for item in data_list] print(data_list) print(data_list_copy) 输出1: [1, 2...

2016-10-19 17:55:28

阅读数 1535

评论数 0

Python小技巧 1:列表项的拼接

典型代码: data_list = ['a', 'b', 'c', 'd', 'e', 'f'] separator = '\t' data_joined = separator.join(data_list) print(data_joined) 其输出为: a b c d e f 应用场景...

2016-10-15 10:26:05

阅读数 3632

评论数 0

提示
确定要删除当前文章?
取消 删除
关闭
关闭