python enumeration,Python - 如何强制枚举从1开始 - 或解决方法?

在Python中,遇到一个for循环问题,使用enumerate遍历字典的节点,想每3个节点创建一个新的水平布局。由于enumerate默认从0开始,导致第一轮就会创建新行。了解到enumerate有一个start参数可以指定起始值,通过设置start=1来避免第一轮就创建新行的问题。这是一个关于Python编程和界面布局的小技巧。
摘要由CSDN通过智能技术生成

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.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值