搜索
首页后端开发Python教程你可能不知道的30个Python语言的特点技巧

从我开始学习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 '\n'.join('\t'.join('{:

...  

 

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

...     print 'Group: ' + 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 


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python vs.C:申请和用例Python vs.C:申请和用例Apr 12, 2025 am 12:01 AM

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。 Python以简洁和强大的生态系统着称,C 则以高性能和底层控制能力闻名。

2小时的Python计划:一种现实的方法2小时的Python计划:一种现实的方法Apr 11, 2025 am 12:04 AM

2小时内可以学会Python的基本编程概念和技能。1.学习变量和数据类型,2.掌握控制流(条件语句和循环),3.理解函数的定义和使用,4.通过简单示例和代码片段快速上手Python编程。

Python:探索其主要应用程序Python:探索其主要应用程序Apr 10, 2025 am 09:41 AM

Python在web开发、数据科学、机器学习、自动化和脚本编写等领域有广泛应用。1)在web开发中,Django和Flask框架简化了开发过程。2)数据科学和机器学习领域,NumPy、Pandas、Scikit-learn和TensorFlow库提供了强大支持。3)自动化和脚本编写方面,Python适用于自动化测试和系统管理等任务。

您可以在2小时内学到多少python?您可以在2小时内学到多少python?Apr 09, 2025 pm 04:33 PM

两小时内可以学到Python的基础知识。1.学习变量和数据类型,2.掌握控制结构如if语句和循环,3.了解函数的定义和使用。这些将帮助你开始编写简单的Python程序。

如何在10小时内通过项目和问题驱动的方式教计算机小白编程基础?如何在10小时内通过项目和问题驱动的方式教计算机小白编程基础?Apr 02, 2025 am 07:18 AM

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

如何在使用 Fiddler Everywhere 进行中间人读取时避免被浏览器检测到?如何在使用 Fiddler Everywhere 进行中间人读取时避免被浏览器检测到?Apr 02, 2025 am 07:15 AM

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

Python 3.6加载Pickle文件报错"__builtin__"模块未找到怎么办?Python 3.6加载Pickle文件报错"__builtin__"模块未找到怎么办?Apr 02, 2025 am 07:12 AM

Python3.6环境下加载Pickle文件报错:ModuleNotFoundError:Nomodulenamed...

如何提高jieba分词在景区评论分析中的准确性?如何提高jieba分词在景区评论分析中的准确性?Apr 02, 2025 am 07:09 AM

如何解决jieba分词在景区评论分析中的问题?当我们在进行景区评论分析时,往往会使用jieba分词工具来处理文�...

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。