Pythonic way to iterate through a range starting at 1

后端 未结 5 866
伪装坚强ぢ
伪装坚强ぢ 2021-01-01 16:04

Currently if I want to iterate 1 through n I would likely use the following method:

for _ in rang         


        
5条回答
  •  臣服心动
    2021-01-01 16:38

    range(1, n+1) is not considered duplication, but I can see that this might become a hassle if you were going to change 1 to another number.

    This removes the duplication using a generator:

    for _ in (number+1 for number in range(5)):
        print(_)
    

提交回复
热议问题