Numpy 数组列表未成功附加所有整数

Numpy Array Lists not successfully appending all integers

代码:

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 个数字 - 只记录第一个。

似乎有两个问题。

  1. 您的 append 命令适用于 np.array,但 poslistnumlistlist 类型。对于那些人来说,它只是 numlist.append(num)poslist.append(i)。而不是 numpy.append you want the list version.
  2. 正如 diggusbickus 所指出的,您在 while 循环中的追加语句在 if 语句内,您需要将它们推回,以便它们在 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 对此答案的评论中所建议的那样,如果您想使用 np.concatenate,它看起来更像是 poslist = np.concatenate((poslist, [i,]))。 (比较两种方法。)

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()