Hướng dẫn print list vertically in python - in danh sách theo chiều dọc trong python

Cải thiện bài viết

Lưu bài viết

In danh sách đã được xử lý nhiều lần. Nhưng đôi khi chúng ta cần một định dạng khác nhau để có được đầu ra của danh sách. Điều này cũng có ứng dụng trong việc chuyển đổi ma trận. Danh sách in theo chiều dọc cũng có ứng dụng trong phát triển web. Hãy thảo luận về những cách nhất định trong đó nhiệm vụ này có thể đạt được.

Phương pháp số 1: Sử dụng phương thức ngây thơ Phương pháp ngây thơ có thể được sử dụng để in danh sách theo chiều dọc vis. Sử dụng các vòng lặp và in từng phần tử chỉ mục của mỗi danh sách liên tiếp sẽ giúp chúng tôi đạt được nhiệm vụ này.
The naive method can be used to print the list vertically vis. using the loops and printing each index element of each list successively will help us achieve this task.

Các

Copied!

headers = [ 'ID', 'Name', 'Country' ] employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] print(f'{headers[0]: <10}{headers[1]: <15}{headers[2]}') # ID Name Country # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(f'{row[0]: <10}{row[1]: <15}{row[2]}')
9

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
0

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
1

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
2

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
3

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
4

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
5

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
6

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
7

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
8

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
0

Copied!

employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(*row)
0

Copied!

employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(*row)
1

Copied!

employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(*row)
2

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
5

Copied!

employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(*row)
4

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
7

Copied!

employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(*row)
6

Copied!

employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(*row)
7

Copied!

headers = [ 'ID', 'Name', 'Country' ] employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] print(f'{headers[0]: <10}{headers[1]: <15}{headers[2]}') # ID Name Country # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(f'{row[0]: <10}{row[1]: <15}{row[2]}')
9

Copied!

employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(*row)
9=

Copied!

headers = ['ID', 'Name', 'Country'] for item in headers: # ID # Name # Country print(item)
1

Copied!

headers = ['ID', 'Name', 'Country'] for item in headers: # ID # Name # Country print(item)
2

Copied!

employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(*row)
2

Copied!

headers = [ 'ID', 'Name', 'Country' ] employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] print(f'{headers[0]: <10}{headers[1]: <15}{headers[2]}') # ID Name Country # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(f'{row[0]: <10}{row[1]: <15}{row[2]}')
9

Copied!

headers = ['ID', 'Name', 'Country'] for item in headers: # ID # Name # Country print(item)
5

Đầu ra:

The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
1 4 8 
4 6 3 
5 8 10 

Phương pháp số 2: Sử dụng chức năng zip

Copied!

headers = ['ID', 'Name', 'Country'] for item in headers: # ID # Name # Country print(item)
6Using, chúng tôi ánh xạ các phần tử theo chỉ mục tương ứng với một chỉ số tương ứng và sau đó in mỗi chúng. Điều này thực hiện nhiệm vụ in dọc.
Using zip function, we map the elements at respective index to one other and after that print each of them. This performs the task of vertical printing.

Các

Copied!

headers = [ 'ID', 'Name', 'Country' ] employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] print(f'{headers[0]: <10}{headers[1]: <15}{headers[2]}') # ID Name Country # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(f'{row[0]: <10}{row[1]: <15}{row[2]}')
9

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
0

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
1

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
2

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
3

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
4

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
5

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
6

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
7

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
8

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
0

Copied!

employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(*row)
0

Copied!

employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(*row)
1

Copied!

employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(*row)
2

Copied!

headers = [ 'ID', 'Name', 'Country' ] employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] print(f'{headers[0]: <10}{headers[1]: <15}{headers[2]}') # ID Name Country # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(f'{row[0]: <10}{row[1]: <15}{row[2]}')
9=3

Đầu ra:

The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
1 4 8
4 6 3
5 8 10

In một danh sách theo chiều dọc trong Python #

Để in danh sách theo chiều dọc:

  1. Sử dụng vòng lặp

    Copied!

    my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
    5 để lặp qua danh sách.
  2. Sử dụng một chuỗi được định dạng theo nghĩa đen để định dạng mỗi hàng.
  3. Sử dụng chức năng =5 để in kết quả.

Copied!

headers = [ 'ID', 'Name', 'Country' ] employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] print(f'{headers[0]: <10}{headers[1]: <15}{headers[2]}') # ID Name Country # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(f'{row[0]: <10}{row[1]: <15}{row[2]}')

Mẫu mã in một danh sách các danh sách theo chiều dọc. Nếu bạn cần in danh sách một chiều theo chiều dọc, hãy cuộn xuống ví dụ tiếp theo.

Danh sách tiêu đề là tùy chọn và được in ngay trước các mục trong danh sách =6.

Chúng tôi đã sử dụng một chuỗi được định dạng theo nghĩa đen để định dạng mỗi hàng.

Các chuỗi chữ được định dạng cũng cho phép chúng tôi sử dụng ngôn ngữ nhỏ dành riêng cho định dạng trong các khối biểu thức.

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'

Không gian giữa đại tràng và dấu hiệu ít hơn là ký tự lấp đầy.

Các dấu hiệu ít hơn hoặc lớn hơn là sự liên kết.

Dấu ít hơn so với sắp xếp chuỗi bên trái và dấu lớn hơn so với dấu hiệu bên phải.

Nếu bạn không muốn định dạng từng hàng, hãy sử dụng toán tử giải nén có thể lặp lại trong cuộc gọi đến =5.

Copied!

employees = [ [1, 'alice', 'Austria'], [2, 'bobbyhadz', 'Bulgaria'], [3, 'carl', 'Canada'], ] # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for row in employees: print(*row)

Toán tử giải nén * có thể giải quyết được cho phép chúng tôi giải nén các cuộc gọi chức năng có thể lặp lại, trong toàn bộ và trong các biểu thức của máy phát.

Nếu bạn cần in danh sách một chiều theo chiều dọc, với mỗi mục trên một dòng riêng biệt, hãy sử dụng vòng lặp

Copied!

my_str = 'hi' # 👇️ left-aligned result = f'{my_str: <6}' print(repr(result)) # 👉️ 'hi ' # 👇️ right-aligned result = f'{my_str: >6}' print(repr(result)) # 👉️ ' hi'
5 đơn giản.

Copied!

headers = ['ID', 'Name', 'Country'] for item in headers: # ID # Name # Country print(item)

Bạn có thể sử dụng chức năng

Copied!

headers = ['ID', 'Name', 'Country'] for item in headers: # ID # Name # Country print(item)
6 nếu bạn cần in danh sách một chiều theo chiều dọc, với định dạng.

Copied!

headers = ['ID', 'Name', 'Country'] my_list = [1, 'alice', 'Austria', 2, 'bobbyhadz', 'Bulgaria', 3, 'carl', 'Canada'] columns = 3 print(f'{headers[0]: <10}{headers[1]: <15}{headers[2]}') # ID Name Country # 1 alice Austria # 2 bobbyhadz Bulgaria # 3 carl Canada for first, second, third in zip(my_list[::columns], my_list[1::columns], my_list[2::columns]): print(f'{first: <10}{second: <15}{third}')

Tiêu đề in là tùy chọn.

Hàm zip lặp lại trên một số vòng lặp song song và tạo ra các bộ dữ liệu với một mục từ mỗi mục có thể.

Hàm test_list 7 trả về một trình lặp của bộ dữ liệu.

Copied!

headers = ['ID', 'Name', 'Country'] my_list = [1, 'alice', 'Austria', 2, 'bobbyhadz', 'Bulgaria', 3, 'carl', 'Canada'] columns = 3 # [(1, 'alice', 'Austria'), (2, 'bobbyhadz', 'Bulgaria'), (3, 'carl', 'Canada')] print(list(zip(my_list[::columns], my_list[1::columns], my_list[2::columns])))

Chúng tôi đã sử dụng việc cắt danh sách trong cuộc gọi đến hàm

Copied!

headers = ['ID', 'Name', 'Country'] for item in headers: # ID # Name # Country print(item)
6 với mỗi 3 yếu tố trong danh sách, bắt đầu tại INDEX 0, sau đó tại INDEX
The original list is : [[1, 4, 5], [4, 6, 8], [8, 3, 10]]
1 4 8
4 6 3
5 8 10
1, v.v.