我是靠谱客的博主 敏感鸵鸟,这篇文章主要介绍python 循环触发一次,python for循环仅执行一次?,现在分享给大家,希望可以做个参考。

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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(141)

评论列表共有 0 条评论

立即
投稿
返回
顶部