首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >检测python中dictionary.iteritems()的最后一次迭代

检测python中dictionary.iteritems()的最后一次迭代
EN

Stack Overflow用户
提问于 2011-05-23 04:02:14
回答 11查看 12.1K关注 0票数 16

在使用iteritems()遍历字典时,有没有一种简单的方法来检测最后一次迭代

EN

Stack Overflow用户

发布于 2011-05-23 04:18:48

正如其他人所说的,字典没有定义的顺序,所以很难想象为什么需要它,但这里就是它

代码语言:javascript
复制
last = None
for current in your_dict.iteritems():
  if last is not None:
    # process last
  last = current

# now last contains the last thing in dict.iteritems()
if last is not None: # this could happen if the dict was empty
  # process the last item
票数 1
EN
查看全部 11 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6090417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档