<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    海上月明

    editer by sun
    posts - 162, comments - 51, trackbacks - 0, articles - 8
       :: 首頁 :: 新隨筆 ::  :: 聚合  :: 管理

    [轉]Gems of Python

    Posted on 2008-11-11 09:41 pts 閱讀(430) 評論(0)  編輯  收藏 所屬分類: Python
    Gems of Python - Eric Florenzano's Website
    Gems of Python

    * Tags:
    * programming,
    * python

    Posted on Nov. 2, 2008 at 4:04 P.M.

    I've been using Python for a few years now, and continue to love it. But the language is large enough that it seems every project that I look at uses a different subset of the language's features. The nice thing is that by reading different people's code, you can find out about some real gems in the Python programming language. Some of these tips are extremely obvious, and some are more obscure. In any case, here are what I consider to be some real gems:

    1.

    filter This is one of Python's built-in functions, which allows you to remove unwanted items from a list. It's very useful and highly overlooked. Here's an example of how to use it:

    >>> lst = ['1', '2', '3', '4', 'asdf', '5']
    >>> def is_int_string(i):
    ... try:
    ... int(i)
    ... return True
    ... except ValueError:
    ... return False
    ...
    >>> int_string_list = filter(is_int_string, lst)
    >>> int_string_list
    ['1', '2', '3', '4', '5']

    But there is a really neat filter shortcut, too. You can have it filter out any values which evaluate to False.

    >>> lst = [0, 2, '', 'asdf', None, [], (), 'fdsa']
    >>> filter(None, lst)
    [2, 'asdf', 'fdsa']

    This is really great for filtering out unwanted values in a list. I use it actually quite frequently and hope to see it in more python projects.
    2.

    itertools.chain I often have a list of lists, and want to run some operation on each item in that list of lists. itertools.chain allows that to happen. Let me demonstrate with an example:

    >>> import itertools
    >>> lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    >>> list(itertools.chain(*lst))
    [1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> sum(itertools.chain(*lst))
    45

    itertools.chain has become a major part of my day-to-day coding toolbox, and it's a real python gem.
    3.

    setdefault Do you find yourself writing this type of try..except KeyError pattern often?

    >>> dct = {}
    >>> items = ['anne', 'david', 'kevin', 'eric', 'anthony', 'andrew']
    ... for name in items:
    ... try:
    ... dct[name[0]].append(name)
    ... except KeyError:
    ... dct[name[0]] = [name]
    >>> dct
    {'a': ['anne', 'anthony', 'andrew'], 'k': ['kevin'], 'e': ['eric'], 'd': ['david']}

    That's a lot of boilerplate code for something that seems like it should be much easier. Thankfully, this can be solved much more easily by using setdefault, like so:

    >>> dct = {}
    >>> items = ['anne', 'david', 'kevin', 'eric', 'anthony', 'andrew']
    >>> for name in items:
    ... dct.setdefault(name[0], []).append(name)
    ...
    >>> dct
    {'a': ['anne', 'anthony', 'andrew'], 'k': ['kevin'], 'e': ['eric'], 'd': ['david']}

    Much better, isn't it?
    4.

    defaultdict Given setdefault, it still seems like there should be a better way. If you know upfront that you will be setting everything to an empty list if it isn't found, you should be able to specify that and have it just Do The Right Thing. Thankfully, defaultdict does just that:

    >>> from collections import defaultdict
    >>> dct = defaultdict(list)
    >>> items = ['anne', 'david', 'kevin', 'eric', 'anthony', 'andrew']
    >>> for name in items:
    ... dct[name[0]].append(name)
    ...
    >>> dct
    defaultdict(, {'a': ['anne', 'anthony', 'andrew'], 'k': ['kevin'], 'e': ['eric'], 'd': ['david']})
    >>> dict(dct)
    {'a': ['anne', 'anthony', 'andrew'], 'k': ['kevin'], 'e': ['eric'], 'd': ['david']}

    I usually end up converting the defaultdict back to a regular dict before passing it around, but other than that, it's quite a useful tool to use.
    5.

    zip This is one that most people usually know, but still I find that sometimes people do strange things where a zip would be much easier.

    >>> numbers = [1, 2, 3, 4, 5]
    >>> letters = ['a', 'b', 'c', 'd', 'e']
    >>> for num, let in zip(numbers, letters):
    ... print "Letter %d is '%s'" % (num, let)
    ...
    Letter 1 is 'a'
    Letter 2 is 'b'
    Letter 3 is 'c'
    Letter 4 is 'd'
    Letter 5 is 'e'

    Pretty simple, very common, and yet sometimes coders seem to work around it. Also worth mentioning is that there's an iterable version of this, izip, which is located in itertools.
    6.

    title I wrote some little function to turn a string of lower case text into a 'titleized' string at work, and a few coworkers laughed and said, "why didn't you just use title"? The answer: I didn't know about it. It's not something you run into every day, but when you need it, it's super useful. Here's an example:

    >>> s = 'the little green men'
    >>> s.title()
    'The Little Green Men'

    It's that easy! Next time you need to turn something into a title, look for this method on strings first.

    That's all I've got for now. There are many more gems in the Python programming language, but these are some that I think are especially useful. Please share in the comments if you have any other gems that you have found in your uses of Python.

    UPDATE: Eric Holscher has posted a list of his Python gems as well, and it's got some great stuff on it. Check it out!
    主站蜘蛛池模板: 国产免费人成视频尤勿视频| 亚洲国产成人精品电影| 麻豆亚洲AV成人无码久久精品| 国产91色综合久久免费分享| 亚洲国产成人高清在线观看 | 亚洲熟妇av一区二区三区漫画| 亚洲欧美在线x视频| 国产精品深夜福利免费观看| 亚洲综合伊人制服丝袜美腿| 无码高潮少妇毛多水多水免费| 亚洲国产精品日韩在线观看| 69视频免费观看l| 亚洲欧洲精品一区二区三区| 最近中文字幕无免费| 亚洲国产成人精品无码区在线秒播| 午夜免费1000部| 四虎亚洲精品高清在线观看| 国产在线观看免费观看不卡| 亚洲色精品VR一区区三区| 日韩精品视频免费网址| 无码人妻一区二区三区免费视频| 亚洲日韩在线第一页 | 免费成人在线电影| 久久精品国产精品亚洲色婷婷| 日韩人妻一区二区三区免费| 亚洲一区在线免费观看| 日韩激情无码免费毛片| 四虎影视永久在线精品免费 | 亚洲AV中文无码字幕色三| 日韩午夜理论免费TV影院| 亚洲国产乱码最新视频| 啊v在线免费观看| a级毛片黄免费a级毛片| 亚洲校园春色小说| 免费亚洲视频在线观看| 国产99视频精品免费专区| 自拍日韩亚洲一区在线| 亚洲日韩中文字幕日韩在线| 午夜不卡久久精品无码免费| 亚洲中文精品久久久久久不卡| 亚洲一级黄色视频|