概述
import numpy as np
def Vin(t):
inputs = []
for i in range (1000):
if (-1)**(np.floor( 2 * t[i] )) == 1:
Vin = (1)
inputs.append(Vin)
else:
Vin = (-1)
inputs.append(Vin)
return inputs
when I use this function on a range of t values, I only get one result,
i.e.
input1=Vin(tpoints)
print (input1)
only gives [1], whereas I want the function to do it for every t value.
解决方案
You are returning from with in the for loop,So you would return from the function on first iteration of the loop.
you may re-writ e the function as following
Code
def Vin(t):
inputs = []
for i in range (1000):
if (-1)**(np.floor( 2 * t[i] )) == 1:
inputs.append(1)
else:
inputs.append(-1)
return inputs
Check the indentation of return inputs in the function here .
BTW, your function can be reduced to as more pythonic and efficient code
def Vin(t):
reduce map(lambda x:int((-1)**(np.floor( t[i] < 1))), range (1000))
最后
以上就是敏感鸵鸟为你收集整理的python 循环触发一次,python for循环仅执行一次?的全部内容,希望文章能够帮你解决python 循环触发一次,python for循环仅执行一次?所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复