Hướng dẫn cartesian product of two dictionaries python - sản phẩm cartesian của hai từ điển python

Tôi đã gặp phải vấn đề này một thời gian trước đây để thực hiện các trường hợp thử nghiệm. Bây giờ tôi có một gói trên pip được gọi là 'looper', mở rộng itertools với một số phép thuật từ điển và những thứ khác mà tôi thấy hữu ích.

//pypi.python.org/pypi/looper

Những gì bạn muốn dường như không phải là một sản phẩm Full Cartesian của hai từ điển, sẽ dài 36 mặt hàng, kết hợp mỗi khóa D1 [K1] * D1 [K2] * D2 [K1] * D2 [K2].

Thay vào đó, bạn dường như muốn d1 [k1, k2] * d2 [k1, k2], lặp qua n đều cho mỗi khóa. Điều này được gọi là hàm zip và Dict_zip thực hiện điều này cho từ điển.

from pprint import pprint
from looper import iterutil

dict_1 = {'status':  ['online', 'Away', 'Offline'],
          'Absent':  ['yes', 'no', 'half day']}
dict_2 = {'healthy': ['yes', 'no'],
          'insane':  ['yes', 'no']}

# the first thing to do is to zip the dictionaries up. This produces a dictionary for each value of n in d[k][n]
zipped_dict_1 = iterutil.dict_zip[**dict_1]
# {'Absent': 'yes', 'status': 'online'}
# {'Absent': 'no', 'status': 'Away'}
# {'Absent': 'half day', 'status': 'Offline'}
zipped_dict_2 = iterutil.dict_zip[**dict_2]
# {'healthy': 'yes', 'insane': 'yes'}
# {'healthy': 'no', 'insane': 'no'}


# Now the output is a list of flattened dictionaries, take the Cartesian product of them.
product_dict = iterutil.product[zipped_dict_1,zipped_dict_2] 
# [{'Absent': 'yes', 'status': 'online'}, {'healthy': 'yes', 'insane': 'yes'}]
# [{'Absent': 'yes', 'status': 'online'}, {'healthy': 'no', 'insane': 'no'}]
# [{'Absent': 'no', 'status': 'Away'}, {'healthy': 'yes', 'insane': 'yes'}]
# [{'Absent': 'no', 'status': 'Away'}, {'healthy': 'no', 'insane': 'no'}]
# [{'Absent': 'half day', 'status': 'Offline'}, {'healthy': 'yes', 'insane': 'yes'}]
# [{'Absent': 'half day', 'status': 'Offline'}, {'healthy': 'no', 'insane': 'no'}]

# The product function produces tuples which must be combined in to a final dictionary.
# Merge the dictionaries using imap
merged_dict =  iterutil.imap[lambda x: dict[x[0].items[]+x[1].items[]],product_dict]

for d in merged_dict:
    pprint[d]

Đầu ra

{'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
{'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
{'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
{'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
{'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
{'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận
    The combination of above two can be used to perform this particular task. This is just a shorthand to the longer method of loops and can be used to perform this task in one line.

    Đôi khi, trong khi làm việc với từ điển, chúng ta có thể có vấn đề tiện ích trong đó chúng ta cần thực hiện hoạt động cơ bản giữa các khóa thông thường của từ điển. Điều này có thể được mở rộng cho bất kỳ hoạt động sẽ được thực hiện. Hãy để thảo luận về sản phẩm của các giá trị chính và các cách để giải quyết nó trong bài viết này.

    Phương pháp số 1: Sử dụng từ điển Hiểu + keys[] Sự kết hợp của hai trên có thể được sử dụng để thực hiện nhiệm vụ cụ thể này. Đây chỉ là một tốc ký cho phương pháp dài hơn của các vòng lặp và có thể được sử dụng để thực hiện nhiệm vụ này trong một dòng.

    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    5
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    6
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    7
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    8
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    9keys[]0

    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    5
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    6keys[]3
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    8
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    9keys[]6

    {'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
    {'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
    {'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
    {'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
    {'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
    {'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}
    
    12=
    {'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
    {'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
    {'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
    {'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
    {'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
    {'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}
    
    14

    {'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
    {'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
    {'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
    {'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
    {'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
    {'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}
    
    15=
    {'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
    {'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
    {'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
    {'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
    {'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
    {'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}
    
    17

    keys[]7=

    {'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
    {'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
    {'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
    {'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
    {'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
    {'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}
    
    20test_dict1 0
    {'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
    {'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
    {'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
    {'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
    {'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
    {'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}
    
    22test_dict1 5 test_dict1 6test_dict1 7
    {'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
    {'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
    {'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
    {'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
    {'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
    {'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}
    
    26

    Đầu ra:

    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    

    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    

    The combination of above method can be used to perform this particular task. In this, the Counter function converts the dictionary in the form in which the minus operator can perform the task of multiplication.

    Cải thiện bài viết

    Lưu bài viết

    Phương pháp số 1: Sử dụng từ điển Hiểu + keys[] Sự kết hợp của hai trên có thể được sử dụng để thực hiện nhiệm vụ cụ thể này. Đây chỉ là một tốc ký cho phương pháp dài hơn của các vòng lặp và có thể được sử dụng để thực hiện nhiệm vụ này trong một dòng.

    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    5
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    6
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    7
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    8
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    9keys[]0

    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    5
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    6keys[]3
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    8
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    9keys[]6

    {'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
    {'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
    {'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
    {'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
    {'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
    {'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}
    
    12=
    {'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
    {'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
    {'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
    {'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
    {'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
    {'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}
    
    14

    {'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
    {'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
    {'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
    {'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
    {'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
    {'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}
    
    15=
    {'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
    {'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
    {'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
    {'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
    {'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
    {'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}
    
    17

    keys[]7=

    {'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
    {'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
    {'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
    {'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
    {'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
    {'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}
    
    20test_dict1 0
    {'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
    {'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
    {'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
    {'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
    {'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
    {'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}
    
    22test_dict1 5 test_dict1 6test_dict1 7
    {'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
    {'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
    {'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
    {'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
    {'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
    {'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}
    
    26

    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    5
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    6=1
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    8
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    9
    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    
    6
    {'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
    {'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
    {'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
    {'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
    {'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
    {'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}
    
    33
    {'Absent': 'yes', 'healthy': 'yes', 'insane': 'yes', 'status': 'online'}
    {'Absent': 'yes', 'healthy': 'no', 'insane': 'no', 'status': 'online'}
    {'Absent': 'no', 'healthy': 'yes', 'insane': 'yes', 'status': 'Away'}
    {'Absent': 'no', 'healthy': 'no', 'insane': 'no', 'status': 'Away'}
    {'Absent': 'half day', 'healthy': 'yes', 'insane': 'yes', 'status': 'Offline'}
    {'Absent': 'half day', 'healthy': 'no', 'insane': 'no', 'status': 'Offline'}
    
    34

    Đầu ra:

    The original dictionary 1 : {'best': 7, 'is': 4, 'gfg': 6}
    The original dictionary 2 : {'best': 10, 'is': 6, 'gfg': 10}
    The product dictionary is : {'best': 70, 'is': 24, 'gfg': 60}
    


    Bài Viết Liên Quan

    Chủ Đề