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. https://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.
Đầu ra Cải thiện bài viết Lưu bài viết Cải thiện bài viết Lưu bài viết Đọc Bàn luận Đô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 + 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}9 keys() 0The 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 keys() 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}9 keys() 6 12= 14 15= 17
20test_dict1 0 22test_dict1 5 test_dict1 6test_dict1 7 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}
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 + 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}9 keys() 0The 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 keys() 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}9 keys() 6 12= 14 15= 17
20test_dict1 0 22test_dict1 5 test_dict1 6test_dict1 7 26The 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 33 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} |