Python - 如何强制枚举从1开始 - 或解决方法? [英] Python -- how to force enumerate to start at 1 -- or workaround?

查看:434
本文介绍了Python - 如何强制枚举从1开始 - 或解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  for index,(key,value)in enumerate(self.addArgs [ khzObj]。nodes.items()):

我想开始一个新的wx horizo​​ntal boxsizer在每个第三项之后创建一个每个节点有3个节点的面板,然后继续节点中的节点。显而易见的解决方案是:

pre $ code如果索引%3 == 0:添加一个水平间隔符

但是枚举从0开始,所以0%3 == 0,它将立即开始一个新的行。我已经试过了:

  if index == 0:index = index + 1 

但当然这不起作用,因为它创建了一个新的var而不是改变原来的 - 所以我得到了1,1,2, 3,4等,这将无法正常工作,因为我会得到4个节点之前,我打了一个索引%3 == 0.



有关如何做的任何建议这个?这不是一个很大的枚举,通常只有10-15项左右。感谢。

解决方案

自Python 2.6开始, enumerate() start 参数来指示枚举的起始位置。请参阅 enumerate 的文档


I have a simple for loop:

for index, (key, value) in enumerate(self.addArgs["khzObj"].nodes.items()):

and I want to start a new wx horizontal boxsizer after every 3rd item to create a panel with 3 nodes each and going on for as many as are in nodes. The obvious solution is to do:

if index % 3 == 0: add a horizontal spacer

but the enumerate starts at 0, so 0 % 3 == 0 and it would start a new row right off the bat. I've tried doing:

if index == 0: index = index + 1

but of course that doesn't work because it creates a new var instead of changing the original -- so I get 1, 1, 2, 3, 4, etc and that won't work because I'll get 4 nodes before I hit a index % 3 == 0.

Any suggestions on how to do this? This isn't a big enumerate, usually only about 10-15 items. Thanks.

解决方案

Since Python 2.6, enumerate() takes an optional start parameter to indicate where to start the enumeration. See the documentation for enumerate.

这篇关于Python - 如何强制枚举从1开始 - 或解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆