Найти - Пользователи
Полная версия: Создание матрицы, 5 строк.
Начало » Центр помощи » Создание матрицы, 5 строк.
1 2
gisolog
небольшое дополнение, раз речь была о матрицах:
>>> import numpy as np
>>> m = np.ones([5,5])
>>> m
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])
>>> m2 = np.zeros([5,5])
>>> m2
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])
там же np.empty(), np.identity() и т.д. и т.п. NUMPY:Array creation routines
ну и собсно:
>>> np.random.randint(0,10,25).reshape(5,-1)
array([[9, 9, 5, 8, 0],
       [0, 5, 6, 9, 8],
       [2, 4, 9, 6, 3],
       [8, 4, 1, 5, 5],
       [1, 6, 9, 4, 7]])
>>> np.random.randint(0,10,25).reshape(5,-1).tolist()
[[3, 1, 6, 6, 1], [4, 0, 5, 4, 8], [0, 2, 4, 4, 0], [4, 6, 6, 1, 1], [0, 8, 8, 1, 0]]
FishHook
fata1ex
FishHook, у тебя ошибка где-то - цифры другие.

Жжошь!
Даже не сразу дошло
alekzp
В учебнике Саммерфилда есть универсальный пример создания матрицы из любого количество строк и столбцов.

import random
def get_int(msg, minimum, default):
    while True:
        try:
            line = input(msg)
            if not line and default is not None:
                return default
            i = int(line)
            if i < minimum:
                print("must be >=", minimum)
            else:
                return i
        except ValueError as err:
            print(err)
rows = get_int("rows: ", 1, None)
columns = get_int("columns: ", 1, None)
minimum = get_int("minimum (or Enter for 0): ", -1000000, 0)
default = 1000
if default < minimum:
    default = 2 * minimum
maximum = get_int("maximum (or Enter for " + str(default) + "): ",
                  minimum, default)
row = 0
while row < rows:
    line = ""
    column = 0
    while column < columns:
        i = random.randint(minimum, maximum)
        s = str(i)
        while len(s) < 10:
            s = " " + s
        line += s
        column += 1
    print(line)
    row += 1
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB