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]]