源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Python编程语言的35个与众不同之处(语言特征和使用技巧)

  • 时间:2020-02-22 09:01 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Python编程语言的35个与众不同之处(语言特征和使用技巧)
[b]一、Python介绍[/b]   从我开始学习Python时我就决定维护一个经常使用的“窍门”列表。不论何时当我看到一段让我觉得“酷,这样也行!”的代码时(在一个例子中、在StackOverflow、在开源码软件中,等等),我会尝试它直到理解它,然后把它添加到列表中。这篇文章是清理过列表的一部分。如果你是一个有经验的Python程序员,尽管你可能已经知道一些,但你仍能发现一些你不知道的。如果你是一个正在学习Python的C、C++或Java程序员,或者刚开始学习编程,那么你会像我一样发现它们中的很多非常有用。 每个窍门或语言特性只能通过实例来验证,无需过多解释。虽然我已尽力使例子清晰,但它们中的一些仍会看起来有些复杂,这取决于你的熟悉程度。所以如果看过例子后还不清楚的话,标题能够提供足够的信息让你通过Google获取详细的内容。 [b]二、Python的语言特征[/b] 列表按难度排序,常用的语言特征和技巧放在前面。 [b]1. 分拆 [/b]
[url=https://gist.github.com/hrldcpr/2012250]https://gist.github.com/hrldcpr/2012250[/url]查看详情) [b]29.映射对象到唯一的序列数 (collections.defaultdict) [/b]
[u]复制代码[/u] 代码如下:
>>> import itertools, collections >>> value_to_numeric_map = collections.defaultdict(itertools.count().next) >>> value_to_numeric_map['a'] 0 >>> value_to_numeric_map['b'] 1 >>> value_to_numeric_map['c'] 2 >>> value_to_numeric_map['a'] 0 >>> value_to_numeric_map['b'] 1
[b]30.最大最小元素 (heapq.nlargest和heapq.nsmallest) [/b]
[u]复制代码[/u] 代码如下:
>>> 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]
[b]31.笛卡尔乘积 (itertools.product) [/b]
[u]复制代码[/u] 代码如下:
>>> 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
[b]32.组合的组合和置换 (itertools.combinations 和 itertools.combinations_with_replacement) [/b]
[u]复制代码[/u] 代码如下:
>>> 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
[b]33.排序 (itertools.permutations)[/b]
[u]复制代码[/u] 代码如下:
>>> 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
[b]34.链接的迭代 (itertools.chain) [/b]
[u]复制代码[/u] 代码如下:
>>> 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)
[b]35.按给定值分组行 (itertools.groupby) [/b]
[u]复制代码[/u] 代码如下:
>>> 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 
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部