Hướng dẫn convert key-value to dictionary python - chuyển đổi khóa-giá trị thành từ điển python
Đôi khi, trong khi làm việc với các chuỗi Python, chúng ta có thể gặp vấn đề trong đó chúng ta cần chuyển đổi một cặp giá trị khóa chuỗi chuỗi thành từ điển. Điều này có thể có các ứng dụng trong đó chúng tôi đang làm việc với dữ liệu chuỗi cần được chuyển đổi. Hãy để thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện. & NBSP; Show Phương thức số 1: Sử dụng map () + split () + loop & nbsp; Sự kết hợp của các chức năng trên có thể được sử dụng để thực hiện nhiệm vụ này. Trong đó, chúng tôi thực hiện chuyển đổi các cặp giá trị khóa thành từ điển bằng bản đồ và việc chia các cặp giá trị khóa được thực hiện bằng cách sử dụng split (). & Nbsp; Python3The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}1 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}2 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}3 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}4 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}5 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}6 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}7 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}8 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}9 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}0 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}2 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}2 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}3 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}4 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}5 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}6 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}7 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}8 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}9 0 1 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}5 3 4 5 6The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}5 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}8 9 1 1 2 3The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}0 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}2 6 7The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}4 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}5 0 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}7 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}8 3Đầu ra: & nbsp; The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'} Phương thức số 2: Sử dụng Dict () + Biểu thức máy phát + split () + map () & nbsp; Đây là một cách khác mà vấn đề này có thể được giải quyết. Trong đó, chúng tôi thực hiện nhiệm vụ theo cách tương tự như trên nhưng theo 1 cách sử dụng biểu thức Dict () và Trình tạo. & NBSP; Python3The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}1 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}2 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}3 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}4 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}5 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}6 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}7 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}8 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}9 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}0 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}2 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}2 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}3 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}4 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}5 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}6 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}7 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}8 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}4 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}5 0 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}7 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}8 3Đầu ra: & nbsp; The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'} Phương thức số 2: Sử dụng Dict () + Biểu thức máy phát + split () + map () & nbsp;
Độ phức tạp về thời gian và không gian cho tất cả các phương pháp là như nhau: Độ phức tạp về thời gian: O (n)The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}1 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}2 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}3 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}4 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}5 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}6 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}7 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}8 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}9 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}0 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}2 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}2 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}3 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}4 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}5 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}6 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}7 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}8 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}4 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}5 0 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}7 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}8 3Đầu ra: & nbsp; The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'} Tôi muốn lặp lại danh sách này The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}27 và trả lại một danh sách các từ điển cho mỗi nhóm. Ví dụ. The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}28 Tôi đã thử phân tách danh sách theo, (dấu phẩy) và tìm kiếm nó cho 'Tên:'. Tôi có thể trả lại một lĩnh vực như tên, nhưng tôi đang đấu tranh để liên kết với email, vai trò, v.v. Nhờ sự giúp đỡ trước. Đã hỏi ngày 7 tháng 6 năm 2019 lúc 22:59Jun 7, 2019 at 22:59
Leslie Alldridgeleslie AlldridgeLeslie Alldridge 1.2331 Huy hiệu vàng8 Huy hiệu bạc23 Huy hiệu đồng1 gold badge8 silver badges23 bronze badges 2 Không cần phải biết số lượng khóa mà mỗi Dict có trước, bạn có thể lặp lại qua danh sách, chia từng chuỗi thành một khóa và giá trị bằng và tiếp tục thêm giá trị vào dict cuối cùng bằng khóa:
Vì vậy, trong danh sách mẫu của bạn được lưu trữ trong The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}30, The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}31 sẽ trở thành:
Đã trả lời ngày 7 tháng 6 năm 2019 lúc 23:11Jun 7, 2019 at 23:11
0 Tôi giả sử rằng đơn đặt hàng của bạn luôn giống nhau, tức là, trong các nhóm 4. Ý tưởng là phân chia các chuỗi bằng cách sử dụng The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}32 và sau đó tạo các cặp khóa/giá trị và sử dụng lồng nhau cho các vòng lặp. The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}33 là để thoát khỏi khoảng trắngassuming that your order is always the same, i.e., in groups of 4. The idea is to split the strings using The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}32 and then create key/value pairs and use nested for loops. The The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}33 is to get rid of whitespace
Một danh sách hiểu sẽ trông giống như
Đã trả lời ngày 7 tháng 6 năm 2019 lúc 23:09Jun 7, 2019 at 23:09
SheldoresheldoreSheldore 36.3k6 Huy hiệu vàng48 Huy hiệu bạc64 Huy hiệu đồng6 gold badges48 silver badges64 bronze badges Bạn có thể làm:
Đã trả lời ngày 7 tháng 6 năm 2019 lúc 23:10Jun 7, 2019 at 23:10
Ayoub Zarouayoub ZarouAyoub ZAROU 2.3775 huy hiệu bạc19 huy hiệu đồng5 silver badges19 bronze badges Nếu hình thức của dữ liệu trong danh sách được đảm bảo luôn luôn là như vậy trong ví dụ của câu hỏi, thì bạn có thể làm điều này:
Output:
Đã trả lời ngày 7 tháng 6 năm 2019 lúc 23:12Jun 7, 2019 at 23:12
DjaouadnmdjaouadnmDjaouadNM 21.6K4 Huy hiệu vàng29 Huy hiệu bạc53 Huy hiệu Đồng4 gold badges29 silver badges53 bronze badges Giải pháp này giả định kích thước của mỗi nhóm là chính xác 4 The original string is : gfg:1, is:2, best:3 The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}0 Đã trả lời ngày 7 tháng 6 năm 2019 lúc 23:23Jun 7, 2019 at 23:23
GZ0GZ0GZ0 3,9221 Huy hiệu vàng9 Huy hiệu bạc20 Huy hiệu đồng 201 gold badge9 silver badges20 bronze badges Làm thế nào để bạn chuyển đổi các giá trị thành một từ điển?Để chuyển đổi các giá trị từ điển Python thành liệt kê, bạn có thể sử dụng phương thức Dict.Values () trả về một đối tượng Dict_Values. Đối tượng này có thể được lặp lại và nếu bạn chuyển nó vào hàm tạo list (), nó sẽ trả về một đối tượng danh sách với các giá trị từ điển dưới dạng các phần tử.use dict. values() method which returns a dict_values object. This object can be iterated, and if you pass it to list() constructor, it returns a list object with dictionary values as elements.
Làm thế nào để bạn trao đổi một khóaBạn có thể trao đổi các khóa và giá trị trong từ điển với sự hiểu biết từ điển và phương thức mục () ... Tạo một từ điển trong Python ({}, dict (), Dictorsions). Lặp lại từ điển (khóa và giá trị) với vòng lặp trong Python .. Làm thế nào để bạn chuyển đổi giữa các khóa và giá trị của từ điển trong Python?My_dict là cấu trúc dữ liệu có thể lặp lại có thể là từ điển, danh sách hoặc một tuple.Trong trường hợp này, chúng ta cần thêm phương thức.Các mục () trả về một danh sách các cặp (giá trị, giá trị).my_dict.
Làm thế nào để từ điển nhận được giá trị từ khóa?Bạn có thể sử dụng phương thức get () của từ điển (dict) để nhận bất kỳ giá trị mặc định nào mà không có lỗi nếu khóa không tồn tại.Chỉ định khóa là đối số đầu tiên.Giá trị tương ứng được trả về nếu khóa tồn tại và không có giá trị nào được trả về nếu khóa không tồn tại.use the get() method of the dictionary ( dict ) to get any default value without an error if the key does not exist. Specify the key as the first argument. The corresponding value is returned if the key exists, and None is returned if the key does not exist. |