S
S
samsungovetch2018-08-19 15:42:18
Python
samsungovetch, 2018-08-19 15:42:18

How to get a matrix from a string by transforming it?

a1 a2 a3 a4 a5 a6
a2 a3 a4 a5 a6 a1
a3 a4 a5 a6 a1 a2
a4 a5 a6 a1 a2 a3
a5 a6 a1 a2 a3 a4
a6 a1 a2 a3 a4 a5

from random import randint
 
def create_list(row):
    a = []
    for i in range(row):
        a.append(randint(0,20))
    return a
 
 
def print_list(listname):
    for elem in listname:
        print('{:3d}'.format(elem), end = ' ')
    print()

def list3matrix(listname):
    matname = []
    matname.append(listname)
 
    for i in range(1,len(listname)):
        temp = []
        for j in range(0,len(listname)-1):
            temp.append(matname[i-1][j+1])
        temp.append(matname[i-1][0])
        matname.append(temp)
    return matname
 
 
def list2matrix(listname):
    matname = []
    matname.append(listname)
 
    for i in range(1,len(listname)):
        temp = []
        for j in range(0,len(listname)-1):
            matname.append(matname[i-1][j+1])
        matname.append(matname[i-1][0])
    return matname
 
 
 
def print_matrix(matname):
    for row in matname:
        print()
        for elem in row:
            print('{:3d}'.format(elem), end = ' ')
    print()
 
 
n = int(input('Введите количество значений в строке : '))
 
list1 = create_list(n)
print_list(list1)
matrix1 = list2matrix(list1)
print_matrix(matrix1)

I did this - I wrote down the transformations to the temp list, and then from it I wrote it down to mathname. Directly in mathname does not work. Please tell me how to do it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2018-08-19
@dimonchik2013

https://www.datacamp.com/community/tutorials/panda...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question