热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

你可能不知道的30个Python语言的特点技巧(3)

从我开始学习Python时我就决定维护一个经常使用的窍门列表。不论何时当我看到一段让我觉得酷,这样也行!的代码时(在一个例子中、在StackOverflow、在开源码软件中

从我开始学习Python时我就决定维护一个经常使用的“窍门”列表。不论何时当我看到一段让我觉得“酷,这样也行!”的代码时(在一个例子中、在StackOverflow、在开源码软件中,等等),我会尝试它直到理解它,然后把它添加到列表中。这篇文章是清理过列表的一部分。如果你是一个有经验的Python程序员,尽管你可能已经知道一些,但你仍能发现一些你不知道的。如果你是一个正在学习Python的C、C++或Java程序员,或者刚开始学习编程,那么你会像我一样发现它们中的很多非常有用。

每个窍门或语言特性只能通过实例来验证,无需过多解释。虽然我已尽力使例子清晰,但它们中的一些仍会看起来有些复杂,这取决于你的熟悉程度。所以如果看过例子后还不清楚的话,标题能够提供足够的信息让你通过Google获取详细的内容。

列表按难度排序,常用的语言特征和技巧放在前面。

1.30   最大最小元素 (heapq.nlargest和heapq.nsmallest)

>>> a = [random.randint(0, 100) for __ in xrange(100)]  

>>> heapq.nsmallest(5, a)  

[3, 3, 5, 6, 8]  

>>> heapq.nlargest(5, a)  

[100, 100, 99, 98, 98] 

1.31   笛卡尔乘积 (itertools.product)

>>> for p in itertools.product([1, 2, 3], [4, 5]):  

(1, 4)  

(1, 5)  

(2, 4)  

(2, 5)  

(3, 4)  

(3, 5)  

>>> for p in itertools.product([0, 1], repeat=4):  

…     print ''.join(str(x) for x in p)  

…  

0000 

0001 

0010 

0011 

0100 

0101 

0110 

0111 

1000 

1001 

1010 

1011 

1100 

1101 

1110 

1111 

1.32   组合的组合和置换 (itertools.combinations 和 itertools.combinations_with_replacement)

>>> for c in itertools.combinations([1, 2, 3, 4, 5], 3):  

…     print ''.join(str(x) for x in c)  

…  

123 

124 

125 

134 

135 

145 

234 

235 

245 

345 

>>> for c in itertools.combinations_with_replacement([1, 2, 3], 2):  

…     print ''.join(str(x) for x in c)  

…  

11 

12 

13 

22 

23 

33 

1.33   排序 (itertools.permutations)

>>> for p in itertools.permutations([1, 2, 3, 4]):  

…     print ''.join(str(x) for x in p)  

…  

1234 

1243 

1324 

1342 

1423 

1432 

2134 

2143 

2314 

2341 

2413 

2431 

3124 

3142 

3214 

3241 

3412 

3421 

4123 

4132 

4213 

4231 

4312 

4321 

1.34   链接的迭代 (itertools.chain)

>>> a = [1, 2, 3, 4]  

>>> for p in itertools.chain(itertools.combinations(a, 2), itertools.combinations(a, 3)):  

…     print p  

…  

(1, 2)  

(1, 3)  

(1, 4)  

(2, 3)  

(2, 4)  

(3, 4)  

(1, 2, 3)  

(1, 2, 4)  

(1, 3, 4)  

(2, 3, 4)  

>>> for subset in itertools.chain.from_iterable(itertools.combinations(a, n) for n in range(len(a) + 1))  

…     print subset  

…  

()  

(1,)  

(2,)  

(3,)  

(4,)  

(1, 2)  

(1, 3)  

(1, 4)  

(2, 3)  

(2, 4)  

(3, 4)  

(1, 2, 3)  

(1, 2, 4)  

(1, 3, 4)  

(2, 3, 4)  

(1, 2, 3, 4) 

1.35   按给定值分组行 (itertools.groupby)

>>> from operator import itemgetter  

>>> import itertools  

>>> with open('contactlenses.csv', 'r') as infile:  

…     data = [line.strip().split(',') for line in infile]  

…  

>>> data = data[1:]  

>>> def print_data(rows):  

…     print '
&#039;.join(&#039; &#039;.join(&#039;{: <16}&#039;.format(s) for s in row) for row in rows)  

…  

>>> print_data(data)  

young               myope                   no                      reduced                 none  

young               myope                   no                      normal                  soft  

young               myope                   yes                     reduced                 none  

young               myope                   yes                     normal                  hard  

young               hypermetrope            no                      reduced                 none  

young               hypermetrope            no                      normal                  soft  

young               hypermetrope            yes                     reduced                 none  

young               hypermetrope            yes                     normal                  hard  

pre-presbyopic      myope                   no                      reduced                 none  

pre-presbyopic      myope                   no                      normal                  soft  

pre-presbyopic      myope                   yes                     reduced                 none  

pre-presbyopic      myope                   yes                     normal                  hard  

pre-presbyopic      hypermetrope            no                      reduced                 none  

pre-presbyopic      hypermetrope            no                      normal                  soft  

pre-presbyopic      hypermetrope            yes                     reduced                 none  

pre-presbyopic      hypermetrope            yes                     normal                  none  

presbyopic          myope                   no                      reduced                 none  

presbyopic          myope                   no                      normal                  none  

presbyopic          myope                   yes                     reduced                 none  

presbyopic          myope                   yes                     normal                  hard  

presbyopic          hypermetrope            no                      reduced                 none  

presbyopic          hypermetrope            no                      normal                  soft  

presbyopic          hypermetrope            yes                     reduced                 none  

presbyopic          hypermetrope            yes                     normal                  none  

>>> data.sort(key=itemgetter(-1))  

>>> for value, group in itertools.groupby(data, lambda r: r[-1]):  

…     print &#039;———–&#039; 

…     print &#039;Group: &#039; + value  

…     print_data(group)  

…  

———–  

Group: hard  

young               myope                   yes                     normal                  hard  

young               hypermetrope            yes                     normal                  hard  

pre-presbyopic      myope                   yes                     normal                  hard  

presbyopic          myope                   yes                     normal                  hard  

———–  

Group: none  

young               myope                   no                      reduced                 none  

young               myope                   yes                     reduced                 none  

young               hypermetrope            no                      reduced                 none  

young               hypermetrope            yes                     reduced                 none  

pre-presbyopic      myope                   no                      reduced                 none  

pre-presbyopic      myope                   yes                     reduced                 none  

pre-presbyopic      hypermetrope            no                      reduced                 none  

pre-presbyopic      hypermetrope            yes                     reduced                 none  

pre-presbyopic      hypermetrope            yes                     normal                  none  

presbyopic          myope                   no                      reduced                 none  

presbyopic          myope                   no                      normal                  none  

presbyopic          myope                   yes                     reduced                 none  

presbyopic          hypermetrope            no                      reduced                 none  

presbyopic          hypermetrope            yes                     reduced                 none  

presbyopic          hypermetrope            yes                     normal                  none  

———–  

Group: soft  

young               myope                   no                      normal                  soft  

young               hypermetrope            no                      normal                  soft  

pre-presbyopic      myope                   no                      normal                  soft  

pre-presbyopic      hypermetrope            no                      normal                  soft  

presbyopic          hypermetrope            no                      normal                  soft 

来源:编程笔记:原文地址:https://www.gaodaima.com



推荐阅读
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • 超级简单加解密工具的方案和功能
    本文介绍了一个超级简单的加解密工具的方案和功能。该工具可以读取文件头,并根据特定长度进行加密,加密后将加密部分写入源文件。同时,该工具也支持解密操作。加密和解密过程是可逆的。本文还提到了一些相关的功能和使用方法,并给出了Python代码示例。 ... [详细]
  • java drools5_Java Drools5.1 规则流基础【示例】(中)
    五、规则文件及规则流EduInfoRule.drl:packagemyrules;importsample.Employ;ruleBachelorruleflow-group ... [详细]
  • MySQL多表数据库操作方法及子查询详解
    本文详细介绍了MySQL数据库的多表操作方法,包括增删改和单表查询,同时还解释了子查询的概念和用法。文章通过示例和步骤说明了如何进行数据的插入、删除和更新操作,以及如何执行单表查询和使用聚合函数进行统计。对于需要对MySQL数据库进行操作的读者来说,本文是一个非常实用的参考资料。 ... [详细]
  • 深入理解Java虚拟机的并发编程与性能优化
    本文主要介绍了Java内存模型与线程的相关概念,探讨了并发编程在服务端应用中的重要性。同时,介绍了Java语言和虚拟机提供的工具,帮助开发人员处理并发方面的问题,提高程序的并发能力和性能优化。文章指出,充分利用计算机处理器的能力和协调线程之间的并发操作是提高服务端程序性能的关键。 ... [详细]
  • 本文整理了Java中java.lang.NoSuchMethodError.getMessage()方法的一些代码示例,展示了NoSuchMethodErr ... [详细]
  • 在本教程中,我们将看到如何使用FLASK制作第一个用于机器学习模型的RESTAPI。我们将从创建机器学习模型开始。然后,我们将看到使用Flask创建AP ... [详细]
  • Allegro总结:1.防焊层(SolderMask):又称绿油层,PCB非布线层,用于制成丝网印板,将不需要焊接的地方涂上防焊剂.在防焊层上预留的焊盘大小要比实际的焊盘大一些,其差值一般 ... [详细]
  • Opencv提供了几种分类器,例程里通过字符识别来进行说明的1、支持向量机(SVM):给定训练样本,支持向量机建立一个超平面作为决策平面,使得正例和反例之间的隔离边缘被最大化。函数原型:训练原型cv ... [详细]
  • 生产环境下JVM调优参数的设置实例
     正文前先来一波福利推荐: 福利一:百万年薪架构师视频,该视频可以学到很多东西,是本人花钱买的VIP课程,学习消化了一年,为了支持一下女朋友公众号也方便大家学习,共享给大家。福利二 ... [详细]
  • vb.net面试题,请大家帮忙,谢谢。如果需要讲详细一点,那就加我QQ531412815第4题,潜在的错误,这里的错误不是常规错误,属于那种只有在运行是才知道的错误:Catchex ... [详细]
  • 基于词向量计算文本相似度1.测试数据:链接:https:pan.baidu.coms1fXJjcujAmAwTfsuTg2CbWA提取码:f4vx2.实验代码:imp ... [详细]
  • Apple iPad:过渡设备还是平板电脑?
    I’vebeenagonizingoverwhethertopostaniPadarticle.Applecertainlydon’tneedmorepublicityandthe ... [详细]
author-avatar
透明的眼泪2502913707
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有