Effective Python(一)

Effective Python(一)

第八条: 不要使用含有两个以上(不包含两个)表达式的列表推导

示例一:

把矩阵或二维列表转化为一维列表

>>> matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> flat = [x for row in matrix for x in row]
>>> flat
[1, 2, 3, 4, 5, 6, 7, 8, 9]
示例二:

把矩阵或二维列表的数值平方后构成新的矩阵或二维列表

>>> matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> squared = [[x**2 for x in row] for row in matrix]
>>> squared
[[1, 4, 9], [16, 25, 36], [49, 64, 81]]

以上示例就是在列表中多重循环的合理用法。

问题?

如果表达式里还有一层循环呢?那么列表推导就会变得很长。

>>> my_lists = [[[1, 2, 3], [4, 5, 6]]...]
>>> flat = [x for sublist1 in my_lists
            for sublist2 in sublist1
            for x in sublist2]

可以看出,这样的代码阅读性非常差,且不简洁。

>>> flat = []
>>> for sublist1 in my_lists:
        for sublist2 in sublit1:
            flat.append(sublist2)

很明显,这中普通的循环语句带有适当的缩进,看上去比列表推导更加的简洁。

列表推导中也支持if语句

示例三:

从数字列表中选出大于4的偶数,下面两种列表推导方式是等效的。

>>> a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> b = [x for x in a if x > 4 if x % 2 == 0]
>>> c = [x for x in a if x > 4 and x % 2  == 0]
>>> b
[6, 8, 10]
>>> c
[6, 8, 10]
示例4:

从原矩阵中把那些本身能为3整除,去其所在行个元素之和有大于等于10的单元格挑出来。

>>> matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> filtered = [[x for x in row if x % 3 == 0] for row in matrix if sum(row) >= 10]
>>> filtered
[[6], [9]]

总结

在实际编程中,有不少缺失适合列表推导的例子。但是,原作者强烈建议大家尽量不要编写这种包含复杂式子的列表推导。这样写会使其他人很难理解这段代码。虽然省下了几行代码,但却会带阅读代码的人带来很大的障碍。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python is rapidly becoming the standard language for many talks in scientific research, and is particularly popular in biology and bioinformatics. One of the great strengths of Python is the ecosystem of tools and libraries that have grown up around it. This book introduces the novice biologist programmer to tools and techniques that make developing Python code easier and faster and will help you to write more reliable, performant programs. Written by a biologist, it focusses on solving the problems that students and researchers encounter every day: - How do I make my program run faster? - How can I be sure that my results are correct? - How do I share this program with my colleagues? - How can I speed up the process of writing my code? Chapters include: - Environments for development - learn how you can take advantage of different tools for actually writing code, including those designed specifically for scientific work. - Organising and sharing code - learn how Python's module and packaging system works, how to effectively reuse code across multiple projects, and how to share your programs with colleagues and the wider world. - Testing - learn how automated testing can make your code more reliable, how to catch bugs before they impact your work, and how to edit code with confidence. - Performance - learn how to make your code run quickly even on large datasets, how to understand the scaling behaviour of your code, and explore the trade offs involved in designing code. - User interfaces - learn how to make your code more user friendly, how to design effective interfaces, and how to automate record-keeping with Python's logging system.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值