Python cung cấp một số cách khác nhau để làm đẹp đầu ra của từ điển hoặc danh sách. Bạn có thể thêm các định dạng khác nhau để làm cho đầu ra tốt hơn và dễ đọc hơn nhiều. Hãy kiểm tra một số ví dụ
Python in đẹp từ điển
Rất thường xuyên thông tin được trích xuất trong từ điển. Để làm cho đầu ra dễ đọc hơn, bạn cần thực hiện một số thủ thuật nhỏ. Một cách là sử dụng các hàm định dạng của chuỗi. Ví dụ dưới đây chứng minh điều đó
d = {1: ["Python", 33.2, 'UP'],
2: ["Java", 23.54, 'DOWN'],
3: ["Ruby", 17.22, 'UP'],
10: ["Lua", 10.55, 'DOWN'],
5: ["Groovy", 9.22, 'DOWN'],
6: ["C", 1.55, 'UP']
}
print ["{:12}" * [len[dota_teams] + 1]
print[format_row.format["", *dota_teams]]
for team, row in zip[dota_teams, data]:
print[format_row.format[team, *row]]
kết quả
Liquid Virtus.pro PSG.LGD Team Secret
Liquid 1 2 1 x
Virtus.pro x 1 1 x
PSG.LGD 1 x 0 1
Team Secret 2 0 2 1
Python in đẹp từ danh sách với lập bảng
Bạn có thể sử dụng các gói python như. lập bảng. Ưu điểm của việc sử dụng các gói nằm trong bảng để in các danh sách và từ điển đẹp mắt là. không cần thực hiện cài đặt và định dạng tùy chỉnh mỗi khi bạn thêm mục mới vào danh sách. Nói cách khác, đây không phải là giải pháp mã hóa cứng
from tabulate import tabulate
data = [[1, 'Liquid', 24, 12],
[2, 'Virtus.pro', 19, 14],
[3, 'PSG.LGD', 15, 19],
[4,'Team Secret', 10, 20]]
print [tabulate[data, headers=["Pos", "Team", "Win", "Lose"]]]
Điều này sẽ dẫn đến
Pos Team Win Lose
----- ----------- ----- ------
1 Liquid 24 12
2 Virtus.pro 19 14
3 PSG.LGD 15 19
4 Team Secret 10 20
Có các giải pháp khác để in danh sách hoặc chính tả đẹp với Python như. PrettyTable, bảng văn bản, bảng đẹp. Bạn có thể kiểm tra xem chúng có phù hợp với nhu cầu của bạn hơn không
Python in đẹp từ danh sách/dict với gấu trúc
Pandas là một giải pháp tốt khác nếu bạn muốn in dữ liệu của mình ở dạng bảng đẹp mắt, cho phép nhiều tùy chỉnh và hỗ trợ nhiều định dạng khác nhau như. csv, dict, json, v.v.
Liquid Virtus.pro
Liquid 1 2
Virtus.pro x 1
9dota_teams = ["Liquid", "Virtus.pro", "PSG.LGD", "Team Secret"]
data = [[1, 2, 1, 'x'],
['x', 1, 1, 'x'],
[1, 'x', 0, 1],
[2, 0, 2, 1]]
format_row = "{:>12}" * [len[dota_teams] + 1]
print[format_row.format["", *dota_teams]]
for team, row in zip[dota_teams, data]:
print[format_row.format[team, *row]]
0 Output: NAME AGE COURSE Samuel 21 Data Structures Richie 20 Machine Learning Lauren 21 OOPS with java2
NAME AGE COURSE Samuel 21 Data Structures Richie 20 Machine Learning Lauren 21 OOPS with Java8
Output: NAME AGE COURSE Samuel 21 Data Structures Richie 20 Machine Learning Lauren 21 OOPS with java4
dota_teams = ["Liquid", "Virtus.pro", "PSG.LGD", "Team Secret"]
data = [[1, 2, 1, 'x'],
['x', 1, 1, 'x'],
[1, 'x', 0, 1],
[2, 0, 2, 1]]
format_row = "{:>12}" * [len[dota_teams] + 1]
print[format_row.format["", *dota_teams]]
for team, row in zip[dota_teams, data]:
print[format_row.format[team, *row]]
12Output: NAME AGE COURSE Samuel 21 Data Structures Richie 20 Machine Learning Lauren 21 OOPS with java6
Output: NAME AGE COURSE Samuel 21 Data Structures Richie 20 Machine Learning Lauren 21 OOPS with java02
Output: NAME AGE COURSE Samuel 21 Data Structures Richie 20 Machine Learning Lauren 21 OOPS with java6
Output: NAME AGE COURSE Samuel 21 Data Structures Richie 20 Machine Learning Lauren 21 OOPS with java22
Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
50 Pandas DataFrame là cấu trúc dữ liệu được gắn nhãn 2 chiều với các cột có các loại khác nhau. Nó thường là đối tượng gấu trúc được sử dụng phổ biến nhất. Pandas DataFrame có thể được tạo theo nhiều cách bằng Python. Hãy thảo luận về cách tạo Khung dữ liệu Pandas từ Danh sách từ điển
Phương pháp 1. Chuyển đổi danh sách từ điển thành DataFrame của gấu trúc bằng from_records
Pandas hàm from records[] của DataFrame. Nó thay đổi dữ liệu có cấu trúc hoặc bản ghi thành DataFrames. Nó chuyển đổi một chuỗi ndarray, tuple hoặc dict có cấu trúc hoặc DataFrame thành một đối tượng DataFrame
Python3
dota_teams = ["Liquid", "Virtus.pro", "PSG.LGD", "Team Secret"]
data = [[1, 2, 1, 'x'],
['x', 1, 1, 'x'],
[1, 'x', 0, 1],
[2, 0, 2, 1]]
format_row = "{:>12}" * [len[dota_teams] + 1]
print[format_row.format["", *dota_teams]]
for team, row in zip[dota_teams, data]:
print[format_row.format[team, *row]]
2 dota_teams = ["Liquid", "Virtus.pro", "PSG.LGD", "Team Secret"]
data = [[1, 2, 1, 'x'],
['x', 1, 1, 'x'],
[1, 'x', 0, 1],
[2, 0, 2, 1]]
format_row = "{:>12}" * [len[dota_teams] + 1]
print[format_row.format["", *dota_teams]]
for team, row in zip[dota_teams, data]:
print[format_row.format[team, *row]]
3dota_teams = ["Liquid", "Virtus.pro", "PSG.LGD", "Team Secret"]
data = [[1, 2, 1, 'x'],
['x', 1, 1, 'x'],
[1, 'x', 0, 1],
[2, 0, 2, 1]]
format_row = "{:>12}" * [len[dota_teams] + 1]
print[format_row.format["", *dota_teams]]
for team, row in zip[dota_teams, data]:
print[format_row.format[team, *row]]
4dota_teams = ["Liquid", "Virtus.pro", "PSG.LGD", "Team Secret"]
data = [[1, 2, 1, 'x'],
['x', 1, 1, 'x'],
[1, 'x', 0, 1],
[2, 0, 2, 1]]
format_row = "{:>12}" * [len[dota_teams] + 1]
print[format_row.format["", *dota_teams]]
for team, row in zip[dota_teams, data]:
print[format_row.format[team, *row]]
5dota_teams = ["Liquid", "Virtus.pro", "PSG.LGD", "Team Secret"]
data = [[1, 2, 1, 'x'],
['x', 1, 1, 'x'],
[1, 'x', 0, 1],
[2, 0, 2, 1]]
format_row = "{:>12}" * [len[dota_teams] + 1]
print[format_row.format["", *dota_teams]]
for team, row in zip[dota_teams, data]:
print[format_row.format[team, *row]]
6dota_teams = ["Liquid", "Virtus.pro", "PSG.LGD", "Team Secret"]
data = [[1, 2, 1, 'x'],
['x', 1, 1, 'x'],
[1, 'x', 0, 1],
[2, 0, 2, 1]]
format_row = "{:>12}" * [len[dota_teams] + 1]
print[format_row.format["", *dota_teams]]
for team, row in zip[dota_teams, data]:
print[format_row.format[team, *row]]
0 dota_teams = ["Liquid", "Virtus.pro", "PSG.LGD", "Team Secret"]
data = [[1, 2, 1, 'x'],
['x', 1, 1, 'x'],
[1, 'x', 0, 1],
[2, 0, 2, 1]]
format_row = "{:>12}" * [len[dota_teams] + 1]
print[format_row.format["", *dota_teams]]
for team, row in zip[dota_teams, data]:
print[format_row.format[team, *row]]
8Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
60Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
61Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
62Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
63Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
64Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
61Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
66Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
63Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
68Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
61Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
60Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
61Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
62Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
63______160Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
61Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
66Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
63______164Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
61dota_teams = ["Liquid", "Virtus.pro", "PSG.LGD", "Team Secret"]
data = [[1, 2, 1, 'x'],
['x', 1, 1, 'x'],
[1, 'x', 0, 1],
[2, 0, 2, 1]]
format_row = "{:>12}" * [len[dota_teams] + 1]
print[format_row.format["", *dota_teams]]
for team, row in zip[dota_teams, data]:
print[format_row.format[team, *row]]
20Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
63Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
68Pos Lang Percent Change
1 Python 33.2 UP
2 Java 23.54 DOWN
3 Ruby 17.22 UP
5 Groovy 9.22 DOWN
6 C 1.55 UP
10 Lua 10.55 DOWN
61dota_teams = ["Liquid", "Virtus.pro", "PSG.LGD", "Team Secret"]
data = [[1, 2, 1, 'x'],
['x', 1, 1, 'x'],
[1, 'x', 0, 1],
[2, 0, 2, 1]]
format_row = "{:>12}" * [len[dota_teams] + 1]
print[format_row.format["", *dota_teams]]
for team, row in zip[dota_teams, data]:
print[format_row.format[team, *row]]
24dota_teams = ["Liquid", "Virtus.pro", "PSG.LGD", "Team Secret"]
data = [[1, 2, 1, 'x'],
['x', 1, 1, 'x'],
[1, 'x', 0, 1],
[2, 0, 2, 1]]
format_row = "{:>12}" * [len[dota_teams] + 1]
print[format_row.format["", *dota_teams]]
for team, row in zip[dota_teams, data]:
print[format_row.format[team, *row]]
25