如何在Python中进行动态计算

huangapple go评论98阅读模式
英文:

How to doing dynamic calculation in python

问题

如何使奇数索引执行(-),偶数索引执行(+),最大迭代次数为6。迭代1 +10,迭代2 -20,迭代3 +30,迭代4 -40,迭代5 +50,迭代6 -60

  1. import numpy as np
  2. AA = np.array([[9.27914],
  3. [9.33246],
  4. [9.26303],
  5. [9.30597],
  6. [9.6594],
  7. [9.04283],
  8. [8.88866],
  9. [8.89956]])
  10. max_iter = 6
  11. for i in range(len(AA)):
  12. if i % 2 == 0:
  13. AA[i][0] = AA[i][0] + (i // 2 + 1) * 10
  14. else:
  15. AA[i][0] = AA[i][0] - (i // 2 + 1) * 10
  16. AA = np.round(AA, 5) # 四舍五入到五位小数
  17. print("expected results:")
  18. print("AA=np.array([")
  19. for i in range(len(AA)):
  20. print(f" [{AA[i][0]:.5f}],")
  21. print("])")

预期结果:

  1. AA=np.array([
  2. [19.27914],
  3. [-10.66754],
  4. [39.26303],
  5. [-30.69403],
  6. [59.6594],
  7. [-50.95717],
  8. [18.88866],
  9. [-11.10044],
  10. ])

这段代码将按照您的要求对数组进行修改,使奇数索引执行减法,偶数索引执行加法,最大迭代次数为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

  1. AA = np.array([[9.27914]+10,
  2. [9.33246]-20,
  3. [9.26303]+30,
  4. [9.30597]-40,
  5. [9.6594 ]+50,
  6. [9.04283]-60,
  7. [8.88866]+10,
  8. [8.89956]-20])

expected results:

  1. AA=np.array([
  2. [19.27914],
  3. [-10.66754],
  4. [39.26303],
  5. [-30.69403],
  6. [59.6594],
  7. [-50.95717],
  8. [18.88866],
  9. [-11.10044],
  10. ])

I try use this code but not working

  1. max_iter = 6
  2. iter = 0
  3. for i in range(len(AA)):
  4. if i % 2 == 0:
  5. AA[i][0] = AA[i][0] + (iter % max_iter)
  6. else:
  7. AA[i][0] = AA[i][0] - (iter % max_iter)
  8. iter += 10

答案1

得分: 2

这是[tag:numpy],不要循环,使用矢量化代码。

取模运算符(%)非常有用,可以重新启动一个序列。

输出:

  1. array([[ 19.27914],
  2. [-10.66754],
  3. [ 39.26303],
  4. [-30.69403],
  5. [ 59.6594 ],
  6. [-50.95717],
  7. [ 18.88866],
  8. [-11.10044]])

使用的输入:

  1. AA = np.array([[9.27914],
  2. [9.33246],
  3. [9.26303],
  4. [9.30597],
  5. [9.6594 ],
  6. [9.04283],
  7. [8.88866],
  8. [8.89956]])

中间步骤:

  1. (a%6+1)*10
  2. array([[10],
  3. [20],
  4. [30],
  5. [40],
  6. [50],
  7. [60],
  8. [10],
  9. [20]])
  10. np.where(a%2, -1, 1)
  11. array([[ 1],
  12. [-1],
  13. [ 1],
  14. [-1],
  15. [ 1],
  16. [-1],
  17. [ 1],
  18. [-1]])
英文:

This is [tag:numpy], don't loop, use vectorial code.

The modulo operator (%) is quite useful to restart a sequence.

  1. max_iter = 6
  2. a = np.arange(len(AA))[:,None]
  3. out = AA + (a%max_iter+1)*10 * np.where(a%2, -1, 1)

Output:

  1. array([[ 19.27914],
  2. [-10.66754],
  3. [ 39.26303],
  4. [-30.69403],
  5. [ 59.6594 ],
  6. [-50.95717],
  7. [ 18.88866],
  8. [-11.10044]])

Used input:

  1. AA = np.array([[9.27914],
  2. [9.33246],
  3. [9.26303],
  4. [9.30597],
  5. [9.6594 ],
  6. [9.04283],
  7. [8.88866],
  8. [8.89956]])

Intermediates:

  1. (a%6+1)*10
  2. array([[10],
  3. [20],
  4. [30],
  5. [40],
  6. [50],
  7. [60],
  8. [10],
  9. [20]])
  10. np.where(a%2, -1, 1)
  11. array([[ 1],
  12. [-1],
  13. [ 1],
  14. [-1],
  15. [ 1],
  16. [-1],
  17. [ 1],
  18. [-1]])

答案2

得分: 1

你已经非常接近了。只需进行3个小的更改。我在括号内添加了+1,对每个数组操作添加了*10,并将iter += 10更改为array += 1。

  1. max_iter = 6
  2. array = 0
  3. for i in range(len(AA)):
  4. if i % 2 == 0:
  5. AA[i][0] = AA[i][0] + (array % max_iter + 1) * 10
  6. else:
  7. AA[i][0] = AA[i][0] - (array % max_iter + 1) * 10
  8. array += 1

实际上,你可以删除if else语句,如果使用以下代码可以在一行中完成:

  1. 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

  1. max_iter = 6
  2. iter = 0
  3. for i in range(len(AA)):
  4. if i % 2 == 0:
  5. AA[i][0] = AA[i][0] + (iter % max_iter+1)*10
  6. else:
  7. AA[i][0] = AA[i][0] - (iter % max_iter+1)*10
  8. iter += 1

In fact, you can remove the if else statement and do it in a single line if you use the following:

  1. AA[i][0] = AA[i][0] +(-1)**(i)* (iter % max_iter+1)*10

答案3

得分: 0

请尝试以下代码:

  1. AA = [
  2. 9.27914,
  3. 9.33246,
  4. 9.26303,
  5. 9.30597,
  6. 9.6594,
  7. 9.04283,
  8. 8.88866,
  9. 8.89956
  10. ]
  11. BB=[]
  12. iter = 10
  13. for i in range(len(AA)):
  14. if i % 2 == 0:
  15. var = AA[i] + (iter)
  16. else:
  17. var = AA[i] - (iter)
  18. BB.append(var)
  19. iter += 10
  20. for i in range(len(BB)):
  21. print(BB[i])

注意:最大迭代次数被省略了...将尽快修复。

英文:

Try this:

  1. AA = [
  2. 9.27914,
  3. 9.33246,
  4. 9.26303,
  5. 9.30597,
  6. 9.6594,
  7. 9.04283,
  8. 8.88866,
  9. 8.89956
  10. ]
  11. BB=[]
  12. iter = 10
  13. for i in range(len(AA)):
  14. if i % 2 == 0:
  15. var = AA[i] + (iter)
  16. else:
  17. var = AA[i] - (iter)
  18. BB.append(var)
  19. iter += 10
  20. for i in range(len(BB)):
  21. print(BB[i])

Note: left out max iterations... will fix asap.

答案4

得分: 0

你目前的根本问题是混淆了变量 iter 的含义。有时似乎你想要它是10的倍数,有时又是1到6之间的数字。让我们将其分离并稍微清理一下代码。

给定:

  1. import numpy
  2. AA = numpy.array([
  3. [9.27914],
  4. [9.33246],
  5. [9.26303],
  6. [9.30597],
  7. [9.6594 ],
  8. [9.04283],
  9. [8.88866],
  10. [8.89956]
  11. ])

然后我们可以这样做:

  1. max_iter = 6
  2. iter = 1
  3. for i in range(len(AA)):
  4. delta = 10 * (iter % max_iter)
  5. sign = 1 if i % 2 == 0 else -1
  6. AA[i][0] += sign * delta
  7. iter += 1
  8. print(AA)

以获得:

  1. [
  2. [ 19.27914]
  3. [-10.66754]
  4. [ 39.26303]
  5. [-30.69403]
  6. [ 59.6594 ]
  7. [ 9.04283]
  8. [ 18.88866]
  9. [-11.10044]
  10. ]

或者如果你更喜欢接近你最初的代码:

  1. max_iter = 6
  2. iter = 1
  3. for i in range(len(AA)):
  4. if i % 2 == 0:
  5. AA[i][0] += 10 * (iter % max_iter)
  6. else:
  7. AA[i][0] -= 10 * (iter % max_iter)
  8. iter += 1
  9. 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:

  1. import numpy
  2. AA = numpy.array([
  3. [9.27914],
  4. [9.33246],
  5. [9.26303],
  6. [9.30597],
  7. [9.6594 ],
  8. [9.04283],
  9. [8.88866],
  10. [8.89956]
  11. ])

Then we can do:

  1. max_iter = 6
  2. iter = 1
  3. for i in range(len(AA)):
  4. delta = 10 * (iter % max_iter)
  5. sign = 1 if i % 2 == 0 else -1
  6. AA[i][0] += sign * delta
  7. iter += 1
  8. print(AA)

to get:

  1. [
  2. [ 19.27914]
  3. [-10.66754]
  4. [ 39.26303]
  5. [-30.69403]
  6. [ 59.6594 ]
  7. [ 9.04283]
  8. [ 18.88866]
  9. [-11.10044]
  10. ]

or if you like something closer to what you started with:

  1. max_iter = 6
  2. iter = 1
  3. for i in range(len(AA)):
  4. if i % 2 == 0:
  5. AA[i][0] += 10 * (iter % max_iter)
  6. else:
  7. AA[i][0] -= 10 * (iter % max_iter)
  8. iter += 1
  9. print(AA)

答案5

得分: 0

以下是已翻译的代码部分:

  1. 我为你编写了一个不使用循环并能产生所需结果的函数
  2. AA = np.array([[9.27914],
  3. [9.33246],
  4. [9.26303],
  5. [9.30597],
  6. [9.6594 ],
  7. [9.04283],
  8. [8.88866],
  9. [8.89956]])
  10. expected_output = np.array([[19.27914],
  11. [-10.66754],
  12. [39.26303],
  13. [-30.69403],
  14. [59.6594],
  15. [-50.95717],
  16. [18.88866],
  17. [-11.10044],
  18. ])
  19. def test(AA, iterstep, maxiter):
  20. aa_shape = AA.shape
  21. arr1 = np.tile(np.arange(1, max_iter+1), (aa_shape[0] // maxiter) + 1)[:aa_shape[0]]
  22. sign_arr = np.tile(np.array([1, -1]), (aa_shape[0] // 2) + 1)[:aa_shape[0]]
  23. _ = (arr1*sign_arr)*10
  24. _=_.reshape(AA.shape)
  25. return AA + _
  26. res = test(AA, 10, 6)
  27. # 测试
  28. assert (np.all(np.isclose(res, expected_output)))
英文:

I write for you a function that works without loops and give you desired result.

  1. AA = np.array([[9.27914],
  2. [9.33246],
  3. [9.26303],
  4. [9.30597],
  5. [9.6594 ],
  6. [9.04283],
  7. [8.88866],
  8. [8.89956]])
  9. expected_output = np.array([[19.27914],
  10. [-10.66754],
  11. [39.26303],
  12. [-30.69403],
  13. [59.6594],
  14. [-50.95717],
  15. [18.88866],
  16. [-11.10044],
  17. ])
  18. def test(AA, iterstep, maxiter):
  19. aa_shape = AA.shape
  20. arr1 = np.tile(np.arange(1, max_iter+1), (aa_shape[0] // maxiter) + 1)[:aa_shape[0]]
  21. sign_arr = np.tile(np.array([1, -1]), (aa_shape[0] // 2) + 1)[:aa_shape[0]]
  22. _ = (arr1*sign_arr)*10
  23. _=_.reshape(AA.shape)
  24. return AA + _
  25. res = test(AA, 10, 6)
  26. # Test
  27. assert (np.all(np.isclose(res, expected_output)))

huangapple
  • 本文由 发表于 2023年6月1日 22:21:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定