{"id":1170234,"date":"2025-01-15T16:20:08","date_gmt":"2025-01-15T08:20:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1170234.html"},"modified":"2025-01-15T16:20:11","modified_gmt":"2025-01-15T08:20:11","slug":"python%e5%88%97%e8%a1%a8%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%batuple","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1170234.html","title":{"rendered":"python\u5217\u8868\u4e2d\u5982\u4f55\u521b\u5efatuple"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26070952\/bb7b21d0-e745-476f-88c5-cae6fd3012f8.webp\" alt=\"python\u5217\u8868\u4e2d\u5982\u4f55\u521b\u5efatuple\" \/><\/p>\n<p><p> <strong>\u5728Python\u5217\u8868\u4e2d\u521b\u5efatuple\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u76f4\u63a5\u521b\u5efa\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u7b49\u3002\u76f4\u63a5\u521b\u5efa\u662f\u6700\u5e38\u7528\u4e14\u6700\u7b80\u5355\u7684\u4e00\u79cd\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f8b\u5982\uff1a\u76f4\u63a5\u521b\u5efa\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u3002<\/strong><br \/>\u76f4\u63a5\u521b\u5efa\u662f\u6700\u5e38\u7528\u4e14\u6700\u7b80\u5355\u7684\u4e00\u79cd\u65b9\u6cd5\u3002\u901a\u8fc7\u76f4\u63a5\u5728\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7ec4\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u521b\u5efa\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002\u6bd4\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_with_tuples = [(1, 2), (3, 4), (5, 6)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4e0d\u540c\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u76f4\u63a5\u521b\u5efatuple\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/strong><\/h2>\n<p><p>\u76f4\u63a5\u5728\u5217\u8868\u4e2d\u6dfb\u52a0\u5143\u7ec4\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u4e5f\u662f\u6700\u76f4\u89c2\u7684\u4e00\u79cd\u3002\u4f60\u53ea\u9700\u8981\u5c06\u5143\u7ec4\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u5373\u53ef\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_with_tuples = [(1, 2), (3, 4), (5, 6)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>list_with_tuples<\/code>\u662f\u4e00\u4e2a\u5305\u542b\u4e09\u4e2a\u5143\u7ec4\u7684\u5217\u8868\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6dfb\u52a0\u66f4\u591a\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u6dfb\u52a0\u591a\u4e2a\u5143\u7ec4<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">list_with_tuples = []<\/p>\n<p>list_with_tuples.append((1, 2))<\/p>\n<p>list_with_tuples.append((3, 4))<\/p>\n<p>list_with_tuples.append((5, 6))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7a7a\u5217\u8868\uff0c\u7136\u540e\u4f7f\u7528<code>append<\/code>\u65b9\u6cd5\u5411\u5217\u8868\u4e2d\u6dfb\u52a0\u591a\u4e2a\u5143\u7ec4\u3002\u8fd9\u6837\u53ef\u4ee5\u66f4\u52a0\u52a8\u6001\u5730\u6dfb\u52a0\u5143\u7ec4\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efatuple\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/strong><\/h2>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662fPython\u4e2d\u975e\u5e38\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u7279\u6027\uff0c\u5b83\u5141\u8bb8\u4f60\u5728\u4e00\u884c\u4ee3\u7801\u4e2d\u521b\u5efa\u5217\u8868\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u521b\u5efa\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">list_with_tuples = [(i, i + 1) for i in range(1, 6)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e94\u4e2a\u5143\u7ec4\u7684\u5217\u8868\u3002\u6bcf\u4e2a\u5143\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f<code>i<\/code>\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u662f<code>i + 1<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u4f7f\u7528\u6761\u4ef6\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u5728\u5217\u8868\u63a8\u5bfc\u5f0f\u4e2d\u4f7f\u7528\u6761\u4ef6\u8868\u8fbe\u5f0f\u6765\u521b\u5efa\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_with_tuples = [(i, i + 1) for i in range(1, 6) if i % 2 == 0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4ec5\u521b\u5efa\u90a3\u4e9b\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u5076\u6570\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u521b\u5efatuple\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/strong><\/h2>\n<p><p><code>zip<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff08\u4f8b\u5982\u5217\u8868\uff09\u7ec4\u5408\u6210\u4e00\u4e2a\u5143\u7ec4\u7684\u8fed\u4ee3\u5668\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u6765\u521b\u5efa\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u4f7f\u7528<code>zip<\/code>\u51fd\u6570<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 3, 5]<\/p>\n<p>list2 = [2, 4, 6]<\/p>\n<p>list_with_tuples = list(zip(list1, list2))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u5c06\u4e24\u4e2a\u5217\u8868<code>list1<\/code>\u548c<code>list2<\/code>\u7ec4\u5408\u6210\u4e00\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868<code>list_with_tuples<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u4f7f\u7528\u591a\u4e2a\u5217\u8868<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u7ec4\u5408\u591a\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<p>list3 = [7, 8, 9]<\/p>\n<p>list_with_tuples = list(zip(list1, list2, list3))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4e09\u4e2a\u5217\u8868\u7ec4\u5408\u6210\u4e00\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528<code>map<\/code>\u51fd\u6570\u521b\u5efatuple\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/strong><\/h2>\n<p><p><code>map<\/code>\u51fd\u6570\u662fPython\u4e2d\u53e6\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u5230\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u6bcf\u4e2a\u5143\u7d20\u4e0a\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>map<\/code>\u51fd\u6570\u6765\u521b\u5efa\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u4f7f\u7528<code>map<\/code>\u51fd\u6570<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 3, 5]<\/p>\n<p>list2 = [2, 4, 6]<\/p>\n<p>list_with_tuples = list(map(lambda x, y: (x, y), list1, list2))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>map<\/code>\u51fd\u6570\u548c\u4e00\u4e2a<code>lambda<\/code>\u8868\u8fbe\u5f0f\u5c06\u4e24\u4e2a\u5217\u8868\u7ec4\u5408\u6210\u4e00\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u4f7f\u7528\u591a\u4e2a\u5217\u8868<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>map<\/code>\u51fd\u6570\u7ec4\u5408\u591a\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2, 3]<\/p>\n<p>list2 = [4, 5, 6]<\/p>\n<p>list3 = [7, 8, 9]<\/p>\n<p>list_with_tuples = list(map(lambda x, y, z: (x, y, z), list1, list2, list3))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4e09\u4e2a\u5217\u8868\u7ec4\u5408\u6210\u4e00\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u521b\u5efatuple\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/strong><\/h2>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u9700\u8981\u521b\u5efa\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\uff0c\u5176\u4e2d\u5305\u542b\u591a\u4e2a\u5c42\u6b21\u7684\u5143\u7ec4\u548c\u5217\u8868\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u521b\u5efa\u5d4c\u5957\u5217\u8868<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">nested_list_with_tuples = [<\/p>\n<p>    [(1, 2), (3, 4)],<\/p>\n<p>    [(5, 6), (7, 8)]<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u5217\u8868\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5b50\u5217\u8868\u4e2d\u5305\u542b\u4e24\u4e2a\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u52a8\u6001\u521b\u5efa\u5d4c\u5957\u5217\u8868<\/h2>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u52a8\u6001\u521b\u5efa\u5d4c\u5957\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_list_with_tuples = []<\/p>\n<p>for i in range(2):<\/p>\n<p>    sublist = []<\/p>\n<p>    for j in range(2):<\/p>\n<p>        sublist.append((i * 2 + j + 1, i * 2 + j + 2))<\/p>\n<p>    nested_list_with_tuples.append(sublist)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5d4c\u5957\u5faa\u73af\u52a8\u6001\u521b\u5efa\u4e86\u4e00\u4e2a\u5d4c\u5957\u5217\u8868\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u4f7f\u7528<code>itertools.product<\/code>\u521b\u5efatuple\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/strong><\/h2>\n<p><p><code>itertools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u5904\u7406\u8fed\u4ee3\u5668\u7684\u51fd\u6570\u3002<code>itertools.product<\/code>\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u7b1b\u5361\u5c14\u79ef\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u521b\u5efa\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u4f7f\u7528<code>itertools.product<\/code><\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>list1 = [1, 2]<\/p>\n<p>list2 = [3, 4]<\/p>\n<p>list_with_tuples = list(itertools.product(list1, list2))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>itertools.product<\/code>\u751f\u6210\u4e86\u4e24\u4e2a\u5217\u8868\u7684\u7b1b\u5361\u5c14\u79ef\uff0c\u7ed3\u679c\u662f\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u4f7f\u7528\u591a\u4e2a\u5217\u8868<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>itertools.product<\/code>\u7ec4\u5408\u591a\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [1, 2]<\/p>\n<p>list2 = [3, 4]<\/p>\n<p>list3 = [5, 6]<\/p>\n<p>list_with_tuples = list(itertools.product(list1, list2, list3))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4e09\u4e2a\u5217\u8868\u7ec4\u5408\u6210\u4e00\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u521b\u5efatuple\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/strong><\/h2>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4e0d\u4f1a\u7acb\u5373\u751f\u6210\u5217\u8868\uff0c\u800c\u662f\u8fd4\u56de\u4e00\u4e2a\u751f\u6210\u5668\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6765\u521b\u5efa\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">list_with_tuples = list((i, i + 1) for i in range(1, 6))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u4e94\u4e2a\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u4f7f\u7528\u6761\u4ef6\u8868\u8fbe\u5f0f<\/h2>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u5728\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u6761\u4ef6\u8868\u8fbe\u5f0f\u6765\u521b\u5efa\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list_with_tuples = list((i, i + 1) for i in range(1, 6) if i % 2 == 0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4ec5\u521b\u5efa\u90a3\u4e9b\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u5076\u6570\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<h2><strong>\u516b\u3001\u4f7f\u7528<code>namedtuple<\/code>\u521b\u5efatuple\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/strong><\/h2>\n<p><p><code>namedtuple<\/code>\u662fPython\u6807\u51c6\u5e93<code>collections<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u5de5\u5382\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u521b\u5efa\u5e26\u5b57\u6bb5\u540d\u7684\u5143\u7ec4\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>namedtuple<\/code>\u6765\u521b\u5efa\u66f4\u5177\u53ef\u8bfb\u6027\u7684\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u4f7f\u7528<code>namedtuple<\/code><\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import namedtuple<\/p>\n<p>Point = namedtuple(&#39;Point&#39;, [&#39;x&#39;, &#39;y&#39;])<\/p>\n<p>list_with_tuples = [Point(1, 2), Point(3, 4), Point(5, 6)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>namedtuple<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>Point<\/code>\u7684\u5143\u7ec4\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u8bbf\u95ee\u5b57\u6bb5<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b57\u6bb5\u540d\u8bbf\u95ee<code>namedtuple<\/code>\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for point in list_with_tuples:<\/p>\n<p>    print(f&#39;x: {point.x}, y: {point.y}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5b57\u6bb5\u540d<code>x<\/code>\u548c<code>y<\/code>\u8bbf\u95ee\u5e76\u6253\u5370<code>namedtuple<\/code>\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<h2><strong>\u4e5d\u3001\u4f7f\u7528<code>dataclass<\/code>\u521b\u5efatuple\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/strong><\/h2>\n<p><p><code>dataclass<\/code>\u662fPython 3.7\u5f15\u5165\u7684\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u7528\u4e8e\u7b80\u5316\u7c7b\u7684\u5b9a\u4e49\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>dataclass<\/code>\u6765\u521b\u5efa\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u4f7f\u7528<code>dataclass<\/code><\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">from dataclasses import dataclass<\/p>\n<p>@dataclass<\/p>\n<p>class Point:<\/p>\n<p>    x: int<\/p>\n<p>    y: int<\/p>\n<p>list_with_tuples = [Point(1, 2), Point(3, 4), Point(5, 6)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>dataclass<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>Point<\/code>\u7684\u7c7b\uff0c\u5e76\u5c06\u5176\u5b9e\u4f8b\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u8bbf\u95ee\u5b57\u6bb5<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b57\u6bb5\u540d\u8bbf\u95ee<code>dataclass<\/code>\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for point in list_with_tuples:<\/p>\n<p>    print(f&#39;x: {point.x}, y: {point.y}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u5b57\u6bb5\u540d<code>x<\/code>\u548c<code>y<\/code>\u8bbf\u95ee\u5e76\u6253\u5370<code>dataclass<\/code>\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u521b\u5efatuple\u5e76\u6dfb\u52a0\u5230\u5217\u8868\u4e2d<\/strong><\/h2>\n<p><p>\u4f60\u53ef\u4ee5\u5b9a\u4e49\u81ea\u5df1\u7684\u51fd\u6570\u6765\u521b\u5efa\u5143\u7ec4\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\u548c\u53ef\u91cd\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u5b9a\u4e49\u81ea\u5b9a\u4e49\u51fd\u6570<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">def create_tuple(a, b):<\/p>\n<p>    return (a, b)<\/p>\n<p>list_with_tuples = [create_tuple(1, 2), create_tuple(3, 4), create_tuple(5, 6)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>create_tuple<\/code>\u7684\u51fd\u6570\uff0c\u5e76\u4f7f\u7528\u5b83\u521b\u5efa\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u548c\u5faa\u73af<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u52a8\u6001\u521b\u5efa\u5143\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def create_tuple(a, b):<\/p>\n<p>    return (a, b)<\/p>\n<p>list_with_tuples = []<\/p>\n<p>for i in range(1, 6):<\/p>\n<p>    list_with_tuples.append(create_tuple(i, i + 1))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5faa\u73af\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u52a8\u6001\u521b\u5efa\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e00\u3001\u4f7f\u7528<code>itertools.ch<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n<\/code>\u5408\u5e76\u591a\u4e2a\u5217\u8868\u5e76\u521b\u5efatuple<\/strong><\/h2>\n<p><p><code>itertools.chain<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u591a\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u94fe\u63a5\u5728\u4e00\u8d77\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5408\u5e76\u591a\u4e2a\u5217\u8868\u5e76\u521b\u5efa\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u4f7f\u7528<code>itertools.chain<\/code><\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>list1 = [(1, 2), (3, 4)]<\/p>\n<p>list2 = [(5, 6), (7, 8)]<\/p>\n<p>list_with_tuples = list(itertools.chain(list1, list2))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>itertools.chain<\/code>\u5c06\u4e24\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u5408\u5e76\u6210\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u4f7f\u7528\u591a\u4e2a\u5217\u8868<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>itertools.chain<\/code>\u5408\u5e76\u591a\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">list1 = [(1, 2)]<\/p>\n<p>list2 = [(3, 4)]<\/p>\n<p>list3 = [(5, 6)]<\/p>\n<p>list_with_tuples = list(itertools.chain(list1, list2, list3))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u4e09\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u5408\u5e76\u6210\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e8c\u3001\u4f7f\u7528<code>chain.from_iterable<\/code>\u5408\u5e76\u5d4c\u5957\u5217\u8868\u5e76\u521b\u5efatuple<\/strong><\/h2>\n<p><p><code>chain.from_iterable<\/code>\u662f<code>itertools.chain<\/code>\u7684\u4e00\u4e2a\u7c7b\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5c06\u5d4c\u5957\u7684\u53ef\u8fed\u4ee3\u5bf9\u8c61\u5c55\u5f00\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u5408\u5e76\u5d4c\u5957\u5217\u8868\u5e76\u521b\u5efa\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u4f7f\u7528<code>chain.from_iterable<\/code><\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import itertools<\/p>\n<p>nested_list = [[(1, 2), (3, 4)], [(5, 6), (7, 8)]]<\/p>\n<p>list_with_tuples = list(itertools.chain.from_iterable(nested_list))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>chain.from_iterable<\/code>\u5c06\u5d4c\u5957\u5217\u8868\u5c55\u5f00\u6210\u4e00\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h2>\u793a\u4f8b\uff1a\u4f7f\u7528\u591a\u4e2a\u5d4c\u5957\u5217\u8868<\/h2>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>chain.from_iterable<\/code>\u5408\u5e76\u591a\u4e2a\u5d4c\u5957\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">nested_list1 = [[(1, 2)], [(3, 4)]]<\/p>\n<p>nested_list2 = [[(5, 6)], [(7, 8)]]<\/p>\n<p>list_with_tuples = list(itertools.chain.from_iterable(nested_list1 + nested_list2))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u591a\u4e2a\u5d4c\u5957\u5217\u8868\u5408\u5e76\u6210\u4e00\u4e2a\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<h2><strong>\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\u521b\u5efa\u5305\u542b\u5143\u7ec4\u7684\u5217\u8868\u6709\u591a\u79cd\u65b9\u6cd5\u3002<strong>\u76f4\u63a5\u521b\u5efa\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528<code>zip<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>map<\/code>\u51fd\u6570\u3001\u4f7f\u7528\u5d4c\u5957\u5217\u8868\u3001\u4f7f\u7528<code>itertools.product<\/code>\u3001\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u3001\u4f7f\u7528<code>namedtuple<\/code>\u3001\u4f7f\u7528<code>dataclass<\/code>\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u3001\u4f7f\u7528<code>itertools.chain<\/code>\u3001\u4f7f\u7528<code>chain.from_iterable<\/code>\u7b49\u65b9\u6cd5<\/strong>\u90fd\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u5217\u8868\u4e2d\u521b\u5efa\u5143\u7ec4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5c06\u591a\u4e2a\u503c\u7528\u9017\u53f7\u5206\u9694\u5e76\u653e\u5728\u5706\u62ec\u53f7\u5185\u6765\u521b\u5efa\u5143\u7ec4\u3002\u8981\u5c06\u5143\u7ec4\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\uff0c\u53ea\u9700\u5c06\u5176\u4f5c\u4e3a\u5143\u7d20\u6dfb\u52a0\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<code>my_list = [(1, 2), (3, 4)]<\/code>\uff0c\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u5143\u7ec4\u7684\u5217\u8868\u3002<\/p>\n<p><strong>\u521b\u5efa\u7a7a\u5143\u7ec4\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u6709\u4ec0\u4e48\u65b9\u6cd5\uff1f<\/strong><br \/>\u8981\u521b\u5efa\u4e00\u4e2a\u7a7a\u5143\u7ec4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e00\u5bf9\u5706\u62ec\u53f7<code>()<\/code>\u3002\u53ef\u4ee5\u901a\u8fc7\u5217\u8868\u7684<code>append<\/code>\u65b9\u6cd5\u5c06\u5176\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\uff0c\u5982<code>my_list.append(())<\/code>\u3002\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u7075\u6d3b\u5730\u5728\u5217\u8868\u4e2d\u6dfb\u52a0\u591a\u4e2a\u7a7a\u5143\u7ec4\uff0c\u4e4b\u540e\u518d\u6839\u636e\u9700\u8981\u586b\u5145\u5b83\u4eec\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5217\u8868\u4e2d\u5b58\u50a8\u591a\u4e2a\u5143\u7ec4\u5e76\u8fdb\u884c\u64cd\u4f5c\uff1f<\/strong><br \/>\u8981\u5728\u5217\u8868\u4e2d\u5b58\u50a8\u591a\u4e2a\u5143\u7ec4\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06\u5b83\u4eec\u4f5c\u4e3a\u5217\u8868\u7684\u5143\u7d20\u3002\u4f8b\u5982\uff0c<code>my_list = [(1, 2), (3, 4), (5, 6)]<\/code>\u3002\u60a8\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5143\u7ec4\uff0c\u4f7f\u7528\u5faa\u73af\u904d\u5386\u5217\u8868\uff0c\u6216\u8005\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u6765\u8fdb\u884c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u8fc7\u6ee4\u6216\u8f6c\u6362\u5143\u7ec4\u4e2d\u7684\u503c\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5c06\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u5143\u7ec4\u5b58\u50a8\u5728\u540c\u4e00\u4e2a\u5217\u8868\u4e2d\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u5141\u8bb8\u60a8\u5728\u5217\u8868\u4e2d\u5b58\u50a8\u4e0d\u540c\u7c7b\u578b\u7684\u5143\u7ec4\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6574\u6570\u3001\u5b57\u7b26\u4e32\u548c\u6d6e\u70b9\u6570\u7684\u5143\u7ec4\u5217\u8868\uff0c\u5982<code>my_list = [(1, &#39;a&#39;), (2.5, &#39;b&#39;)]<\/code>\u3002\u8fd9\u4f7f\u5f97\u5728\u540c\u4e00\u4e2a\u5217\u8868\u4e2d\u5904\u7406\u591a\u79cd\u6570\u636e\u7c7b\u578b\u53d8\u5f97\u975e\u5e38\u7075\u6d3b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u5217\u8868\u4e2d\u521b\u5efatuple\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u76f4\u63a5\u521b\u5efa\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u4f7f\u7528zip\u51fd\u6570\u7b49\u3002\u76f4\u63a5\u521b\u5efa\u662f\u6700\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1170244,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170234"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1170234"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170234\/revisions"}],"predecessor-version":[{"id":1170246,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170234\/revisions\/1170246"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1170244"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1170234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1170234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1170234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}