How do I use a C-style for loop in Python?

后端 未结 7 1607
南旧
南旧 2020-12-01 06:17

I want to use the traditional C-style for loop in Python. I want to loop through characters of a string, but also know what it is, and be able to jump through characters (e.

相关标签:
7条回答
  • 2020-12-01 07:02

    You can do the following, given an array a:

    for i in range(len(a)):
      a[i] = i
    

    That's the closest Python can get to C-style loops.

    You can also give the range command more arguments; for example,

    for i in range(2, len(a), 3)

    will start at i = 2, and increment it by 3 as long as the result is less than len(a).

    0 讨论(0)
提交回复
热议问题