英文:
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=', ')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论