【问题标题】:Numpy Array Lists not successfully appending all integersNumpy 数组列表未成功附加所有整数
【发布时间】:2021-12-20 04:10:45
【问题描述】:

代码:

import matplotlib.pyplot as plt
import numpy as np

poslist = []
numlist = []
i = 1

num = int(input('Number to check?'))
plt.title(str(num) + ': Collatz Graph')
plt.xlabel('Step')
plt.ylabel('Value')
print(num)
poslist.append(poslist, i)
numlist.append(numlist, num)

while True:
    i = i + 1
    if num % 2 == 0:
        num = num // 2
        print(num)
    elif num == 1:
        break
    else:
        num = num * 3
        num = num + 1
        print(num)
        poslist.append(poslist, i)
        numlist.append(numlist, num)

print('Number Array:', numlist)
print('Position Array', poslist)
plt.plot(*(poslist, numlist))
plt.show()

当我使用listname.append(listname, var) NumPy 函数时,它会跳过大部分变量。它只会将每 3 个或第 4 个变量添加到列表中。对于计步器阵列也是如此。即使添加 time.sleep(4) 函数,它似乎也一次计算 3 或 4 个数字 - 只记录第一个。

【问题讨论】:

  • 更正您的代码并消除最后两个附加内容
  • 我对它的运行感到惊讶。 alist.append(alist,1) 引发 TypeError,参数太多。正确的语法是alist.append(1)

标签: python-3.x numpy matplotlib timing


【解决方案1】:

似乎有两个问题。

  1. 您的append 命令适用于np.array,但poslistnumlistlist 类型。对于那些它只是numlist.append(num)poslist.append(i)。你想要的是list version,而不是numpy.append
  2. 正如 diggusbiccus 指出的那样,您在 while 循环中的 append 语句位于 if 语句中,您需要将它们推回以使其位于其外部。
import matplotlib.pyplot as plt
#import numpy as np <--- You are not using numpy

poslist = []
numlist = []
i = 1

num = int(input('Number to check?'))
plt.title(str(num) + ': Collatz Graph')
plt.xlabel('Step')
plt.ylabel('Value')
print(num)
poslist.append(i) # <--- Change append statement for list
numlist.append(num) # <--- Same as above

while True:
    i = i + 1
    if num % 2 == 0:
        num = num // 2
        print(num)
    elif num == 1:
        break
    else:
        num = num * 3
        num = num + 1
        print(num)
    poslist.append(i) # <--- Shift out of if/else statement and change append
    numlist.append(num) # <--- Same as above

print('Number Array:', numlist)
print('Position Array', poslist)
plt.plot(*(poslist, numlist))
plt.show()

如果您想使用 numpy 数组,另一个选择是更改您的列表。如果想使用np.append,您可以使用poslist = np.append(poslist, i) 之类的东西。但是,正如 hpaulj 对此答案的 cmets 所建议的那样,如果您想使用np.concatenate,它看起来更像poslist = np.concatenate((poslist, [i,]))。 (This post 比较这两种方法。)

import matplotlib.pyplot as plt
import numpy as np 

poslist = np.array([]) # <--- change list to array
numlist = np.array([]) # <--- change list to array
i = 1

num = int(input('Number to check?'))
plt.title(str(num) + ': Collatz Graph')
plt.xlabel('Step')
plt.ylabel('Value')
print(num)

#poslist = np.append(poslist, i) # <--- Fix syntax of np.append
poslist = np.concatenate((poslist, [i,])) # <-- Or use concatenate instead
numlist = np.concatenate((numlist, [num,])) # <-- Or use concatenate instead

while True:
    i = i + 1
    if num % 2 == 0:
        num = num // 2
        print(num)
    elif num == 1:
        break
    else:
        num = num * 3
        num = num + 1
        print(num)
    poslist = np.concatenate((poslist, [i,])) # <--- Move out of if/else statement
    numlist = np.concatenate((numlist, [num,])) 

print('Number Array:', numlist)
print('Position Array', poslist)
plt.plot(*(poslist, numlist))
plt.show()

【讨论】:

  • 我们不鼓励使用np.append,尤其是在循环中。
  • @hpaulj 我可以理解为什么这不应该在循环中完成,但评论暗示它总是不鼓励。具体来说,为什么不鼓励它?我们是谁?
  • ``np.append` 是np.concatenate 的封面。太多新手尝试将其用作列表追加克隆,会出错并降低性能。将标量添加到一维数组时可以,因为它将标量转换为连接可以使用的数组。除此之外,我鼓励使用 concatenate 或其stack 封面之一。
  • 谢谢@hpaulj,我根据您的反馈编辑了我的答案,建议np.concatenate。您建议,既然我们添加了一个标量,这里就可以了,但希望这将鼓励长期更好的实践。 (这是我喜欢 SO 的原因之一,即使您不寻找答案也可以继续学习!)
猜你喜欢
  • 2013-11-07
  • 2021-02-21
  • 2022-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-22
相关资源
最近更新 更多