如何将随机矩阵变成表格? [英] How can I turn random matrix into a table?

查看:42
本文介绍了如何将随机矩阵变成表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我给的代码.

随机导入def create_random_matrix(rows_min,rows_max, cols_min, cols_max):矩阵 = []# 为行数生成一个随机数# 注意 randint 与类似函数的工作方式不同# 你已经看到 rows_min 和 rows_max 都包含在内# http://docs.python.org/3/library/random.html#random.randint行 = random.randint(rows_min,rows_max)对于范围内的行(行):# 向矩阵添加一行矩阵.附加([])# 为列数生成一个随机数cols = random.randint(cols_min, cols_max)# 为每个生成一个 1 到 100 之间的随机数# 行的单元格对于范围内的 col(cols):矩阵[行].追加(random.randint(1, 100))# 完毕返回矩阵def print_matrix(twod_list):打印(twod_list)如果 __name__ == "__main__":random_matrix = create_random_matrix(8, 12, 3, 7)打印矩阵(随机矩阵)

代码创建一个这样的随机矩阵:

<块引用><块引用><块引用>

[[52, 23, 11, 95, 79], [3, 63, 11], [5, 78, 3, 14, 37], [89, 98, 10], [24, 60, 80, 73, 84, 94], [45, 14, 28], [51, 19, 9], [43, 86, 63, 71, 19], [58, 6, 43, 17, 87, 64, 87], [77, 57, 97], [9, 71, 54, 20], [77, 86, 22]]

但是我怎样才能改变代码来输出这样的东西呢?

36 83 35 73

28 11 3 45 30 44

39 97 3 10 90 5 42

55 73 56 27 7 37

84 49 35 43

100 20 22 95 75 25

58 81 26 34 41 44 72

32 23 21

31 37 1

95 90 26 6 78 49 22

5 17 31

86 25 73 56 10

解决方案

这是一个简单的解决你的问题的方法来打印列表的成员:

mymatrix = [[52, 23, 11, 95, 79], [3, 63, 11], [5, 78, 3, 14, 37], [89, 98, 10], [24, 60, 80, 73, 84, 94], [45, 14, 28], [51, 19, 9], [43, 86, 63, 71, 19], [58, 6, 43, 17,87, 64, 87], [77, 57, 97], [9, 71, 54, 20], [77, 86, 22]]对于 mymatrix 中的列表:对于列表中的项目:打印项目,打印

输出看起来像:

52 23 11 95 793 63 115 78 3 14 3789 98 1024 60 80 73 84 9445 14 2851 19 943 86 63 71 1958 6 43 17 87 64 8777 57 979 71 54 2077 86 22

Here is the code I'm given.

import random

def create_random_matrix(rows_min, rows_max, cols_min, cols_max):
    matrix = []

    # generate a random number for the number of rows
    # notice that randint works differently from similar functions
    # you have seen in that rows_min and rows_max are both inclusive
    # http://docs.python.org/3/library/random.html#random.randint
    rows = random.randint(rows_min, rows_max)

    for row in range(rows):
        # add a row to the matrix
        matrix.append([])

        # generate a random number for the number of columns
        cols = random.randint(cols_min, cols_max)

        # generate a random number between 1 and 100 for each
        # cell of the row
        for col in range(cols):
        matrix[row].append(random.randint(1, 100))

    # done
    return matrix

def print_matrix(twod_list):
    print(twod_list)

if __name__ == "__main__":

    random_matrix = create_random_matrix(8, 12, 3, 7)

    print_matrix(random_matrix)

The code creates a random matrix like this:

[[52, 23, 11, 95, 79], [3, 63, 11], [5, 78, 3, 14, 37], [89, 98, 10], [24, 60, 80, 73, 84, 94], [45, 14, 28], [51, 19, 9], [43, 86, 63, 71, 19], [58, 6, 43, 17, 87, 64, 87], [77, 57, 97], [9, 71, 54, 20], [77, 86, 22]]

But how can I change the code to output something like this instead?

36 83 35 73

28 11 3 45 30 44

39 97 3 10 90 5 42

55 73 56 27 7 37

84 49 35 43

100 20 22 95 75 25

58 81 26 34 41 44 72

32 23 21

31 37 1

95 90 26 6 78 49 22

5 17 31

86 25 73 56 10

解决方案

This is a simple solution to your problem to print the members of a list of lists:

mymatrix = [[52, 23, 11, 95, 79], [3, 63, 11], [5, 78, 3, 14, 37], [89, 98, 10], [24, 60, 80, 73, 84, 94], [45, 14, 28], [51, 19, 9], [43, 86, 63, 71, 19], [58, 6, 43, 17, 87, 64, 87], [77, 57, 97], [9, 71, 54, 20], [77, 86, 22]]
for list in mymatrix:
    for item in list:
        print item,
    print

the output would look like:

52 23 11 95 79
3 63 11
5 78 3 14 37
89 98 10
24 60 80 73 84 94
45 14 28
51 19 9
43 86 63 71 19
58 6 43 17 87 64 87
77 57 97
9 71 54 20
77 86 22

这篇关于如何将随机矩阵变成表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆