如何将for循环的结果存储为数组?

时间:2019-06-25 22:21:00

标签: python python-3.x numpy

在对随机数执行功能之前,我需要生成一个随机数列表,并将原始随机数和结果存储为数组中的元组。

# Here is how I'm generating my random integers
celsius = random.sample (range(-10, 40), 35)
print (celsius)

# This is how I store them as an array
array = numpy.array(celsius)
print(celsius)

# This is how I am trying to list my pairs in an array
for n in celsius:
    f = (float(n * 1.8 + 32))
    pairs = (n, f)
    numpy.array(pairs)
    print(pairs)

我得到了成对的列表,但是当我再次打印时,它会打印最后一对,而不是整个列表。另外,当我检查时,长度被列为两个。

理想情况下,这些对将是数组中的元组。任何帮助,不胜感激!

4 个答案:

答案 0 :(得分:3)

这就是我要做的:

import numpy as np

# isolate the wisdom related to the conversion in a function
def celsius2fahrenheit(x):
    return (x * 1.8 + 32)

# generate random values
celsius_arr = np.random.randint(-10, 40, 35)

# compute the converted values
fahrenheit_arr = celsius2fahrenheit(celsius_arr)

# stack them together
pairs_arr = np.stack([celsius_arr, fahrenheit_arr])

使用显式循环执行此操作会给NumPy数组带来次佳的性能,特别是如果您不事先分配内存。

不过,只是为了说明如何做到这一点:

import numpy as np

# same as before
def celsius2fahrenheit(x):
    return (x * 1.8 + 32)

# allocate the memory
pair_arr = np.zeros(2, 35)
for i in range(35):
    # generate a random number
    x = np.random.randint(-10, 40)
    # store its value and the converted value
    pair_arr[:, i] = x, celsius2fahrenheit(x)

最后,您可以使用普通的Python list,这些Python更适合于动态增长的序列:

import random

# same as before
def celsius2fahrenheit(x):
    return (x * 1.8 + 32)

# Option 1: all in a single loop
pairs = []
for _ in range(35):
    x = random.randint(-10, 40)
    pairs.append([x, celsius2fahrenheit(x)])

# Option 2: create two lists to join later
celsius_values = [random.randint(-10, 40) for _ in range(35)]
fahrenheit_values = [celsius2fahrenheit(x) for x in celsius_values]
pair_values = list(zip(celsius_values, fahrenheit_values))

答案 1 :(得分:0)

您需要将每个“对”结果附加到一个空数组/列表中,这就是我要这样做的方式;

celsius = random.sample(range(-10, 40), 35)

array = numpy.array(celsius)

final_list = []
for n in celsius:
    f = (float(n * 1.8 + 32))
    pairs = [n, f]
    final_list.append(pairs)

print(final_list)

这给了我这样的输出-[[0,32.0],[10,50.0],[11,51.8],...]]

答案 2 :(得分:0)

我相信您想要做的是:

#get our list of random celcius numbers
celsius = random.sample (range(-10, 40), 35)
#create an empty list to use later
list = []
#for each element in the list of celcius numbers
for c in celsius:
   #get a farenheit value
   f = float(c*1.8+32)
   #add a sublist consisting of the celsius and fahrenheit numbers to our list
   list += [[c, f]]
#convert the list to a numpy array
array = numpy.array(list)

答案 3 :(得分:0)

由于未提及,因此简单的列表理解方式:

import numpy as np
import random

celsius = np.array(random.sample(range(-10, 40), 35))

def g(i):
    return float(i * 1.8 + 32)

np.array([(i, g(i)) for i in celsius])
array([[ 27. ,  80.6],
       [ 19. ,  66.2],
       [ 34. ,  93.2],
       [ 39. , 102.2],
       [ 38. , 100.4],
       [  9. ,  48.2],
       [ 25. ,  77. ],
       [ 14. ,  57.2],
       [ 12. ,  53.6],
       [  3. ,  37.4],
       [ -8. ,  17.6],
       [ 16. ,  60.8],
       [ 17. ,  62.6],
       [ 32. ,  89.6],
       [ 35. ,  95. ],
       [  8. ,  46.4],
       [ 33. ,  91.4],
       [ 10. ,  50. ],
       [ 15. ,  59. ],
       [ 18. ,  64.4],
       [ 36. ,  96.8],
       [ 26. ,  78.8],
       [ -6. ,  21.2],
       [ 29. ,  84.2],
       [  5. ,  41. ],
       [ -1. ,  30.2],
       [  6. ,  42.8],
       [ -5. ,  23. ],
       [ 30. ,  86. ],
       [-10. ,  14. ],
       [ -2. ,  28.4],
       [ 31. ,  87.8],
       [ -3. ,  26.6],
       [  7. ,  44.6],
       [  2. ,  35.6]])