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

2022-01-21 0 760

从我开始学习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('{: <16}'.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 '-----------' 

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

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Python教程 你可能不知道的30个Python语言的特点技巧(3) http://www.20zxx.cn/294319/xuexijiaocheng/python.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务