Hướng dẫn how to print matrix in python - làm thế nào để in ma trận trong python

Ma trận không là gì ngoài sự sắp xếp hình chữ nhật của dữ liệu hoặc số. Nói cách khác, nó là một mảng dữ liệu hoặc số hình chữ nhật. Các mục ngang trong một ma trận được gọi là ‘hàng, trong khi các mục dọc được gọi là‘ cột. Nếu một ma trận có số r số hàng và số C của các cột thì thứ tự của ma trận được đưa ra bởi r x c. Mỗi mục trong một ma trận có thể là các giá trị số nguyên hoặc giá trị nổi, hoặc thậm chí nó có thể là các số phức tạp.r x c. Each entries in a matrix can be integer values, or floating values, or even it can be complex numbers.

Examples:

// 3 x 4 matrix
     1 2 3 4
M =  4 5 6 7
     6 7 8 9

// 2 x 3 matrix in Python
A = [ [ 2, 5, 7 ],
      [ 4, 7, 9 ] ]

// 3 x 4 matrix in Python where entries are floating numbers
B = [ [ 1.0, 3.5, 5.4, 7.9 ],
      [ 9.0, 2.5, 4.2, 3.6 ],
      [ 1.5, 3.2, 1.6, 6.5 ] ]

Trong Python, chúng ta có thể lấy ma trận đầu vào của người dùng theo những cách khác nhau. Một số phương pháp cho ma trận đầu vào của người dùng trong Python được hiển thị bên dưới:

Mã số 1:

Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
1
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
2
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
3
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
4
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
5
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
4__17171718

Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
9
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
2
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
3
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
4
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
5
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
4
Enter the number of rows:2
Enter the number of columns:2
Enter the entries in a single line separated by space: 1 2 3 1 
[[1 2]
 [3 1]]
5
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
8

Enter the number of rows:2
Enter the number of columns:2
Enter the entries in a single line separated by space: 1 2 3 1 
[[1 2]
 [3 1]]
7
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
2
Enter the number of rows:2
Enter the number of columns:2
Enter the entries in a single line separated by space: 1 2 3 1 
[[1 2]
 [3 1]]
9

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
0
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
4
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
2
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
3

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
4
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
5
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
6
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
7
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
8

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
9
[[1 2 3]
 [3 4 5]
 [7 8 9]]
0
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
229

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
9
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
4
[[1 2 3]
 [3 4 5]
 [7 8 9]]
5
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
6
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
7
[[1 2 3]
 [3 4 5]
 [7 8 9]]
8

[[1 2 3]
 [3 4 5]
 [7 8 9]]
9
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

for line in a:
    print ['  '.join[map[str, line]]]
0
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
3
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
4
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
5
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

for line in a:
    print ['  '.join[map[str, line]]]
4

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
9
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

for line in a:
    print ['  '.join[map[str, line]]]
6

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
4
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
5
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
6
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
7
1  2  3
3  4  5
7  8  9
1

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
9
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
4
[[1 2 3]
 [3 4 5]
 [7 8 9]]
5
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
6
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
7
1  2  3
3  4  5
7  8  9
7

1  2  3
3  4  5
7  8  9
8
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
0 ____70
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
2
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print['\n'.join[[''.join[['{:4}'.format[item] for item in row]] 
      for row in a]]]
2
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
3

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
9
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
0
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print['\n'.join[[''.join[['{:4}'.format[item] for item in row]] 
      for row in a]]]
6

Output:

Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 

Lót:

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print['\n'.join[[''.join[['{:4}'.format[item] for item in row]] 
      for row in a]]]
7
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
2
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print['\n'.join[[''.join[['{:4}'.format[item] for item in row]] 
      for row in a]]]
9
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
3__

& nbsp; Mã số 2: Sử dụng hàm

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

s = [[str[e] for e in row] for row in a]
lens = [max[map[len, col]] for col in zip[*s]]
fmt = '\t'.join['{{:{}}}'.format[x] for x in lens]
table = [fmt.format[*row] for row in s]
print ['\n'.join[table]]
4 và
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

s = [[str[e] for e in row] for row in a]
lens = [max[map[len, col]] for col in zip[*s]]
fmt = '\t'.join['{{:{}}}'.format[x] for x in lens]
table = [fmt.format[*row] for row in s]
print ['\n'.join[table]]
5.
Code #2: Using
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

s = [[str[e] for e in row] for row in a]
lens = [max[map[len, col]] for col in zip[*s]]
fmt = '\t'.join['{{:{}}}'.format[x] for x in lens]
table = [fmt.format[*row] for row in s]
print ['\n'.join[table]]
4 function and
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

s = [[str[e] for e in row] for row in a]
lens = [max[map[len, col]] for col in zip[*s]]
fmt = '\t'.join['{{:{}}}'.format[x] for x in lens]
table = [fmt.format[*row] for row in s]
print ['\n'.join[table]]
5.

Trong Python, tồn tại một thư viện phổ biến tên là Numpy. Thư viện này là một thư viện cơ bản cho bất kỳ tính toán khoa học nào. Nó cũng được sử dụng cho các mảng đa chiều và như chúng ta biết ma trận là một mảng hình chữ nhật, chúng ta sẽ sử dụng thư viện này cho ma trận đầu vào của người dùng.NumPy. This library is a fundamental library for any scientific computation. It is also used for multidimensional arrays and as we know matrix is a rectangular array, we will use this library for user input matrix.

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

s = [[str[e] for e in row] for row in a]
lens = [max[map[len, col]] for col in zip[*s]]
fmt = '\t'.join['{{:{}}}'.format[x] for x in lens]
table = [fmt.format[*row] for row in s]
print ['\n'.join[table]]
6
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

s = [[str[e] for e in row] for row in a]
lens = [max[map[len, col]] for col in zip[*s]]
fmt = '\t'.join['{{:{}}}'.format[x] for x in lens]
table = [fmt.format[*row] for row in s]
print ['\n'.join[table]]
7

Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
1
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
2
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
3
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
4
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
5
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
4__17171718

Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
9
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
2
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
3
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
4
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
5
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
4
Enter the number of rows:2
Enter the number of columns:2
Enter the entries in a single line separated by space: 1 2 3 1 
[[1 2]
 [3 1]]
5
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
8

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
0
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
4
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
16
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
3

Enter the number of rows:2
Enter the number of columns:2
Enter the entries in a single line separated by space: 1 2 3 1 
[[1 2]
 [3 1]]
7
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
2
Enter the number of rows:2
Enter the number of columns:2
Enter the entries in a single line separated by space: 1 2 3 1 
[[1 2]
 [3 1]]
9

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
4
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
5
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
6
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
7
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
8

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
0
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
32

Output:

Enter the number of rows:2
Enter the number of columns:2
Enter the entries in a single line separated by space: 1 2 3 1 
[[1 2]
 [3 1]]

  1. Làm thế nào để
  2. Python làm thế nào
  3. In ma trận trong Python

Tạo: Tháng 3-20, 2021 | Cập nhật: Tháng 5-13, 2021

  1. Sử dụng vòng
    import numpy as np
    
    a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]
    
    print[a]
    
    4 để in ma trận trong Python
  2. Sử dụng phương pháp hiểu danh sách để in ma trận trong Python

Danh sách hiểu biết cung cấp một cách ngắn gọn và thanh lịch để làm việc với các danh sách trong một dòng mã duy nhất.

Phương pháp này cũng sử dụng vòng lặp

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
4 nhưng được coi là nhanh hơn một chút so với việc sử dụng nó theo truyền thống, như trong phương pháp trước đó.

Lấy mã sau làm ví dụ.

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]

Output:

[[1 2 3]
 [3 4 5]
 [7 8 9]]

Mặc dù mã trên sử dụng gần như các chức năng giống như trong phương thức trước, nhưng nó thực hiện hiệu quả hơn và trong một dòng. Sự khác biệt chính khác là hàm

Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
39 được sử dụng ở đây để cung cấp khoảng cách cần thiết giữa các phần tử chuỗi.

Sử dụng vòng
import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
4 để in ma trận trong Python

Phương pháp này sẽ lặp lại thông qua ma trận bằng cách sử dụng vòng lặp

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
4 và in từng hàng riêng lẻ sau khi định dạng đúng. Mã sau đây cho thấy cách.

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

for line in a:
    print ['  '.join[map[str, line]]]

Output:

1  2  3
3  4  5
7  8  9

Chúng tôi sử dụng hàm

Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
22, chuyển đổi toàn bộ hàng thành một chuỗi và sau đó chúng tôi áp dụng chức năng
Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
37 cho toàn bộ hàng này chuyển đổi tất cả thành một chuỗi và các phần tử riêng biệt theo trình phân tách được chỉ định.

Sử dụng phương pháp hiểu danh sách để in ma trận trong Python

Danh sách hiểu biết cung cấp một cách ngắn gọn và thanh lịch để làm việc với các danh sách trong một dòng mã duy nhất.

Phương pháp này cũng sử dụng vòng lặp

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print[a]
4 nhưng được coi là nhanh hơn một chút so với việc sử dụng nó theo truyền thống, như trong phương pháp trước đó.

Lấy mã sau làm ví dụ.

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

print['\n'.join[[''.join[['{:4}'.format[item] for item in row]] 
      for row in a]]]

Output:

   1   2   3
   3   4   5
   7   8   9

Mặc dù mã trên sử dụng gần như các chức năng giống như trong phương thức trước, nhưng nó thực hiện hiệu quả hơn và trong một dòng. Sự khác biệt chính khác là hàm

Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
39 được sử dụng ở đây để cung cấp khoảng cách cần thiết giữa các phần tử chuỗi.

Một cách khác để sử dụng phương pháp này được hiển thị dưới đây. Ở đây chúng tôi chia mã riêng thành các dòng riêng lẻ và cố gắng làm cho ma trận giống như cấu trúc giống như bảng.

import numpy as np

a = np.array[[[1,2,3],[3,4,5],[7,8,9]]]

s = [[str[e] for e in row] for row in a]
lens = [max[map[len, col]] for col in zip[*s]]
fmt = '\t'.join['{{:{}}}'.format[x] for x in lens]
table = [fmt.format[*row] for row in s]
print ['\n'.join[table]]

Output:

Enter the number of rows:2
Enter the number of columns:3
Enter the entries rowwise:
1
2
3
4
5
6

1 2 3 
4 5 6 
0

Bài viết liên quan - Ma trận Python

  • Ma trận kề trong Python
  • Tạo ma trận nhận dạng với Python
  • Ma trận đường chéo trong python
  • Ma trận thưa thớt trong Python
  • Làm thế nào để bạn in một ma trận 2D trong Python?

    Đầu ra.Để in ra toàn bộ mảng hai chiều, chúng ta có thể sử dụng Python cho vòng lặp như hình dưới đây.Chúng tôi sử dụng cuối dòng để in ra các giá trị trong các hàng khác nhau.use python for loop as shown below. We use end of line to print out the values in different rows.

    Làm thế nào để bạn tạo một ma trận trong Python?

    Python không có cách đơn giản để thực hiện kiểu dữ liệu ma trận.Ma trận Python có thể được tạo bằng cách sử dụng kiểu dữ liệu danh sách lồng nhau và bằng cách sử dụng thư viện Numpy.Thư viện Python Numpy giúp đối phó với các mảng.Numpy xử lý một mảng nhanh hơn một chút so với danh sách.

    Làm thế nào để bạn in một ma trận trong một vòng lặp trong Python?

    In ra một ma trận 3x3 của--S S sử dụng cho các vòng lặp.x = "-" Đối với I trong phạm vi [3]: Đối với n trong phạm vi [3]: Đối với x trong phạm vi [3]: in x, bạn sẽ cần được lồng cho các vòng để thực hiện điều này.

    Làm thế nào để bạn tìm thấy ma trận 3 3 trong Python?

    Bạn có thể sử dụng Numpy.Đầu tiên, chuyển đổi danh sách của bạn thành mảng numpy.Sau đó, lấy một phần tử và định hình lại nó thành ma trận 3x3.use numpy. First, convert your list into numpy array. Then, take an element and reshape it to 3x3 matrix.

    Bài Viết Liên Quan

    Chủ Đề