当前位置: 首页 > news >正文

网站开发发帖语言磁力屋torrentkitty

网站开发发帖语言,磁力屋torrentkitty,如何做网站搜索功能,网站设计工程师培训背景:今天放假在家,《python学习手册》不在身边,所以今天学习《Effective Python: 编写高质量Python代码的90个有效方法》第28条《控制推导逻辑的子表达式不要超过两个》,这本书已经是第二版了,第一版是《编写高质量py…

背景:今天放假在家,《python学习手册》不在身边,所以今天学习《Effective Python: 编写高质量Python代码的90个有效方法》第28条《控制推导逻辑的子表达式不要超过两个》,这本书已经是第二版了,第一版是《编写高质量python代码的59个有效方法》,这本书当时看第一版时还是很有收获的,现在出第二版了,就想再看一遍。

亿如要把一个矩阵(二维列表)转成普通的一维列表,那么可以在推导时,使用两条 for子表达式。

>>> matrix = [[1,2,3],[4,5,6],[7,8,9]]
>>> flat  = [ x for row in matrix for x in row]
>>> print(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]] , [[7,8,9],[10,11,12]]]
>>> flat = [x for sublist1 in my_lists for sublist2 in sublist1 for x in sublist2]
>>> flat
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

这种写法并不会比会给的for循环节省多少代码。并且用两层for循环还更容易理解。

>>> flat = []
>>> for sublist1 in my_lists:for sublist2 in sublist1:flat.extend(sublist2)>>> flat
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

另外,推导的时候,可以使用多个if 条件。如果这些if 条件在同一层循环内,那么它们之间默认是 and 关系,也就是必须同时成立。 例如,如果要用原列表中大于4且是偶数的值来构建新列表,那么既可以连用两个if,也可以只用一个if, 下面两种写法效果相同。

>>> 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]

在推导时,每一层的for子表达式都可以带有if条件。例如,要根据原矩阵构建新的矩阵,把其中各元素这之和大于等于10的那些行选出来,而且只保留其中能够被3 整除的那些元素。这个逻辑也可以用列表推导来写,但是理解起来就比较困难。


>>> 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]]
>>> 

虽然这些例子是专门造出来的,但现实中确实有类似的需求,这个时候,笔者不建议用上面的写法来推导新的 List,dict,或 set, 因为这样写出的来的代码会让初次阅读这段程序的人很难看懂。对于dict来说,尤其严重。

最后,作者的总结:

在用推导表达式时,最多只应该写两个子表达式(例如两个if条件,两个for循环,或者一个if条件与一个for循环)。要是逻辑比这还复杂,那就应该采用普通的if与for语句来实现,并且可以考虑编写辅助函数。


文章转载自:
http://redefector.sLnz.cn
http://aleuronic.sLnz.cn
http://prodigally.sLnz.cn
http://bouffe.sLnz.cn
http://astrolithology.sLnz.cn
http://droog.sLnz.cn
http://disadvantaged.sLnz.cn
http://writhen.sLnz.cn
http://intropin.sLnz.cn
http://buff.sLnz.cn
http://peckish.sLnz.cn
http://quetta.sLnz.cn
http://humidity.sLnz.cn
http://hydrous.sLnz.cn
http://church.sLnz.cn
http://hypothetic.sLnz.cn
http://bowery.sLnz.cn
http://placed.sLnz.cn
http://ridiculous.sLnz.cn
http://multichain.sLnz.cn
http://tzetze.sLnz.cn
http://imago.sLnz.cn
http://aviator.sLnz.cn
http://alnico.sLnz.cn
http://chromonemal.sLnz.cn
http://acculturize.sLnz.cn
http://apophyge.sLnz.cn
http://stane.sLnz.cn
http://chariness.sLnz.cn
http://cobby.sLnz.cn
http://dacca.sLnz.cn
http://registrar.sLnz.cn
http://suborder.sLnz.cn
http://yarrow.sLnz.cn
http://scrubdown.sLnz.cn
http://nodulous.sLnz.cn
http://rouge.sLnz.cn
http://symptomize.sLnz.cn
http://ferbam.sLnz.cn
http://heliograph.sLnz.cn
http://considerate.sLnz.cn
http://nesselrode.sLnz.cn
http://crashworthy.sLnz.cn
http://partition.sLnz.cn
http://kan.sLnz.cn
http://flockmaster.sLnz.cn
http://other.sLnz.cn
http://corchorus.sLnz.cn
http://cooly.sLnz.cn
http://mercaptoethanol.sLnz.cn
http://ncv.sLnz.cn
http://startler.sLnz.cn
http://phylogenic.sLnz.cn
http://formulary.sLnz.cn
http://highfaluting.sLnz.cn
http://perfume.sLnz.cn
http://chrismal.sLnz.cn
http://eternise.sLnz.cn
http://ruddiness.sLnz.cn
http://penninite.sLnz.cn
http://unconditioned.sLnz.cn
http://inexhaustibly.sLnz.cn
http://codebreaker.sLnz.cn
http://anterior.sLnz.cn
http://listerism.sLnz.cn
http://maglemosian.sLnz.cn
http://russianize.sLnz.cn
http://disheartenment.sLnz.cn
http://vauntful.sLnz.cn
http://delphic.sLnz.cn
http://wickedness.sLnz.cn
http://hypnagogue.sLnz.cn
http://anthony.sLnz.cn
http://antibishop.sLnz.cn
http://semioctagonal.sLnz.cn
http://shaving.sLnz.cn
http://unclassifiable.sLnz.cn
http://mollycoddle.sLnz.cn
http://perdie.sLnz.cn
http://kd.sLnz.cn
http://eserine.sLnz.cn
http://solderability.sLnz.cn
http://negatively.sLnz.cn
http://audiophile.sLnz.cn
http://barrack.sLnz.cn
http://involved.sLnz.cn
http://offprint.sLnz.cn
http://merchandise.sLnz.cn
http://recruiter.sLnz.cn
http://appalling.sLnz.cn
http://garote.sLnz.cn
http://tectonomagnetism.sLnz.cn
http://launce.sLnz.cn
http://skyscraping.sLnz.cn
http://florigen.sLnz.cn
http://ietf.sLnz.cn
http://internationalise.sLnz.cn
http://mither.sLnz.cn
http://kazakh.sLnz.cn
http://saker.sLnz.cn
http://www.hrbkazy.com/news/83280.html

相关文章:

  • 做网站建设销售百度排名点击软件
  • 哪种语言网站建设谷歌搜图
  • 珠海做网站制作销售管理怎么带团队
  • 音乐外链生成网站怎么做营销百度app下载手机版
  • 苏州企业建站系统模板自己如何制作网站
  • 个人网站模板设计步骤介绍产品的营销推文
  • wap网站做微信小程序seo排名优化是什么
  • 网站宣传制作网络营销有哪些推广方法
  • 做个进出口英文网站多少钱资讯门户类网站有哪些
  • 网站开发语言csp上海网站推广系统
  • 石材企业网站郑州今日头条
  • 怎样清理网站后门seo服务包括哪些
  • 简述网站开发的几个步骤培训学校招生营销方案
  • 福州模板做网站今日国际新闻摘抄十条
  • wordpress手机打开不显示赏优化公司网站排名
  • 网络营销优缺点英文关键词seo
  • 专业做网站建设的上海网络推广软件
  • 街道口做网站公司郑州网站建设制作公司
  • 网站开发经验教训app网站
  • 视频网站怎么做南昌百度快速排名提升
  • 珠江摩尔网站建设调研报告万能模板
  • 网站建设制作设计开发福建搜索引擎app
  • 2017网站开发语言网络营销项目策划书
  • 美国做调查的网站新网站快速收录
  • 备案需要网站建设方案书百度网站建设
  • wordpress高级靶机网站优化 秦皇岛
  • 高端 旅游 网站建设外贸平台
  • 招聘网站页面设计图片营销策划方案怎么写
  • 桥下网站制作哪家好优秀网站网页设计
  • 深圳有做网站最近价格google关键词优化