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

在Python中,作者遇到一个问题:他们有一个for循环遍历enumerate对象,希望每3个元素开始一个新的水平框。由于enumerate从0开始,第一项就会导致立即开始新的行。解决方案是利用enumerate的start参数从1开始计数,从而避免在第一个元素处创建额外的分隔。
摘要由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、付费专栏及课程。

余额充值