{"id":1357,"date":"2022-10-28T17:13:18","date_gmt":"2022-10-28T09:13:18","guid":{"rendered":"http:\/\/175.24.184.226\/?p=1357"},"modified":"2022-10-28T17:13:18","modified_gmt":"2022-10-28T09:13:18","slug":"python-product%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"\/?p=1357","title":{"rendered":"python\u2014\u2014product()\u51fd\u6570"},"content":{"rendered":"<h2>\u4e00\u3001\u6982\u8ff0<\/h2>\n<p>product \u7528\u4e8e\u6c42\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u7b1b\u5361\u5c14\u79ef(Cartesian Product)\uff0c\u5b83\u8ddf\u5d4c\u5957\u7684 for \u5faa\u73af\u7b49\u4ef7.\u5373:<strong>product(A, B) \u548c ((x,y) for x in A for y in B)\u4e00\u6837.<\/strong><\/p>\n<p>\u5b83\u7684\u4e00\u822c\u4f7f\u7528\u5f62\u5f0f\u5982\u4e0b\uff1a<\/p>\n<blockquote>\n<pre><code>itertools.product(\\*iterables, repeat=1)<\/code><\/pre>\n<\/blockquote>\n<hr \/>\n<h2>\u4e8c\u3001\u5b9e\u4f8b\u8bf4\u660e<\/h2>\n<h3>\uff08\u4e00\uff09\u5bf9\u5217\u8868\u83b7\u53d6\u591a\u4e2a\u65e0\u7a77\u5faa\u73af\u5668\u7684\u7b1b\u5361\u5c14\u79ef<\/h3>\n<pre><code>from itertools import product\na = [&#039;a&#039;,&#039;b&#039;,&#039;c&#039;]\nb = [1,2,3]\nfor i ,j in product(a,b):  #\u5bf9\u5217\u8868\u83b7\u53d6\u591a\u4e2a\u65e0\u7a77\u5faa\u73af\u5668\u7684\u7b1b\u5361\u5c14\u79ef\n    print (i,j)\nprint (&#039;-----------------------------------------------------&#039;)<\/code><\/pre>\n<p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<img src=\"https:\/\/img-blog.csdnimg.cn\/a98abaaec1c44ab18d8b03d42cee5fad.png\" alt=\"\" \/><\/strong><\/p>\n<h3>\uff08\u4e8c\uff09\u5bf9\u5143\u7ec4\u83b7\u53d6\u591a\u4e2a\u65e0\u7a77\u5faa\u73af\u5668\u7684\u7b1b\u5361\u5c14\u79ef<\/h3>\n<pre><code>from itertools import product\nc = (&#039;e&#039;,&#039;f&#039;,&#039;g&#039;)\nd = (4,5,6)\nfor i,j in product(c,d):#\u5bf9\u5143\u7ec4\u83b7\u53d6\u591a\u4e2a\u65e0\u7a77\u5faa\u73af\u5668\u7684\u7b1b\u5361\u5c14\u79ef\n    print (i,j)\nprint (&#039;-----------------------------------------------------&#039;)<\/code><\/pre>\n<p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<img src=\"https:\/\/img-blog.csdnimg.cn\/3379a62134b14808b2bd6b6d90816a57.png?x-oss-process=image\/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGlhb2NodWhlLg==,size_15,color_FFFFFF,t_70,g_se,x_16\" alt=\"\" \/><\/strong><\/p>\n<h3>\uff08\u4e09\uff09\u5bf9\u5b57\u5178\u83b7\u53d6\u591a\u4e2a\u65e0\u7a77\u5faa\u73af\u5668\u7684\u7b1b\u5361\u5c14\u79ef<\/h3>\n<pre><code>from itertools import product\ne = {&#039;\u9752\u9e1f&#039;:&#039;\u6700\u7f8e&#039;,&#039;\u4e16\u5b50&#039;:&#039;\u7ea8\u7ed4&#039;}\nj = {&#039;xiaofeng&#039;:&#039;1&#039;,&#039;\u5e9f\u7269&#039;:&#039;2&#039;}#\u5bf9\u5b57\u5178\u83b7\u53d6\u591a\u4e2a\u65e0\u7a77\u5faa\u73af\u5668\u7684\u7b1b\u5361\u5c14\u79ef\nfor i,j in product(e,j):\n    print (i,j)   #\u5b57\u5178\u53ea\u5bf9\u952e\u8fdb\u884c\u7b1b\u5361\u5c14\u79ef\u4ea4\u6362\nprint (&#039;-----------------------------------------------------&#039;)<\/code><\/pre>\n<p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<img src=\"https:\/\/img-blog.csdnimg.cn\/53dfb9636bc343638363511bd8e63487.png\" alt=\"\" \/><\/strong><\/p>\n<h3><strong>\u6ce8\uff1a\u5b57\u5178\u53ea\u5bf9\u952e\u8fdb\u884c\u7b1b\u5361\u5c14\u79ef\u4ea4\u6362\uff01\uff01\uff01<\/strong><\/h3>\n<h2>\u4ee3\u7801\u793a\u4f8b<\/h2>\n<pre><code class=\"language-python\"># -*- coding: UTF-8 -*-\n&quot;&quot;&quot;=========================================================\n@Project -&gt; File: PythonLocal -&gt; 1.product\n@IDE: PyCharm\n@author: lxc\n@date: 2022\/10\/28 \u4e0b\u5348 5:02\n@Desc:\n\u529f\u80fd\u63cf\u8ff0\uff1a\nproduct\u65b9\u6cd5\u63cf\u8ff0\n\u5176\u5b9e\u4e5f\u6ca1\u6709\u6539\u53d8\u5faa\u73af\u6b21\u6570\uff0c\u5c31\u662f\u53d6\u7b1b\u5361\u5c14\u79ef\n&quot;&quot;&quot;\nfrom itertools import product\n\ndef list_product():\n    &quot;&quot;&quot;\n    \uff08\u4e00\uff09\u5bf9\u5217\u8868\u83b7\u53d6\u591a\u4e2a\u65e0\u7a77\u5faa\u73af\u5668\u7684\u7b1b\u5361\u5c14\u79ef\n    :return:\n    &quot;&quot;&quot;\n    from itertools import product\n    a = [&#039;a&#039;, &#039;b&#039;, &#039;c&#039;]\n    b = [1, 2, 3]\n    for i, j in product(a, b):  # \u5bf9\u5217\u8868\u83b7\u53d6\u591a\u4e2a\u65e0\u7a77\u5faa\u73af\u5668\u7684\u7b1b\u5361\u5c14\u79ef\n        print(i, j)\n    print(&#039;-----------------------------------------------------&#039;)\n\ndef tuple_product():\n    &quot;&quot;&quot;\n    \uff08\u4e8c\uff09\u5bf9\u5143\u7ec4\u83b7\u53d6\u591a\u4e2a\u65e0\u7a77\u5faa\u73af\u5668\u7684\u7b1b\u5361\u5c14\u79ef\n    :return:\n    &quot;&quot;&quot;\n    from itertools import product\n    c = (&#039;e&#039;, &#039;f&#039;, &#039;g&#039;)\n    d = (4, 5, 6)\n    for i, j in product(c, d):  # \u5bf9\u5143\u7ec4\u83b7\u53d6\u591a\u4e2a\u65e0\u7a77\u5faa\u73af\u5668\u7684\u7b1b\u5361\u5c14\u79ef\n        print(i, j)\n    print(&#039;-----------------------------------------------------&#039;)\n\ndef dict_product():\n    &quot;&quot;&quot;\n    \u5bf9\u5b57\u5178\u83b7\u53d6\u591a\u4e2a\u65e0\u7a77\u5faa\u73af\u5668\u7684\u7b1b\u5361\u5c14\u79ef\n    :return:\n    &quot;&quot;&quot;\n    e = {&#039;\u9752\u9e1f&#039;: &#039;\u6700\u7f8e&#039;, &#039;\u4e16\u5b50&#039;: &#039;\u7ea8\u7ed4&#039;}\n    j = {&#039;xiaofeng&#039;: &#039;1&#039;, &#039;\u5e9f\u7269&#039;: &#039;2&#039;}  # \u5bf9\u5b57\u5178\u83b7\u53d6\u591a\u4e2a\u65e0\u7a77\u5faa\u73af\u5668\u7684\u7b1b\u5361\u5c14\u79ef\n    for i, j in product(e, j):\n        print(i, j)  # \u5b57\u5178\u53ea\u5bf9\u952e\u8fdb\u884c\u7b1b\u5361\u5c14\u79ef\u4ea4\u6362\n\nif __name__ == &#039;__main__&#039;:\n    list_product()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u6982\u8ff0 product \u7528\u4e8e\u6c42\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u7b1b\u5361\u5c14\u79ef(Cartesian Product)\uff0c\u5b83\u8ddf\u5d4c\u5957\u7684 for \u5faa\u73af\u7b49\u4ef7.\u5373:pro   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[34],"tags":[16],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/1357"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1357"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/1357\/revisions"}],"predecessor-version":[{"id":1358,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/1357\/revisions\/1358"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1357"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}