繁体   English   中英

使用没有 NUMPY 的随机数创建二维数组(Python)

[英]Creating a 2D array with random numbers WITHOUT NUMPY (Python)

如何在不使用 NumPy (Python) 的情况下创建具有随机数的二维数组

您可以使用random模块并使用列表理解填充嵌套列表

import random

low = 0
high = 10
cols = 10
rows = 5

[random.choices(range(low,high), k=cols) for _ in range(rows)]

[[5, 7, 1, 0, 6, 5, 9, 2, 5, 6],
 [9, 2, 3, 0, 6, 7, 0, 6, 6, 3],
 [2, 7, 9, 2, 4, 5, 5, 9, 9, 4],
 [2, 6, 7, 8, 5, 1, 4, 4, 4, 4],
 [9, 2, 8, 4, 5, 2, 0, 1, 2, 1]]

对于浮动的嵌套列表,您可以 map 每个range使用float

choices = list(map(float, range(low,high)))
[random.choices(choices , k=cols) for _ in range(rows)]

[[0.0, 3.0, 9.0, 1.0, 5.0, 3.0, 7.0, 4.0, 2.0, 4.0],
 [5.0, 8.0, 7.0, 7.0, 7.0, 2.0, 9.0, 8.0, 2.0, 6.0],
 [3.0, 3.0, 1.0, 9.0, 2.0, 8.0, 7.0, 2.0, 9.0, 7.0],
 [7.0, 8.0, 1.0, 2.0, 0.0, 6.0, 7.0, 6.0, 0.0, 9.0],
 [3.0, 3.0, 3.0, 1.0, 7.0, 8.0, 3.0, 9.0, 2.0, 8.0]]
[[random.random() for _ in range(3)] for _ in range(7)]

这会生成一个大小为[7, 3]的二维数组,其中随机浮点数位于[0, 1)区间

您使用嵌套列表推导。 外层构建一个主列表,而内层构建用作主列表元素的列表。


编辑

然后,您可以根据需要对其进行调整。 例如:

import random
import pprint

NUM_ROWS=7
NUM_COLS=3
MAX_VAL=1000.50
MIN_VAL=-MAX_VAL

pprint.pprint([
  [random.uniform(MIN_VAL, MAX_VAL) for _ in NUM_COLS]
  for _ in NUM_ROWS
])

这将打印一个包含 7 行和 3 个列的列表/数组/矩阵,并在 [-1000.50, 1000.50) 间隔内随机浮动:

[[561.3985362160208, -157.9871329592354, -245.7102502320838],
 [-817.8786101352823, -528.9769041860632, 102.67728824479877],
 [-886.6488625065194, 941.0504221837489, -458.58155555154565],
 [6.69525238666165, 919.5903586746183, 66.70453038938808],
 [754.3718741592056, -121.25678519054622, -577.7163532922043],
 [-352.3158889341157, 254.9985130814921, -365.0937338693691],
 [563.0633042715097, 833.2963094260072, -946.6729221921638]]

生成的数组可以使用array[line][column]进行索引。

import pandas as pd
from random import randint

outside_size = 10 # How many nested lists to include
inside_size = 10  # How many numbers will be in an inside list
outside_list = [] # The final list
for i in range(0, outside_size, 1):
    _list = [] # Create new "inside" (nested) list
    for j in range(0, inside_size, 1): # Populate the nested list with random numbers
        _list.append(randint(0, 100))
    outside_list.append(_list) # Add the inside (nested) list to the outside (final) list

df = pd.DataFrame(outside_list) # Create the dataframe from it
print(df)

OUTPUT:

0   1    2   3   4   5   6   7   8   9
0  63  79  100  42  98  45  80  85  71  98
1  65  38   55   5  49  19  99  87  36  74
2  49  76   71  56  54  30  90  50  96  26
3  31  46   79  38  13  66  10  31   8  59
4   0  98    7  67  87   7  95  79  94  50
5  79  44   86  83   1  79  15  80  31  79
6  86   5   19  78  78  87  77   8  43  90
7  30  59    4   4  68  85  95  34  92  48
8  65  39   28  76  12  59  28  29  15  56
9  34  22   68  57  97  69  59  62  12  29

为了代码的速度和简洁性,您可以使用嵌套列表推导来获得相同的效果......

outside_list = [[randint(0, 100) for j in range(0, inside_size)] for i in range(0, outside_size) ]
df = pd.DataFrame(outside_list)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM