<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!
    主站蜘蛛池模板: 国产一区二区免费在线| 大陆一级毛片免费视频观看 | 亚洲天堂免费在线| 亚洲伦另类中文字幕| 成人性生交大片免费看中文| 免费无码A片一区二三区| 亚洲视频网站在线观看| 污网站免费在线观看| 精品国产无限资源免费观看| 亚洲国产成人精品女人久久久| 亚洲福利一区二区三区| 4虎永免费最新永久免费地址| 亚洲激情电影在线| 色窝窝免费一区二区三区 | 免费人妻无码不卡中文字幕18禁 | 国产成+人+综合+亚洲专| 在线观看免费人成视频色9| 亚洲国产精品ⅴa在线观看| 免费国产黄网站在线观看可以下载| 国产一级淫片免费播放| 成年免费a级毛片| 亚洲AV无码专区国产乱码4SE| 91麻豆国产免费观看| 国产亚洲福利在线视频| 成人爽A毛片免费看| 国产亚洲精品成人久久网站| 岛国片在线免费观看| 精品视频免费在线| 亚洲AV无码一区二区乱孑伦AS| 在线免费中文字幕| 亚洲经典千人经典日产| 国内精品99亚洲免费高清| 人妻免费久久久久久久了| 精品亚洲国产成AV人片传媒| 免费女人高潮流视频在线观看| 成人亚洲国产va天堂| 亚洲中文字幕丝袜制服一区| 男女做羞羞的事视频免费观看无遮挡 | 综合亚洲伊人午夜网 | 亚洲综合久久1区2区3区| 精品国产精品久久一区免费式|