<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!
    主站蜘蛛池模板: 免费又黄又爽又猛的毛片| 污污的视频在线免费观看| 亚洲va久久久噜噜噜久久狠狠| 精品久久免费视频| 91成人免费观看| 9久热这里只有精品免费| 激情无码亚洲一区二区三区| 亚洲视频一区在线播放| 亚洲情XO亚洲色XO无码| 亚洲 小说区 图片区 都市| 妻子5免费完整高清电视| 久久这里只精品热免费99| 久久最新免费视频| 免费人成再在线观看网站| 亚洲av无码一区二区三区天堂| 亚洲激情黄色小说| 久久亚洲AV无码精品色午夜麻豆 | 日韩亚洲产在线观看| 亚洲狠狠综合久久| 久久亚洲精品AB无码播放| 久久精品国产亚洲综合色| 国产国拍精品亚洲AV片| 久久久久亚洲av毛片大| 亚洲性日韩精品一区二区三区 | 亚洲性无码AV中文字幕| 亚洲国产理论片在线播放| 久久国产亚洲精品无码| 亚洲va无码va在线va天堂| 亚洲AV无码久久| 无码乱人伦一区二区亚洲一| 亚洲Aⅴ无码专区在线观看q| 久久亚洲AV午夜福利精品一区| 久久99国产亚洲精品观看| 亚洲AV日韩AV永久无码久久| 亚洲高清在线播放| 久久丫精品国产亚洲av不卡| 亚洲天堂视频在线观看| 亚洲精品国产福利在线观看| 亚洲成年人电影在线观看| 亚洲av午夜精品无码专区| 久久精品国产亚洲AV忘忧草18|