Python For loop with increment

huangapple go评论78阅读模式
英文:

Python For loop with increment

问题

我正在尝试在for循环中增加值,使用了带有范围的for循环。但每次都是逐个迭代。建议我应该如何继续进行。

max1 = 0
newList = []
k = 1
for i in range(0, len(arr), k):
    count1 = 0
    count1 = arr.count(arr[i])
    print(arr[i], " count is ", count1)
    if count1 > max1:
        max1 = count1
        newList.clear()
        newList.insert(0, arr[i])
    elif count1 == max1 and arr[i] not in newList:
        max1 = count1
        newList.append(arr[i])
    k = count1
    print(i, " has to skip records for ", k)
英文:

I am trying to increment the value in for loop, By using for with range.
But every time it is iterating one by one. Suggest how can I proceed with this.

max1=0
newList=[] 
k=1
for i in range(0,len(arr),k):
    count1=0
    count1 = arr.count(arr[i])
    print(arr[i]," count is  ",count1)
    if count1>max1:
        max1=count1
        newList.clear()
        newList.insert(0,arr[i])
    elif count1==max1 and arr[i] not in newList:
        max1=count1
        newList.append(arr[i])
    k = count1
    print(i," has to skip records for ",k)

答案1

得分: 1

使用numpy的arange()函数在Python中生成浮点数范围。

numpy的arange()函数的语法:

arange(start, stop, step, dtype)

如果未提供dtype,则从其他输入参数推断数据类型。

示例:

import numpy

for i in numpy.arange(0, 1, 0.1):
    print(i, end=', ')
英文:

Use numpy’s arange() function to generate the range for float numbers in Python.

Syntax of numpy’s arange() function:

arange (start, stop, step, dtype)

If dtype is not given, infer the data type from the other input arguments.

Example:

import numpy

for i in numpy.arange(0, 1, 0.1):
    print(i, end=', ')

huangapple
  • 本文由 发表于 2020年1月3日 23:20:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定