英文:
How to doing dynamic calculation in python
问题
如何使奇数索引执行(-),偶数索引执行(+),最大迭代次数为6。迭代1 +10,迭代2 -20,迭代3 +30,迭代4 -40,迭代5 +50,迭代6 -60
import numpy as np
AA = np.array([[9.27914],
               [9.33246],
               [9.26303],
               [9.30597],
               [9.6594],
               [9.04283],
               [8.88866],
               [8.89956]])
max_iter = 6
for i in range(len(AA)):
    if i % 2 == 0:
        AA[i][0] = AA[i][0] + (i // 2 + 1) * 10
    else:
        AA[i][0] = AA[i][0] - (i // 2 + 1) * 10
AA = np.round(AA, 5)  # 四舍五入到五位小数
print("expected results:")
print("AA=np.array([")
for i in range(len(AA)):
    print(f"    [{AA[i][0]:.5f}],")
print("])")
预期结果:
AA=np.array([
    [19.27914],
    [-10.66754],
    [39.26303],
    [-30.69403],
    [59.6594],
    [-50.95717],
    [18.88866],
    [-11.10044],
])
这段代码将按照您的要求对数组进行修改,使奇数索引执行减法,偶数索引执行加法,最大迭代次数为6,每次迭代加减10。希望这可以帮助您得到期望的结果。
英文:
How to make it like that so odd indexes will be doing (-) and even indexes will do (+) The max iteration is 6. iteration 1 +10, iteration 2 -20, iteration 3 +30, iteration 4 -40, iteration 5 + 50, iteration 6 -60
AA = np.array([[9.27914]+10,
              [9.33246]-20,
              [9.26303]+30,
              [9.30597]-40,
              [9.6594 ]+50,
              [9.04283]-60,
              [8.88866]+10,
              [8.89956]-20])
expected results:
AA=np.array([
    [19.27914],
    [-10.66754],
    [39.26303],
    [-30.69403],
    [59.6594],
    [-50.95717],
    [18.88866],
    [-11.10044],
])
I try use this code but not working
max_iter = 6
iter = 0
for i in range(len(AA)):
    if i % 2 == 0:
        AA[i][0] = AA[i][0] + (iter % max_iter)
    else:
       AA[i][0] = AA[i][0] - (iter % max_iter)
    iter += 10
答案1
得分: 2
这是[tag:numpy],不要循环,使用矢量化代码。
取模运算符(%)非常有用,可以重新启动一个序列。
输出:
array([[ 19.27914],
       [-10.66754],
       [ 39.26303],
       [-30.69403],
       [ 59.6594 ],
       [-50.95717],
       [ 18.88866],
       [-11.10044]])
使用的输入:
AA = np.array([[9.27914],
               [9.33246],
               [9.26303],
               [9.30597],
               [9.6594 ],
               [9.04283],
               [8.88866],
               [8.89956]])
中间步骤:
(a%6+1)*10
array([[10],
       [20],
       [30],
       [40],
       [50],
       [60],
       [10],
       [20]])
np.where(a%2, -1, 1)
array([[ 1],
       [-1],
       [ 1],
       [-1],
       [ 1],
       [-1],
       [ 1],
       [-1]])
英文:
This is [tag:numpy], don't loop, use vectorial code.
The modulo operator (%) is quite useful to restart a sequence.
max_iter = 6
a = np.arange(len(AA))[:,None]
out = AA + (a%max_iter+1)*10 * np.where(a%2, -1, 1)
Output:
array([[ 19.27914],
       [-10.66754],
       [ 39.26303],
       [-30.69403],
       [ 59.6594 ],
       [-50.95717],
       [ 18.88866],
       [-11.10044]])
Used input:
AA = np.array([[9.27914],
               [9.33246],
               [9.26303],
               [9.30597],
               [9.6594 ],
               [9.04283],
               [8.88866],
               [8.89956]])
Intermediates:
(a%6+1)*10
array([[10],
       [20],
       [30],
       [40],
       [50],
       [60],
       [10],
       [20]])
np.where(a%2, -1, 1)
array([[ 1],
       [-1],
       [ 1],
       [-1],
       [ 1],
       [-1],
       [ 1],
       [-1]])
答案2
得分: 1
你已经非常接近了。只需进行3个小的更改。我在括号内添加了+1,对每个数组操作添加了*10,并将iter += 10更改为array += 1。
max_iter = 6
array = 0
for i in range(len(AA)):
    if i % 2 == 0:
        AA[i][0] = AA[i][0] + (array % max_iter + 1) * 10
    else:
        AA[i][0] = AA[i][0] - (array % max_iter + 1) * 10
    array += 1
实际上,你可以删除if else语句,如果使用以下代码可以在一行中完成:
AA[i][0] = AA[i][0] + (-1) ** (i) * (array % max_iter + 1) * 10
英文:
You were very close. Just had to make 3 small changes. I added a +1 inside the parenthesis, added *10 for each of the array operations and change iter += 10 to array += 1
max_iter = 6
iter = 0
for i in range(len(AA)):
    if i % 2 == 0:
        AA[i][0] = AA[i][0] + (iter % max_iter+1)*10
    else:
        AA[i][0] = AA[i][0] - (iter % max_iter+1)*10
    iter += 1
In fact, you can remove the if else statement and do it in a single line if you use the following:
AA[i][0] = AA[i][0] +(-1)**(i)* (iter % max_iter+1)*10
答案3
得分: 0
请尝试以下代码:
AA = [
    9.27914,
    9.33246,
    9.26303,
    9.30597,
    9.6594,
    9.04283,
    8.88866,
    8.89956
]
BB=[]
iter = 10
for i in range(len(AA)):
    if i % 2 == 0:
        var = AA[i] + (iter)
    else:
       var = AA[i] - (iter)
    BB.append(var)
    iter += 10
    
for i in range(len(BB)):
    print(BB[i])
注意:最大迭代次数被省略了...将尽快修复。
英文:
Try this:
AA = [
    9.27914,
    9.33246,
    9.26303,
    9.30597,
    9.6594,
    9.04283,
    8.88866,
    8.89956
]
BB=[]
iter = 10
for i in range(len(AA)):
    if i % 2 == 0:
        var = AA[i] + (iter)
    else:
       var = AA[i] - (iter)
    BB.append(var)
    iter += 10
    
for i in range(len(BB)):
    print(BB[i])
Note: left out max iterations... will fix asap.
答案4
得分: 0
你目前的根本问题是混淆了变量 iter 的含义。有时似乎你想要它是10的倍数,有时又是1到6之间的数字。让我们将其分离并稍微清理一下代码。
给定:
import numpy
AA = numpy.array([
    [9.27914],
    [9.33246],
    [9.26303],
    [9.30597],
    [9.6594 ],
    [9.04283],
    [8.88866],
    [8.89956]
])
然后我们可以这样做:
max_iter = 6
iter = 1
for i in range(len(AA)):
    delta = 10 * (iter % max_iter)
    sign = 1 if i % 2 == 0 else -1
    AA[i][0] += sign * delta
    iter += 1
print(AA)
以获得:
[
 [ 19.27914]
 [-10.66754]
 [ 39.26303]
 [-30.69403]
 [ 59.6594 ]
 [  9.04283]
 [ 18.88866]
 [-11.10044]
]
或者如果你更喜欢接近你最初的代码:
max_iter = 6
iter = 1
for i in range(len(AA)):
    if i % 2 == 0:
        AA[i][0] += 10 * (iter % max_iter)
    else:
        AA[i][0] -= 10 * (iter % max_iter)
    iter += 1
print(AA)
英文:
The root issue you have is a muddling of the meaning of your variable iter. Sometimes you seems to want to be a multiple of 10 and sometimes a number between 1 and 6.  Let's separate that out and clean up the code a bit.
Given:
import numpy
AA = numpy.array([
    [9.27914],
    [9.33246],
    [9.26303],
    [9.30597],
    [9.6594 ],
    [9.04283],
    [8.88866],
    [8.89956]
])
Then we can do:
max_iter = 6
iter = 1
for i in range(len(AA)):
    delta = 10 * (iter % max_iter)
    sign = 1 if i % 2 == 0 else -1
    AA[i][0] += sign * delta
    iter += 1
print(AA)
to get:
[
 [ 19.27914]
 [-10.66754]
 [ 39.26303]
 [-30.69403]
 [ 59.6594 ]
 [  9.04283]
 [ 18.88866]
 [-11.10044]
]
or if you like something closer to what you started with:
max_iter = 6
iter = 1
for i in range(len(AA)):
    if i % 2 == 0:
        AA[i][0] += 10 * (iter % max_iter)
    else:
        AA[i][0] -= 10 * (iter % max_iter)
    iter += 1
print(AA)
答案5
得分: 0
以下是已翻译的代码部分:
我为你编写了一个不使用循环并能产生所需结果的函数。
    AA = np.array([[9.27914],
              [9.33246],
              [9.26303],
              [9.30597],
              [9.6594 ],
              [9.04283],
              [8.88866],
              [8.89956]])
    expected_output = np.array([[19.27914],
              [-10.66754],
              [39.26303],
              [-30.69403],
              [59.6594],
              [-50.95717],
              [18.88866],
              [-11.10044],
    ])
    def test(AA, iterstep, maxiter):
        aa_shape = AA.shape
        arr1 = np.tile(np.arange(1, max_iter+1), (aa_shape[0] // maxiter) + 1)[:aa_shape[0]] 
        sign_arr = np.tile(np.array([1, -1]), (aa_shape[0] // 2) + 1)[:aa_shape[0]]
        _ = (arr1*sign_arr)*10
        _=_.reshape(AA.shape)
        return AA + _
    res = test(AA, 10, 6)
    # 测试
    assert (np.all(np.isclose(res, expected_output)))
英文:
I write for you a function that works without loops and give you desired result.
AA = np.array([[9.27914],
          [9.33246],
          [9.26303],
          [9.30597],
          [9.6594 ],
          [9.04283],
          [8.88866],
          [8.89956]])
expected_output = np.array([[19.27914],
          [-10.66754],
          [39.26303],
          [-30.69403],
          [59.6594],
          [-50.95717],
          [18.88866],
          [-11.10044],
])
def test(AA, iterstep, maxiter):
    aa_shape = AA.shape
    arr1 = np.tile(np.arange(1, max_iter+1), (aa_shape[0] // maxiter) + 1)[:aa_shape[0]] 
    sign_arr = np.tile(np.array([1, -1]), (aa_shape[0] // 2) + 1)[:aa_shape[0]]
    _ = (arr1*sign_arr)*10
    _=_.reshape(AA.shape)
    return AA + _
res = test(AA, 10, 6)
# Test 
assert (np.all(np.isclose(res, expected_output)))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论