Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Trong Python, từ điển là một bộ sưu tập không có thứ tự, có thể thay đổi và được lập chỉ mục. Từ điển được viết bằng dấu ngoặc xoăn, và chúng có khóa và giá trị. Nó được sử dụng để băm một khóa cụ thể.
Using
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}0 function here we visit each key one by one and remove space with the none. Here translate function takes parameter 32, none where 32 is ASCII value of space ‘ ‘ and replaces it with none.
Hãy cùng xem cách loại bỏ các khoảng trống khỏi các khóa từ điển trong Python.
{k.translate[None, ' ']: v for k, v in dictionary.iteritems[]}
2{k.translate[None, ' ']: v for k, v in dictionary.iteritems[]}
3New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}5
{k.translate[None, ' ']: v for k, v in dictionary.iteritems[]}
5{k.translate[None, ' ']: v for k, v in dictionary.iteritems[]}
6Phương pháp số 1: Sử dụng hàm
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}0 Ở đây, chúng tôi truy cập từng khóa một và xóa không gian bằng không có. Ở đây dịch hàm lấy tham số 32, không có giá trị ASCII nào của không gian ‘và thay thế nó bằng không.
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}1
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}2
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}3
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}4
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}5
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}6
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}7__
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}1
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}2
{k.translate[None, ' ']: v for k, v in dictionary.iteritems[]}
9{k.translate[{32: None}]: v for k, v in dictionary.items[]}
0{k.translate[{32: None}]: v for k, v in dictionary.items[]}
1{k.translate[{32: None}]: v for k, v in dictionary.items[]}
2{k.translate[{32: None}]: v for k, v in dictionary.items[]}
3Output:
New dictionary : {'P01': 'DBMS', 'P03': 'Soft Computing', 'P02': 'OS'}
{k.translate[{32: None}]: v for k, v in dictionary.items[]}
4{k.translate[{32: None}]: v for k, v in dictionary.items[]}
5 {k.translate[{32: None}]: v for k, v in dictionary.items[]}
6{k.translate[{32: None}]: v for k, v in dictionary.items[]}
7 {k.translate[{32: None}]: v for k, v in dictionary.items[]}
8Method #2:
Using
{k.replace[' ', '']: v for k, v in dictionary.items[]}
3 function. In this method, we visit each key in dictionary one by one and replace all spaces in key
with no space. This function takes as argument space and second non-space.Hãy cùng xem cách loại bỏ các khoảng trống khỏi các khóa từ điển trong Python.
{k.translate[None, ' ']: v for k, v in dictionary.iteritems[]}
2{k.translate[None, ' ']: v for k, v in dictionary.iteritems[]}
3New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}5
{k.translate[None, ' ']: v for k, v in dictionary.iteritems[]}
5>>> d = {'C 14': ['15263808', '13210478'], 'W 1': ['13122205']}
>>> {k.translate[None, ' ']: v for k, v in d.iteritems[]}
{'W1': ['13122205'], 'C14': ['15263808', '13210478']}
9Phương pháp số 1: Sử dụng hàm
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}0 Ở đây, chúng tôi truy cập từng khóa một và xóa không gian bằng không có. Ở đây dịch hàm lấy tham số 32, không có giá trị ASCII nào của không gian ‘và thay thế nó bằng không.
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}1
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}2
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}3
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}4
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}5
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}6
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}7__
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}1
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}2
{k.translate[None, ' ']: v for k, v in dictionary.iteritems[]}
9{k.translate[{32: None}]: v for k, v in dictionary.items[]}
0{k.translate[{32: None}]: v for k, v in dictionary.items[]}
1{k.translate[{32: None}]: v for k, v in dictionary.items[]}
2{k.translate[{32: None}]: v for k, v in dictionary.items[]}
3Output:
New dictionary : {'P03': 'Soft Computing', 'P01': 'DBMS', 'P02': 'OS'}
Bạn có thể xử lý tất cả các khóa trong một từ điển hiểu; Bạn có thể sử dụng
student_list = {'S 001': ['Math', 'Science'], 'S 002': ['Math', 'English']}
print["Original dictionary: ",student_list]
student_dict = {x.translate[{32: None}]: y for x, y in student_list.items[]}
print["New dictionary: ",student_dict]
4 để loại bỏ các không gian; Phiên bản Python 2 cho điều đó là:{k.translate[None, ' ']: v for k, v in dictionary.iteritems[]}
Và phiên bản Python 3 là:
{k.translate[{32: None}]: v for k, v in dictionary.items[]}
Bạn cũng có thể sử dụng
student_list = {'S 001': ['Math', 'Science'], 'S 002': ['Math', 'English']}
print["Original dictionary: ",student_list]
student_dict = {x.translate[{32: None}]: y for x, y in student_list.items[]}
print["New dictionary: ",student_dict]
5 để loại bỏ các khoảng trống:{k.replace[' ', '']: v for k, v in dictionary.items[]}
Nhưng điều này có thể chậm hơn cho các phím dài hơn; Xem Python str.translate vs str.replace
Lưu ý rằng những thứ này tạo ra một từ điển mới.
Bản demo trên Python 2:
>>> d = {'C 14': ['15263808', '13210478'], 'W 1': ['13122205']}
>>> {k.translate[None, ' ']: v for k, v in d.iteritems[]}
{'W1': ['13122205'], 'C14': ['15263808', '13210478']}
Và phiên bản tương thích 2.6 [sử dụng phương án để hiểu biết trước Python 2.7]:
dict[[k.translate[None, ' '], v] for k, v in dictionary.iteritems[]]
Từ điển Python: Bài tập-29 với giải pháp
Viết một chương trình Python để loại bỏ khoảng trống khỏi các khóa từ điển.
Giải pháp mẫu:-:-
Mã Python:
student_list = {'S 001': ['Math', 'Science'], 'S 002': ['Math', 'English']}
print["Original dictionary: ",student_list]
student_dict = {x.translate[{32: None}]: y for x, y in student_list.items[]}
print["New dictionary: ",student_dict]
Đầu ra mẫu:
Original dictionary: {'S 001': ['Math', 'Science'], 'S 002': ['Math', 'English']} New dictionary: {'S001': ['Math', 'Science'], 'S002': ['Math', 'English']}
Trình bày bằng hình ảnh:
Viết một chương trình Python để loại bỏ khoảng trống khỏi các khóa từ điển.
Trực quan hóa thực thi mã Python:
Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:
Trình chỉnh sửa mã Python:
Có một cách khác để giải quyết giải pháp này? Đóng góp mã của bạn [và nhận xét] thông qua Disqus.
Trước đây: Viết một chương trình Python để sắp xếp danh sách theo thứ tự bảng chữ cái trong từ điển. Write a Python program to sort a list alphabetically in a dictionary.
Next: Write a Python program to get the top three items in a shop.
Python: Lời khuyên trong ngày
Tạo một trình lặp lại lọc các phần tử từ Itable chỉ trả lại chỉ cho các vị từ là sai:
>>> import itertools # keeping only false values >>> list[itertools.filterfalse[bool, [None, False, 1, 0, 10]]] [None, False, 0]