陷入阵列并根据元素单独应用公式的功能,该函数基于“ if”陈述

发布于 2025-02-10 02:04:14 字数 791 浏览 1 评论 0原文

我有一系列的浮子,可以说“ array =([2.0、7.0、8.0、1.0])”,我需要创建一个第二个数组,该数组适用于第一个元素的公式(超出两个),一个 “如果”语句应用于第三个数组上的相应元素(可以说,array3 =([1,4,5,1]))。类似于“如果array3_element为< 3,而不是array2_element = array_element * 2。如果array3_element> 3,而不是array2_element = array_element * array_element * 3“。对于此示例,“数组”的结果将为“ array2 =([4.0,21.0,24.0,2.0])”。

下面的代码是我的最佳尝试,但它无法正常工作。它仅测试具有“ if”语句的第一个元素,并将相同选择的公式应用于角色数组。

As = np.array ([[20], [20]])

def Rsi (x, curv_adi):
    for i in def_aco(x,curv_adi):
        if abs(i) > epsilon_aco:
            return (As/10000)*(Fsd*1000)
        else:
            return (As/10000)*(Fsd*1000)*((def_aco(x,curv_adi))/(epsilon_aco)) 

*def_aco(x,curv_adi)是代码中的一个函数,该函数在“ def_aco =([2.3275,0.1275])中导致,并表示上面示例上的“ array3”。就像“数组”和“ array2”将是函数结果。

I have an array of floats, lets say "array=([2.0, 7.0, 8.0, 1.0])", and I need to create a second array that applies a formula (out of two) to the elements of the first based on a
"if" statement applied to the respective element on a third array (let`s say, array3 = ([1, 4, 5, 1]) ). Something like "If array3_element is < 3, than array2_element = array_element * 2. If array3_element > 3, than array2_element = array_element * 3". For this example,the result for "array" would be "array2=([4.0, 21.0, 24.0, 2.0])".

The code below is my best attempt, but it doesn`t work as expected. It tests only the first element with the "if" statement and applies the same chosen formula to the role array.

As = np.array ([[20], [20]])

def Rsi (x, curv_adi):
    for i in def_aco(x,curv_adi):
        if abs(i) > epsilon_aco:
            return (As/10000)*(Fsd*1000)
        else:
            return (As/10000)*(Fsd*1000)*((def_aco(x,curv_adi))/(epsilon_aco)) 

*def_aco (x, curv_adi) being a function up in the code that results in "def_aco = ([2.3275, 0.1275])" and represents the "array3" on the example above. As is "array" and "array2 would be the function result.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情癖 2025-02-17 02:04:14

让我们将代码转换为一般术语:

def func(array3):
     for i in array3:
        if condition:
            return array1_element
        else:
            return array3_element 

代码的问题是,您只会获得第一个元素,因为您从函数中返回而不是在所有数组上迭代。

您可以通过:

array2 = []
for n, i in enumerate(array3):
    if <condition>:
        array2.append(array[n])
    else:
        array2.append(array3[n])

如果需要,可以使用列表理解:

array2 = [array[n] if <condition> else array3[n] for n, i in enumerate(array3)]

因此,在您的情况下,您可以通过:

def Rsi (x, curv_adi):
    ret = []
    for n, i in enumerate(def_aco(x,curv_adi)):
        if abs(i) > epsilon_aco:
            ret.append((As/10000)*(Fsd*1000))
        else:
            ret.append((As/10000)*(Fsd*1000)*((def_aco(x,curv_adi))/(epsilon_aco)))
    return ret

``````

let's translate your code to general terms:

def func(array3):
     for i in array3:
        if condition:
            return array1_element
        else:
            return array3_element 

The problem with the code is, that you will get only the first element, because you return from the function instead of iterate over all the array.

You can fix it by:

array2 = []
for n, i in enumerate(array3):
    if <condition>:
        array2.append(array[n])
    else:
        array2.append(array3[n])

If you want, you can use list comprehension instead:

array2 = [array[n] if <condition> else array3[n] for n, i in enumerate(array3)]

So, in your case you can solve it by:

def Rsi (x, curv_adi):
    ret = []
    for n, i in enumerate(def_aco(x,curv_adi)):
        if abs(i) > epsilon_aco:
            ret.append((As/10000)*(Fsd*1000))
        else:
            ret.append((As/10000)*(Fsd*1000)*((def_aco(x,curv_adi))/(epsilon_aco)))
    return ret

`

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文