Mã Python sau đọc tệp bằng hàm open[]. Mỗi dòng dưới dạng chuỗi được phân tách tại ký tự khoảng trắng. Thành phần đầu tiên được sử dụng làm khóa và thứ hai làm giá trị
d = {} with open["dict.txt"] as f: for line in f: [key, val] = line.split[] d[int[key]] = val print [d]
Đầu ra hiển thị nội dung của tệp ở dạng từ điển
{1: 'aaa', 2: 'bbb', 3: 'ccc'}
Cho đến nay tôi đã học được rất nhiều với Python, nhưng khi tôi học từ điển [đôi khi được rút ngắn thành dicts], tôi thực sự hào hứng với những gì có thể làm được. Từ điển trong Python là một chuỗi các khóa và giá trị được lưu trữ bên trong một đối tượng. Đây là loại giống như một siêu mảng; . Tạo từ điển từ mảng thực sự cũng có thể rất đơn giản
Trong blog này, tôi sẽ tìm hiểu cách tạo từ điển và cách đọc và ghi tệp trong mã
từ điển
Từ điển trong Python được biểu thị bằng cách sử dụng dấu ngoặc nhọn hoặc như tôi muốn gọi chúng là ria mép. { }
chỉ ra rằng danh sách bạn đang xem hoàn toàn không phải là danh sách mà là từ điển
shows_and _characters = {
"Bojack Horseman": "Todd",
"My Hero Academia": "Midoriya"
"Ozark": "Ruth"
"Arrested Development": "Tobias",
"Derry Girls": "Sister Michael",
"Tuca & Bertie": "Bertie"
}
Đây là từ điển các chương trình truyền hình yêu thích của tôi và các nhân vật yêu thích của tôi trong chương trình đó. Trong ví dụ này, khóa ở bên trái và giá trị ở bên phải. Để truy cập từ điển, bạn sử dụng một lệnh gọi tương tự như đối với một danh sách, ngoại trừ thay vì nhập số phần tử, bạn sẽ đặt khóa.
shows_with_lists = {
"Bojack Horseman": ["Todd", "Princess Carolyn", "Judah", "Diane"],
"My Hero Academia": ["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
"Ozark": ["Ruth", "Jonah", "Wyatt"],
"Arrested Development": ["Tobias", "Gob", "Anne", "Maeby"],
"Derry Girls": ["Sister Michael", "Orla", "Erin", "Claire", "James"],
"Tuca & Bertie": ["Bertie", "Speckle", "Tuca", "Dakota"]
}
0 sẽ in shows_with_lists = {
"Bojack Horseman": ["Todd", "Princess Carolyn", "Judah", "Diane"],
"My Hero Academia": ["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
"Ozark": ["Ruth", "Jonah", "Wyatt"],
"Arrested Development": ["Tobias", "Gob", "Anne", "Maeby"],
"Derry Girls": ["Sister Michael", "Orla", "Erin", "Claire", "James"],
"Tuca & Bertie": ["Bertie", "Speckle", "Tuca", "Dakota"]
}
1 ra bàn điều khiển. Ngoài ra, cả khóa và giá trị trong ví dụ này đều là chuỗi, nhưng đó không phải là yêu cầu. Các khóa có thể là bất kỳ loại bất biến nào, như chuỗi, int, float và bộ dữ liệu. Các giá trị không có cùng hạn chế này, do đó, các giá trị có thể là một từ điển lồng nhau hoặc một danh sách, ngoài các loại được đề cập cho các khóa. Chẳng hạn, từ điển sau đây là một từ điển hợp lệshows_with_lists = {
"Bojack Horseman": ["Todd", "Princess Carolyn", "Judah", "Diane"],
"My Hero Academia": ["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
"Ozark": ["Ruth", "Jonah", "Wyatt"],
"Arrested Development": ["Tobias", "Gob", "Anne", "Maeby"],
"Derry Girls": ["Sister Michael", "Orla", "Erin", "Claire", "James"],
"Tuca & Bertie": ["Bertie", "Speckle", "Tuca", "Dakota"]
}
Trong ví dụ này, mỗi giá trị là một danh sách. Vì vậy, nếu chúng tôi cố gắng in giá trị cho khóa
shows_with_lists = {
"Bojack Horseman": ["Todd", "Princess Carolyn", "Judah", "Diane"],
"My Hero Academia": ["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
"Ozark": ["Ruth", "Jonah", "Wyatt"],
"Arrested Development": ["Tobias", "Gob", "Anne", "Maeby"],
"Derry Girls": ["Sister Michael", "Orla", "Erin", "Claire", "James"],
"Tuca & Bertie": ["Bertie", "Speckle", "Tuca", "Dakota"]
}
2, chúng tôi sẽ thấy shows_with_lists = {
"Bojack Horseman": ["Todd", "Princess Carolyn", "Judah", "Diane"],
"My Hero Academia": ["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
"Ozark": ["Ruth", "Jonah", "Wyatt"],
"Arrested Development": ["Tobias", "Gob", "Anne", "Maeby"],
"Derry Girls": ["Sister Michael", "Orla", "Erin", "Claire", "James"],
"Tuca & Bertie": ["Bertie", "Speckle", "Tuca", "Dakota"]
}
3 được in ra bảng điều khiển. Tuy nhiên, nếu chúng tôi muốn phần tử cuối cùng trong danh sách giá trị, chúng tôi sẽ viết shows_with_lists = {
"Bojack Horseman": ["Todd", "Princess Carolyn", "Judah", "Diane"],
"My Hero Academia": ["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
"Ozark": ["Ruth", "Jonah", "Wyatt"],
"Arrested Development": ["Tobias", "Gob", "Anne", "Maeby"],
"Derry Girls": ["Sister Michael", "Orla", "Erin", "Claire", "James"],
"Tuca & Bertie": ["Bertie", "Speckle", "Tuca", "Dakota"]
}
4. Điều này sẽ in phần tử cuối cùng trong danh sách, trong trường hợp này là shows_with_lists = {
"Bojack Horseman": ["Todd", "Princess Carolyn", "Judah", "Diane"],
"My Hero Academia": ["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
"Ozark": ["Ruth", "Jonah", "Wyatt"],
"Arrested Development": ["Tobias", "Gob", "Anne", "Maeby"],
"Derry Girls": ["Sister Michael", "Orla", "Erin", "Claire", "James"],
"Tuca & Bertie": ["Bertie", "Speckle", "Tuca", "Dakota"]
}
5Từ điển có thể được viết thủ công hoặc nếu bạn có hai danh sách, bạn có thể kết hợp các phương pháp
shows_with_lists = {
"Bojack Horseman": ["Todd", "Princess Carolyn", "Judah", "Diane"],
"My Hero Academia": ["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
"Ozark": ["Ruth", "Jonah", "Wyatt"],
"Arrested Development": ["Tobias", "Gob", "Anne", "Maeby"],
"Derry Girls": ["Sister Michael", "Orla", "Erin", "Claire", "James"],
"Tuca & Bertie": ["Bertie", "Speckle", "Tuca", "Dakota"]
}
6 và shows_with_lists = {
"Bojack Horseman": ["Todd", "Princess Carolyn", "Judah", "Diane"],
"My Hero Academia": ["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
"Ozark": ["Ruth", "Jonah", "Wyatt"],
"Arrested Development": ["Tobias", "Gob", "Anne", "Maeby"],
"Derry Girls": ["Sister Michael", "Orla", "Erin", "Claire", "James"],
"Tuca & Bertie": ["Bertie", "Speckle", "Tuca", "Dakota"]
}
7 để tạo danh sách thành từ điểnlist_of_shows = ["Bojack Horseman",
"My Hero Academia",
"Ozark",
"Arrested Development",
"Derry Girls",
"Tuca & Bertie"]
list_of_characters = [["Todd", "Princess Carolyn", "Judah", "Diane"],
["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
["Ruth", "Jonah", "Wyatt"],
["Tobias", "Gob", "Anne", "Maeby"],
["Sister Michael", "Orla", "Erin", "Claire", "James"],
["Bertie", "Speckle", "Tuca", "Dakota"]]
combined_shows_characters = dict[zip[list_of_shows, list_of_characters]]
print[combined_shows_characters]
Đây là một cách để tạo từ điển. Một cái khác được gọi là Hiểu từ điển. Cái này tốn nhiều công sức hơn một chút, nhưng có thể được sử dụng theo nhiều cách khác nhau, bao gồm sử dụng một chút logic trên một danh sách để tạo từ điển bằng danh sách gốc đó. Đây là cách với hai ví dụ. một dựa trên các danh sách trên và một với một danh sách duy nhất và một số logic
import math
#This is doing the same work as the above example, but using Dict Comprehension instead.
comprehension_shows_characters = { shows:characters for shows, characters in zip[list_of_shows, list_of_characters] }
hip_to_be_square = [4, 9, 16, 25, 36, 49]
no_longer_hip_to_be_square = { key:math.sqrt[key] for key in hip_to_be_square }
print[no_longer_hip_to_be_square]
Trong từ điển
shows_with_lists = {
"Bojack Horseman": ["Todd", "Princess Carolyn", "Judah", "Diane"],
"My Hero Academia": ["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
"Ozark": ["Ruth", "Jonah", "Wyatt"],
"Arrested Development": ["Tobias", "Gob", "Anne", "Maeby"],
"Derry Girls": ["Sister Michael", "Orla", "Erin", "Claire", "James"],
"Tuca & Bertie": ["Bertie", "Speckle", "Tuca", "Dakota"]
}
8, khóa được tìm thấy trong danh sách shows_with_lists = {
"Bojack Horseman": ["Todd", "Princess Carolyn", "Judah", "Diane"],
"My Hero Academia": ["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
"Ozark": ["Ruth", "Jonah", "Wyatt"],
"Arrested Development": ["Tobias", "Gob", "Anne", "Maeby"],
"Derry Girls": ["Sister Michael", "Orla", "Erin", "Claire", "James"],
"Tuca & Bertie": ["Bertie", "Speckle", "Tuca", "Dakota"]
}
9. Giá trị cho mỗi khóa là căn bậc hai của chính nó, được đưa vào cùng với hàm nhập toán học. Có nhiều phương pháp hữu ích hơn để xử lý từ điển tại đâyĐọc và ghi tệp
Đây là một phần khá thú vị của Python. đọc và ghi các tệp khác ngay trong mã. Với Python, bạn có thể lấy nội dung của một số loại tệp nhất định và sử dụng nó trong mã của mình hoặc thậm chí tạo một tệp mới dựa trên một số thông tin đầu vào. Điều này hữu ích cho việc xử lý dữ liệu và có thể được sử dụng với nhiều loại tệp khác nhau. Hai cái tôi sẽ trình bày ở đây là. csv và. txt
Đọc từ một tập tin
Hãy tưởng tượng một. txt tên là
list_of_shows = ["Bojack Horseman",
"My Hero Academia",
"Ozark",
"Arrested Development",
"Derry Girls",
"Tuca & Bertie"]
list_of_characters = [["Todd", "Princess Carolyn", "Judah", "Diane"],
["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
["Ruth", "Jonah", "Wyatt"],
["Tobias", "Gob", "Anne", "Maeby"],
["Sister Michael", "Orla", "Erin", "Claire", "James"],
["Bertie", "Speckle", "Tuca", "Dakota"]]
combined_shows_characters = dict[zip[list_of_shows, list_of_characters]]
print[combined_shows_characters]
0 chứa dòng list_of_shows = ["Bojack Horseman",
"My Hero Academia",
"Ozark",
"Arrested Development",
"Derry Girls",
"Tuca & Bertie"]
list_of_characters = [["Todd", "Princess Carolyn", "Judah", "Diane"],
["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
["Ruth", "Jonah", "Wyatt"],
["Tobias", "Gob", "Anne", "Maeby"],
["Sister Michael", "Orla", "Erin", "Claire", "James"],
["Bertie", "Speckle", "Tuca", "Dakota"]]
combined_shows_characters = dict[zip[list_of_shows, list_of_characters]]
print[combined_shows_characters]
1. Chúng ta có thể sử dụng Python để lấy dòng đó và biến nó thành một biến. Chạy đoạn mã sau sẽ in nội dung của. txt vào thiết bị đầu cuốiwith open["best-ever.txt"] as text_file:
text_data = text_file.read[]
#This will print the contents of the .txt file.
print[text_data]
Bằng cách sử dụng
list_of_shows = ["Bojack Horseman",
"My Hero Academia",
"Ozark",
"Arrested Development",
"Derry Girls",
"Tuca & Bertie"]
list_of_characters = [["Todd", "Princess Carolyn", "Judah", "Diane"],
["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
["Ruth", "Jonah", "Wyatt"],
["Tobias", "Gob", "Anne", "Maeby"],
["Sister Michael", "Orla", "Erin", "Claire", "James"],
["Bertie", "Speckle", "Tuca", "Dakota"]]
combined_shows_characters = dict[zip[list_of_shows, list_of_characters]]
print[combined_shows_characters]
2, chúng tôi có thể kiểm tra nội dung của các tệp dưới dạng một chuỗi. Nếu tài liệu có nhiều dòng, bạn thậm chí có thể tách chúng ra bằng cách lặp qua chúng bằng cách sử dụng vòng lặp for và phương thức list_of_shows = ["Bojack Horseman",
"My Hero Academia",
"Ozark",
"Arrested Development",
"Derry Girls",
"Tuca & Bertie"]
list_of_characters = [["Todd", "Princess Carolyn", "Judah", "Diane"],
["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
["Ruth", "Jonah", "Wyatt"],
["Tobias", "Gob", "Anne", "Maeby"],
["Sister Michael", "Orla", "Erin", "Claire", "James"],
["Bertie", "Speckle", "Tuca", "Dakota"]]
combined_shows_characters = dict[zip[list_of_shows, list_of_characters]]
print[combined_shows_characters]
3. Sử dụng một trí tưởng tượng. txt có tên là list_of_shows = ["Bojack Horseman",
"My Hero Academia",
"Ozark",
"Arrested Development",
"Derry Girls",
"Tuca & Bertie"]
list_of_characters = [["Todd", "Princess Carolyn", "Judah", "Diane"],
["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
["Ruth", "Jonah", "Wyatt"],
["Tobias", "Gob", "Anne", "Maeby"],
["Sister Michael", "Orla", "Erin", "Claire", "James"],
["Bertie", "Speckle", "Tuca", "Dakota"]]
combined_shows_characters = dict[zip[list_of_shows, list_of_characters]]
print[combined_shows_characters]
4, chúng ta có thể sử dụng cách sau để in ra từng dòng riêng lẻwith open["buncha-lines.txt"] as lines_doc:
for line in lines_doc.readlines[]:
print[line]
Viết một tập tin mới
Tạo một tệp mới cũng dễ dàng với Python. Hàm
list_of_shows = ["Bojack Horseman",
"My Hero Academia",
"Ozark",
"Arrested Development",
"Derry Girls",
"Tuca & Bertie"]
list_of_characters = [["Todd", "Princess Carolyn", "Judah", "Diane"],
["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
["Ruth", "Jonah", "Wyatt"],
["Tobias", "Gob", "Anne", "Maeby"],
["Sister Michael", "Orla", "Erin", "Claire", "James"],
["Bertie", "Speckle", "Tuca", "Dakota"]]
combined_shows_characters = dict[zip[list_of_shows, list_of_characters]]
print[combined_shows_characters]
5 có thể nhận thêm đối số để tạo tệp mới. Trên thực tế, có một đối số mặc định đã được thông qua mỗi lần mà chúng tôi không biết. list_of_shows = ["Bojack Horseman",
"My Hero Academia",
"Ozark",
"Arrested Development",
"Derry Girls",
"Tuca & Bertie"]
list_of_characters = [["Todd", "Princess Carolyn", "Judah", "Diane"],
["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
["Ruth", "Jonah", "Wyatt"],
["Tobias", "Gob", "Anne", "Maeby"],
["Sister Michael", "Orla", "Erin", "Claire", "James"],
["Bertie", "Speckle", "Tuca", "Dakota"]]
combined_shows_characters = dict[zip[list_of_shows, list_of_characters]]
print[combined_shows_characters]
6 là đối số mặc định cho list_of_shows = ["Bojack Horseman",
"My Hero Academia",
"Ozark",
"Arrested Development",
"Derry Girls",
"Tuca & Bertie"]
list_of_characters = [["Todd", "Princess Carolyn", "Judah", "Diane"],
["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
["Ruth", "Jonah", "Wyatt"],
["Tobias", "Gob", "Anne", "Maeby"],
["Sister Michael", "Orla", "Erin", "Claire", "James"],
["Bertie", "Speckle", "Tuca", "Dakota"]]
combined_shows_characters = dict[zip[list_of_shows, list_of_characters]]
print[combined_shows_characters]
5 và đặt nó ở chế độ đọc. Để bật chế độ ghi, hãy chuyển vào một list_of_shows = ["Bojack Horseman",
"My Hero Academia",
"Ozark",
"Arrested Development",
"Derry Girls",
"Tuca & Bertie"]
list_of_characters = [["Todd", "Princess Carolyn", "Judah", "Diane"],
["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
["Ruth", "Jonah", "Wyatt"],
["Tobias", "Gob", "Anne", "Maeby"],
["Sister Michael", "Orla", "Erin", "Claire", "James"],
["Bertie", "Speckle", "Tuca", "Dakota"]]
combined_shows_characters = dict[zip[list_of_shows, list_of_characters]]
print[combined_shows_characters]
8 làm đối số thứ hai. Đoạn mã sau sẽ viết một tệp hoàn toàn mới có tên là list_of_shows = ["Bojack Horseman",
"My Hero Academia",
"Ozark",
"Arrested Development",
"Derry Girls",
"Tuca & Bertie"]
list_of_characters = [["Todd", "Princess Carolyn", "Judah", "Diane"],
["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
["Ruth", "Jonah", "Wyatt"],
["Tobias", "Gob", "Anne", "Maeby"],
["Sister Michael", "Orla", "Erin", "Claire", "James"],
["Bertie", "Speckle", "Tuca", "Dakota"]]
combined_shows_characters = dict[zip[list_of_shows, list_of_characters]]
print[combined_shows_characters]
9 với nội dung là import math
#This is doing the same work as the above example, but using Dict Comprehension instead.
comprehension_shows_characters = { shows:characters for shows, characters in zip[list_of_shows, list_of_characters] }
hip_to_be_square = [4, 9, 16, 25, 36, 49]
no_longer_hip_to_be_square = { key:math.sqrt[key] for key in hip_to_be_square }
print[no_longer_hip_to_be_square]
0with open["best_tv_character.txt", "w"] as best_character:
best_character.write["Peggy Olson from Mad Men"]
Làm việc với. tệp csv
Bạn có thể đọc một. csv bằng Python bằng cách sử dụng
import math
#This is doing the same work as the above example, but using Dict Comprehension instead.
comprehension_shows_characters = { shows:characters for shows, characters in zip[list_of_shows, list_of_characters] }
hip_to_be_square = [4, 9, 16, 25, 36, 49]
no_longer_hip_to_be_square = { key:math.sqrt[key] for key in hip_to_be_square }
print[no_longer_hip_to_be_square]
1 ở đầu tệp, sau đó sử dụng một số phương thức tích hợp sẵn trong mã. Tuy nhiên, mặc dù. csv là văn bản thuần túy, xử lý một. csv giống như bạn đối xử. txt có thể dẫn đến kết quả đầu ra khó đọc; . Không có bảng đó, đầu ra có thể hỗn loạn. Một cách giải quyết vấn đề này là sử dụng phương pháp import math
#This is doing the same work as the above example, but using Dict Comprehension instead.
comprehension_shows_characters = { shows:characters for shows, characters in zip[list_of_shows, list_of_characters] }
hip_to_be_square = [4, 9, 16, 25, 36, 49]
no_longer_hip_to_be_square = { key:math.sqrt[key] for key in hip_to_be_square }
print[no_longer_hip_to_be_square]
2. Phương pháp này cho phép bạn ánh xạ thông tin trong mỗi hàng vào từ điển với các tên trường mà bạn có thể tạo. Tên trường mặc định được thu thập từ hàng đầu tiên của. csv nếu không có tên trường nào được cung cấp. Hãy tưởng tượng một. csv với các cột được gắn nhãn, “Mạng”, “Hiển thị tên”, “Mùa”. Có lẽ chúng tôi chỉ muốn in số mùa từ này. csvimport csv
with open["shows.csv"] as shows_csv:
shows_dict = csv.DictReader[shows_csv]
for row in shows_dict:
print[row["Seasons"]]
Điều này sẽ in ra bảng điều khiển, trên một dòng mới, số mùa cho mỗi hàng tồn tại trong. csv
Cũng giống như với. txt, bạn cũng có thể tạo. tệp csv bằng Python. Nó phức tạp hơn một chút vì bạn cần xác định tiêu đề hoặc tên cột, nhưng đây vẫn là một quá trình nhanh chóng. Điều này có thể được sử dụng để lấy danh sách và biến chúng thành. tệp csv. Hãy xem ví dụ sau
import csv
working_list = [{"Network": "Netflix", "Show Name":"Bojack Horseman", "Seasons":6}, {"Network":"Channel 4","Show Name":"Derry Girls", "Seasons": 3}, {"Network":"HBO Max", "Show Name":"Our Flag Means Death", "Seasons": 1}]
with open["shows.csv", "w"] as shows_csv:
fields = ["Network", "Show Name", "Seasons"]
shows_w = csv.DictWriter[shows_csv, fieldnames = fields]
shows_w.writeheader[]
for item in working_list:
shows_w.writerow[item]
Khối mã trước đó tạo một tệp csv hoàn toàn mới bằng cách sử dụng tham số
import math
#This is doing the same work as the above example, but using Dict Comprehension instead.
comprehension_shows_characters = { shows:characters for shows, characters in zip[list_of_shows, list_of_characters] }
hip_to_be_square = [4, 9, 16, 25, 36, 49]
no_longer_hip_to_be_square = { key:math.sqrt[key] for key in hip_to_be_square }
print[no_longer_hip_to_be_square]
3 trong list_of_shows = ["Bojack Horseman",
"My Hero Academia",
"Ozark",
"Arrested Development",
"Derry Girls",
"Tuca & Bertie"]
list_of_characters = [["Todd", "Princess Carolyn", "Judah", "Diane"],
["Midoriya", "Shoto", "All Might", "Bakugo", "Kirishima"],
["Ruth", "Jonah", "Wyatt"],
["Tobias", "Gob", "Anne", "Maeby"],
["Sister Michael", "Orla", "Erin", "Claire", "James"],
["Bertie", "Speckle", "Tuca", "Dakota"]]
combined_shows_characters = dict[zip[list_of_shows, list_of_characters]]
print[combined_shows_characters]
5. Chúng tôi đặt tên các trường theo cách thủ công theo thứ tự chúng xuất hiện trong một danh sách riêng, sau đó chuyển danh sách đó vào tham số import math
#This is doing the same work as the above example, but using Dict Comprehension instead.
comprehension_shows_characters = { shows:characters for shows, characters in zip[list_of_shows, list_of_characters] }
hip_to_be_square = [4, 9, 16, 25, 36, 49]
no_longer_hip_to_be_square = { key:math.sqrt[key] for key in hip_to_be_square }
print[no_longer_hip_to_be_square]
5 import math
#This is doing the same work as the above example, but using Dict Comprehension instead.
comprehension_shows_characters = { shows:characters for shows, characters in zip[list_of_shows, list_of_characters] }
hip_to_be_square = [4, 9, 16, 25, 36, 49]
no_longer_hip_to_be_square = { key:math.sqrt[key] for key in hip_to_be_square }
print[no_longer_hip_to_be_square]
6. Cuối cùng, chúng ta sử dụng vòng lặp import math
#This is doing the same work as the above example, but using Dict Comprehension instead.
comprehension_shows_characters = { shows:characters for shows, characters in zip[list_of_shows, list_of_characters] }
hip_to_be_square = [4, 9, 16, 25, 36, 49]
no_longer_hip_to_be_square = { key:math.sqrt[key] for key in hip_to_be_square }
print[no_longer_hip_to_be_square]
7 và vòng lặp for với các phương thức import math
#This is doing the same work as the above example, but using Dict Comprehension instead.
comprehension_shows_characters = { shows:characters for shows, characters in zip[list_of_shows, list_of_characters] }
hip_to_be_square = [4, 9, 16, 25, 36, 49]
no_longer_hip_to_be_square = { key:math.sqrt[key] for key in hip_to_be_square }
print[no_longer_hip_to_be_square]
8 để tạo một hàng tiêu đề và lặp lại trên import math
#This is doing the same work as the above example, but using Dict Comprehension instead.
comprehension_shows_characters = { shows:characters for shows, characters in zip[list_of_shows, list_of_characters] }
hip_to_be_square = [4, 9, 16, 25, 36, 49]
no_longer_hip_to_be_square = { key:math.sqrt[key] for key in hip_to_be_square }
print[no_longer_hip_to_be_square]
9 và biến mỗi mục nhập thành một hàng trong. csvĐây chỉ là một vài cách để làm việc với. csv và. tệp txt;
“Nhà truyền bá giáo dục Pj Metz của chúng tôi tiếp tục hành trình học cách viết mã bằng Python. Lần này anh ấy học từ điển và cách đọc và ghi tệp từ mã. ” – Pj Metz