Hướng dẫn python dictionary with list of lists as value - từ điển python với danh sách các danh sách là giá trị

Tôi có hai danh sách mà tôi muốn liên kết theo chỉ mục là các cặp giá trị chính trong một từ điển. Danh sách chính có nhiều yếu tố giống hệt nhau. Tôi muốn tất cả các yếu tố trong danh sách giá trị được ghép đôi như một danh sách danh sách. Tôi đang sử dụng phương thức Danh sách.Append (), nhưng điều này không cho tôi đầu ra mong muốn. Bất kỳ đề xuất nào về mã hoặc tôi nên xem xét vấn đề theo một cách khác?

list1 = ['a', 'b', 'b', 'b', 'c'] list2 = [['1', '2', '3'], ['4', '5', '6'], [ '7', '8', '9'], ['10', '11', '12'], ['13', '14', '15']] combo = {} for i in range(len(list1)): if list1[i] in combo: combo[list1[i]].append(list2[i]) else: combo[list1[i]] = list2[i]

Sản lượng hiện tại:

{'a': ['1', '2', '3'], 'b': ['4', '5', '6', [ '7', '8', '9'], ['10', '11', '12']], 'c': ['13', '14', 15']}

Kết quả mong muốn:

{'a': [['1', '2', '3']], 'b': [['4', '5', '6'], [ '7', '8', '9'], ['10', '11', '12']], 'c': [['13', '14', 15']]}

Đôi khi, trong khi làm việc với một từ điển Python, chúng ta có thể gặp vấn đề trong đó chúng ta cần thực hiện việc làm phẳng một cặp từ điển giá trị khóa sang danh sách và chuyển đổi nó thành danh sách danh sách. Điều này có thể có các ứng dụng trong các miền mà chúng tôi có dữ liệu. 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.

Phương pháp số 1: Sử dụng Loop + item () cách vũ phu này trong đó chúng ta có thể thực hiện nhiệm vụ này. Trong đó, chúng tôi lặp qua tất cả các cặp và trích xuất các phần tử giá trị danh sách bằng cách sử dụng các mục () và hiển thị chúng trong một danh sách mới. & NBSP; This brute force way in which we can perform this task. In this, we loop through all the pairs and extract list value elements using items() and render them in a new list. 

Python3

test_dict =

{'a': [['1', '2', '3']], 'b': [['4', '5', '6'], [ '7', '8', '9'], ['10', '11', '12']], 'c': [['13', '14', 15']]} 9The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 0The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 1 The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 2 The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 3The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 4

{'a': ['1', '2', '3'], 'b': ['4', '5', '6', [ '7', '8', '9'], ['10', '11', '12']], 'c': ['13', '14', 15']} 25= {'a': ['1', '2', '3'], 'b': ['4', '5', '6', [ '7', '8', '9'], ['10', '11', '12']], 'c': ['13', '14', 15']} 27{'a': ['1', '2', '3'], 'b': ['4', '5', '6', [ '7', '8', '9'], ['10', '11', '12']], 'c': ['13', '14', 15']} 28

The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 5= {'a': ['1', '2', '3'], 'b': ['4', '5', '6', [ '7', '8', '9'], ['10', '11', '12']], 'c': ['13', '14', 15']} 27The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 0__133333

{'a': [['1', '2', '3']], 'b': [['4', '5', '6'], [ '7', '8', '9'], ['10', '11', '12']], 'c': [['13', '14', 15']]} 9The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 0The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 8 The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 2 The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 3The original dictionary is : {'is': [7, 6], 'gfg': [1, 3, 4], 'best': [4, 5]} The converted list is : [['is', 7, 6], ['gfg', 1, 3, 4], ['best', 4, 5]] 1

{'a': [['1', '2', '3']], 'b': [['4', '5', '6'], [ '7', '8', '9'], ['10', '11', '12']], 'c': [['13', '14', 15']]} 9The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 0The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 8 The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 2 The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 3The original dictionary is : {'is': [7, 6], 'gfg': [1, 3, 4], 'best': [4, 5]} The converted list is : [['is', 7, 6], ['gfg', 1, 3, 4], ['best', 4, 5]] 1

Đầu ra

The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]]

Bạn có thể chuyển đổi một khóa không This task can also be performed using list comprehension. In this, we perform the task similar to the above method just in a one-liner shorter way. 

Python3

test_dict =

{'a': [['1', '2', '3']], 'b': [['4', '5', '6'], [ '7', '8', '9'], ['10', '11', '12']], 'c': [['13', '14', 15']]} 9The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 0The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 1 The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 2 The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 3The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 4

The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 5= The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 7

{'a': [['1', '2', '3']], 'b': [['4', '5', '6'], [ '7', '8', '9'], ['10', '11', '12']], 'c': [['13', '14', 15']]} 9The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 0The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 8 The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 2 The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 3The original dictionary is : {'is': [7, 6], 'gfg': [1, 3, 4], 'best': [4, 5]} The converted list is : [['is', 7, 6], ['gfg', 1, 3, 4], ['best', 4, 5]] 1

Đầu ra

The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]]

Bạn có thể chuyển đổi một khóa khôngUsing map and dict.keys() This task can also be performed. In this, we get the keys value of dict and iterate over the list of keys and get the values of corresponding values and concatenate both key and value, and form a list of lists. 

Python

test_dict = {'gfg' {'a': ['1', '2', '3'], 'b': ['4', '5', '6', [ '7', '8', '9'], ['10', '11', '12']], 'c': ['13', '14', 15']} 0{'a': ['1', '2', '3'], 'b': ['4', '5', '6', [ '7', '8', '9'], ['10', '11', '12']], 'c': ['13', '14', 15']} 1__12

{'a': [['1', '2', '3']], 'b': [['4', '5', '6'], [ '7', '8', '9'], ['10', '11', '12']], 'c': [['13', '14', 15']]} 9The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 0The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 1 The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 2 The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 3The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 4

{'a': ['1', '2', '3'], 'b': ['4', '5', '6', [ '7', '8', '9'], ['10', '11', '12']], 'c': ['13', '14', 15']} 25= {'a': ['1', '2', '3'], 'b': ['4', '5', '6', [ '7', '8', '9'], ['10', '11', '12']], 'c': ['13', '14', 15']} 27{'a': ['1', '2', '3'], 'b': ['4', '5', '6', [ '7', '8', '9'], ['10', '11', '12']], 'c': ['13', '14', 15']} 28

The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 5= {'a': ['1', '2', '3'], 'b': ['4', '5', '6', [ '7', '8', '9'], ['10', '11', '12']], 'c': ['13', '14', 15']} 27The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 0__133333

{'a': [['1', '2', '3']], 'b': [['4', '5', '6'], [ '7', '8', '9'], ['10', '11', '12']], 'c': [['13', '14', 15']]} 9The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 0The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 8 The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 2 The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]] 3The original dictionary is : {'is': [7, 6], 'gfg': [1, 3, 4], 'best': [4, 5]} The converted list is : [['is', 7, 6], ['gfg', 1, 3, 4], ['best', 4, 5]] 1

Đầu ra

The original dictionary is : {'is': [7, 6], 'gfg': [1, 3, 4], 'best': [4, 5]} The converted list is : [['is', 7, 6], ['gfg', 1, 3, 4], ['best', 4, 5]]


Bạn có thể chuyển đổi một khóa không

Phương pháp số 1: Sử dụng Loop + item () cách vũ phu này trong đó chúng ta có thể thực hiện nhiệm vụ này.Trong đó, chúng tôi lặp qua tất cả các cặp và trích xuất các phần tử giá trị danh sách bằng cách sử dụng các mục () và hiển thị chúng trong một danh sách mới.Using loop + items() This brute force way in which we can perform this task. In this, we loop through all the pairs and extract list value elements using items() and render them in a new list.

Làm thế nào để bạn lập một danh sách thành một từ điển?

Để chuyển đổi danh sách thành từ điển, chúng ta có thể sử dụng danh sách hiểu và tạo khóa: cặp giá trị của các yếu tố liên tiếp. Về mặt, typecase danh sách thành loại Dict.use list comprehension and make a key:value pair of consecutive elements. Finally, typecase the list to dict type.

Danh sách Tuple tập từ điển trong Python là gì?

Danh sách là một tập hợp các dữ liệu được đặt hàng.Một tuple là một bộ sưu tập dữ liệu được đặt hàng.Một bộ là một bộ sưu tập không có thứ tự.Từ điển là một bộ sưu tập dữ liệu không có thứ tự lưu trữ dữ liệu theo các cặp giá trị khóa.

Dict và danh sách toàn diện trong Python là gì?

Danh sách sự hiểu biết và sự hiểu biết từ điển là một sự thay thế mạnh mẽ cho các vòng lặp và cả các chức năng Lambda.Không chỉ thực hiện các danh sách và toàn bộ từ điển làm cho mã ngắn gọn và dễ đọc hơn, chúng còn nhanh hơn các vòng lặp truyền thống.a powerful substitute to for-loops and also lambda functions. Not only do list and dictionary comprehensions make code more concise and easier to read, they are also faster than traditional for-loops.

Chủ đề