dict.items()vs dict.iteritems和类似的问题 [英] dict.items() vs dict.iteritems and similar questions
问题描述
何时使用dict.items()vs dict.iteritems是合适的。
似乎适用于以下内容:
表示密钥,val表示mydict.items():
打印密钥, val
for key,val in mydict.iteritems():
print key,val
此外,什么时候使用range()vs xrange()是合适的。从我对
的理解,xrange()基本上给你一个跨越
范围的迭代器,所以它应该在迭代时使用。你是否只想使用
range()想要将范围实际存储为列表?
谢谢,
画了
When is it appropriate to use dict.items() vs dict.iteritems. Both
seem to work for something like:
for key,val in mydict.items():
print key,val
for key,val in mydict.iteritems():
print key,val
Also, when is it appropriate to use range() vs xrange(). From my
understanding, xrange() essentially gives you an iterator across a
range, so it should be used when iterating. Should you only use
range() when want to physically store the range as a list?
Thanks,
Drew
推荐答案
画了一个écrit:
Drew a écrit :
什么时候适合使用dict.items()vs dict.iteritems。
似乎适用于以下内容:
表示密钥,val表示mydict.items():
打印密钥, val
for key,val in mydict.iteritems():
print key,val
此外,什么时候使用range()vs xrange()是合适的。从我对
的理解,xrange()基本上给你一个跨越
范围的迭代器,所以它应该在迭代时使用。当你想要将范围物理存储为列表时,你应该只使用
range()吗?
When is it appropriate to use dict.items() vs dict.iteritems. Both
seem to work for something like:
for key,val in mydict.items():
print key,val
for key,val in mydict.iteritems():
print key,val
Also, when is it appropriate to use range() vs xrange(). From my
understanding, xrange() essentially gives you an iterator across a
range, so it should be used when iterating. Should you only use
range() when want to physically store the range as a list?
iteritems和xrange仅在请求时提供值。
项目和范围构建完成列表被调用。
两者都有效,你可能更喜欢xrange / iteritems用于迭代大型
集合,你可能更喜欢范围/项目处理结果时
值明确需要一个列表(例如计算它的长度)或者当你是
来操纵循环中的原始容器时。
A +
>
Laurent。
iteritems and xrange only provide values when requested.
items and range build complete list when called.
Both work, you may prefer xrange/iteritems for iteration on large
collections, you may prefer range/items when processing of the result
value explicitly need a list (ex. calculate its length) or when you are
going to manipulate the original container in the loop.
A+
Laurent.
3月14日上午11点44分,Laurent Pointal< laurent.poin ... @ limsi.frwrote:
On Mar 14, 11:44 am, Laurent Pointal <laurent.poin...@limsi.frwrote:
两者都有效,你可能更喜欢xrange / iteritems用于迭代大型
集合,你可能在处理结果时更喜欢范围/项目
值明确需要一个列表(例如计算它的长度)或者当你在操作循环中的原始容器时使用
。
A +
洛朗。
Both work, you may prefer xrange/iteritems for iteration on large
collections, you may prefer range/items when processing of the result
value explicitly need a list (ex. calculate its length) or when you are
going to manipulate the original container in the loop.
A+
Laurent.
Laurent -
非常有帮助,正是我所寻找的。
谢谢,
画了
Laurent -
Extremely helpful, exactly what I was looking for.
Thanks,
Drew
#只要看一下输出,xrange就好像很明显
对于大范围,
将更具记忆效率:
print" with range():",range(100,200)
print" with xrange():",xrange(100,200)
d = {1:2,2:3,3:4}
d.items()
d.iteritems()
#我一直很想使用Pysizer(需要修补Python)
来证明差异。
Drew写道:
# Just by looking at the output, it seems pretty obvious that xrange
would be more memory effcient for large ranges:
print "With range():",range(100,200)
print "With xrange():",xrange(100,200)
d = {1:2,2:3,3:4}
d.items()
d.iteritems()
# I have been curious to use Pysizer (which requires patching Python)
to demonstrate the difference.
Drew wrote:
什么时候适合使用dict.items()vs dict.iteritems。
似乎适用于以下内容:
表示密钥,val表示mydict.items():
打印密钥, val
for key,val in mydict.iteritems():
print key,val
此外,什么时候使用range()vs xrange()是合适的。从我对
的理解,xrange()基本上给你一个跨越
范围的迭代器,所以它应该在迭代时使用。你是否只想使用
range()想要将范围实际存储为列表?
谢谢,
画了
When is it appropriate to use dict.items() vs dict.iteritems. Both
seem to work for something like:
for key,val in mydict.items():
print key,val
for key,val in mydict.iteritems():
print key,val
Also, when is it appropriate to use range() vs xrange(). From my
understanding, xrange() essentially gives you an iterator across a
range, so it should be used when iterating. Should you only use
range() when want to physically store the range as a list?
Thanks,
Drew
-
Shane Geiger
IT总监
National经济教育委员会
sg*****@ncee.net | 402-438-8958 | http://www.ncee.net
领导经济和金融扫盲运动
--
Shane Geiger
IT Director
National Council on Economic Education
sg*****@ncee.net | 402-438-8958 | http://www.ncee.net
Leading the Campaign for Economic and Financial Literacy
这篇关于dict.items()vs dict.iteritems和类似的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!