Python 点滴

1. What is the difference between dict.items() and dict.iteritems()?

It's part of an evolution. Originally Python items() built a real list of tuples and returned that. That could potentially take a lot of extra memory. Then generators were introduced to the language in general, and that method was reimplemented as a iterator-generator method named iteritems(). The original being left for backwards compatibility. One of the changes of Python 3.x is that theitems() methods now also return iterators and a list is never fully built. In Python 3.x there is noiteritems() method any longer,asitems() is now the same asiteritems() in Python 2.x (WRONG!!!).

the Py3 behavior isn't the same as iteritems(). It actually makes a full sequence-protocol object that also reflects changes to the dict (and is backed by the dict itself, rather than a redundant list)- it's been backported to 2.7 as viewitems()

In Py2.x :

dict.items(), dict.keys() and dict.values() return acopy of the dictionary'slist of(k, v) pair, keys and values, which could takes a lot of memory if the copied list is very large.

dict.iteritems(), dict.iterkeys() and dict.itervalues() return aniterator over the dictionary’s(k, v) pair, keys and values

dict.viewitems(), dict.viewkeys() and dict.viewvalues() return theview objects, which can reflect the dictionary's changes (i.e. if youdel an item or add a(k,v) pair in the dictionary, the view object canautomatically change at the same time.)

While in Py3.x, things are more clean, since there are only

dict.items(), dict.keys() and dict.values() available, which return theview objects just asdict.viewitems() in Py2.x did.

But just as @lvc noted, view object isn't the same asiterator, so if you want to return aniterator in Py3.x, you could useiter(dictview) :


2. ImportError: No module named matplotlib.pyplot

You have 2 pythons installed on your machine, one is the standard python that comes with MacOSX and the second is the one you installed with ports (this is the one that has matplotlib installed in it's library, the one that comes with macosx does not).

/usr/bin/python

Is the standard mac python and since it doesn't have matplotlib you should always start your script with the one installed with ports.

Python import matplotlib.pyplot not working


3. Understanding imports and PYTHONPATH


4. The Quick Guide to Python Eggs


5. The Hitchhiker's Guide to Packaging


6. A module's __name__



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值