{"id":1101635,"date":"2025-01-08T15:51:41","date_gmt":"2025-01-08T07:51:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1101635.html"},"modified":"2025-01-08T15:51:43","modified_gmt":"2025-01-08T07:51:43","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e6%95%b0%e6%8d%ae%e5%88%86%e4%b8%a4%e6%ac%a1%e8%bf%94%e5%9b%9e-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1101635.html","title":{"rendered":"python\u5982\u4f55\u628a\u6570\u636e\u5206\u4e24\u6b21\u8fd4\u56de"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064322\/62115760-7528-4049-8a66-72aa4df9c1ac.webp\" alt=\"python\u5982\u4f55\u628a\u6570\u636e\u5206\u4e24\u6b21\u8fd4\u56de\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u636e\u5206\u4e24\u6b21\u8fd4\u56de<\/strong>\uff0c<strong>\u4f7f\u7528yield\u751f\u6210\u5668\u3001\u5206\u6b65\u8fd4\u56de\u3001\u4f7f\u7528\u4e24\u4e2a\u8fd4\u56de\u503c<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528yield\u751f\u6210\u5668\u662f\u4e00\u79cd\u975e\u5e38\u9ad8\u6548\u4e14\u7b26\u5408Python\u60ef\u7528\u6cd5\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u6211\u4eec\u6765\u8be6\u7ec6\u63cf\u8ff0\u4f7f\u7528yield\u751f\u6210\u5668\u7684\u65b9\u6cd5\u3002yield\u751f\u6210\u5668\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u4f7f\u7528yield\u8bed\u53e5\u800c\u4e0d\u662freturn\u8bed\u53e5\u8fd4\u56de\u503c\u3002\u6bcf\u6b21\u8c03\u7528\u751f\u6210\u5668\u7684__next__()\u65b9\u6cd5\u65f6\uff0c\u5b83\u4f1a\u4ece\u4e0a\u4e00\u6b21\u505c\u6b62\u7684\u5730\u65b9\u7ee7\u7eed\u6267\u884c\uff0c\u76f4\u5230\u9047\u5230\u4e0b\u4e00\u4e2ayield\u8bed\u53e5\u3002\u8fd9\u4f7f\u5f97\u751f\u6210\u5668\u975e\u5e38\u9002\u5408\u7528\u4e8e\u9700\u8981\u9010\u6b65\u8fd4\u56de\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528yield\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u662fPython\u4e2d\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u4e00\u6b21\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u8fd4\u56de\u6240\u6709\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528yield\u8bed\u53e5\u5c06\u6570\u636e\u5206\u6b21\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def data_generator(data):<\/p>\n<p>    half = len(data) \/\/ 2<\/p>\n<p>    yield data[:half]<\/p>\n<p>    yield data[half:]<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>data = [1, 2, 3, 4, 5, 6]<\/p>\n<p>gen = data_generator(data)<\/p>\n<p>first_half = next(gen)<\/p>\n<p>second_half = next(gen)<\/p>\n<p>print(first_half)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p>print(second_half)  # \u8f93\u51fa: [4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u751f\u6210\u5668\u51fd\u6570<code>data_generator<\/code>\u5c06\u6570\u636e\u5206\u6210\u4e24\u90e8\u5206\uff0c\u6bcf\u6b21\u8c03\u7528next\u65b9\u6cd5\u65f6\u8fd4\u56de\u4e00\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5206\u6b65\u8fd4\u56de<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5206\u6b65\u8fd4\u56de\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u6570\u636e\u7684\u5206\u6b21\u8fd4\u56de\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u9700\u8981\u660e\u786e\u63a7\u5236\u6570\u636e\u8fd4\u56de\u987a\u5e8f\u7684\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def split_data(data):<\/p>\n<p>    half = len(data) \/\/ 2<\/p>\n<p>    return data[:half], data[half:]<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>data = [1, 2, 3, 4, 5, 6]<\/p>\n<p>first_half, second_half = split_data(data)<\/p>\n<p>print(first_half)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p>print(second_half)  # \u8f93\u51fa: [4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>split_data<\/code>\u51fd\u6570\u4e00\u6b21\u8fd4\u56de\u4e24\u4e2a\u90e8\u5206\u7684\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u5206\u522b\u83b7\u53d6\u8fd9\u4e24\u4e2a\u90e8\u5206\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u4e24\u4e2a\u8fd4\u56de\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u51fd\u6570\u8fd4\u56de\u4e24\u4e2a\u503c\u6765\u5b9e\u73b0\u6570\u636e\u7684\u5206\u6b21\u8fd4\u56de\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6570\u636e\u91cf\u8f83\u5c0f\u6216\u8005\u660e\u786e\u77e5\u9053\u9700\u8981\u8fd4\u56de\u7684\u90e8\u5206\u6570\u636e\u91cf\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def split_data(data):<\/p>\n<p>    half = len(data) \/\/ 2<\/p>\n<p>    return data[:half], data[half:]<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>data = [1, 2, 3, 4, 5, 6]<\/p>\n<p>first_half, second_half = split_data(data)<\/p>\n<p>print(first_half)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p>print(second_half)  # \u8f93\u51fa: [4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0csplit_data\u51fd\u6570\u4e00\u6b21\u8fd4\u56de\u4e24\u4e2a\u90e8\u5206\u7684\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u5206\u522b\u83b7\u53d6\u8fd9\u4e24\u4e2a\u90e8\u5206\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u8fed\u4ee3\u5668<\/h3>\n<\/p>\n<p><p>\u8fed\u4ee3\u5668\u662f\u4e00\u79cd\u66f4\u901a\u7528\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u9700\u8981\u6309\u9700\u83b7\u53d6\u6570\u636e\u7684\u573a\u666f\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u6570\u636e\u5206\u6210\u591a\u4e2a\u90e8\u5206\uff0c\u6bcf\u6b21\u8fed\u4ee3\u8fd4\u56de\u4e00\u4e2a\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class Dat<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>terator:<\/p>\n<p>    def __init__(self, data):<\/p>\n<p>        self.data = data<\/p>\n<p>        self.index = 0<\/p>\n<p>        self.step = len(data) \/\/ 2<\/p>\n<p>    def __iter__(self):<\/p>\n<p>        return self<\/p>\n<p>    def __next__(self):<\/p>\n<p>        if self.index &gt;= len(self.data):<\/p>\n<p>            raise StopIteration<\/p>\n<p>        result = self.data[self.index:self.index + self.step]<\/p>\n<p>        self.index += self.step<\/p>\n<p>        return result<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>data = [1, 2, 3, 4, 5, 6]<\/p>\n<p>iterator = DataIterator(data)<\/p>\n<p>for part in iterator:<\/p>\n<p>    print(part)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cDataIterator\u7c7b\u5b9e\u73b0\u4e86\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u6bcf\u6b21\u8fed\u4ee3\u8fd4\u56de\u6570\u636e\u7684\u4e00\u90e8\u5206\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u6765\u6309\u9700\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u961f\u5217<\/h3>\n<\/p>\n<p><p>\u961f\u5217\u662f\u4e00\u79cd\u5148\u8fdb\u5148\u51fa\uff08FIFO\uff09\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u961f\u5217\u6765\u5b58\u50a8\u6570\u636e\uff0c\u5e76\u6309\u9700\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from queue import Queue<\/p>\n<p>def data_queue(data):<\/p>\n<p>    q = Queue()<\/p>\n<p>    half = len(data) \/\/ 2<\/p>\n<p>    q.put(data[:half])<\/p>\n<p>    q.put(data[half:])<\/p>\n<p>    return q<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>data = [1, 2, 3, 4, 5, 6]<\/p>\n<p>q = data_queue(data)<\/p>\n<p>first_half = q.get()<\/p>\n<p>second_half = q.get()<\/p>\n<p>print(first_half)  # \u8f93\u51fa: [1, 2, 3]<\/p>\n<p>print(second_half)  # \u8f93\u51fa: [4, 5, 6]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cdata_queue\u51fd\u6570\u5c06\u6570\u636e\u5206\u6210\u4e24\u90e8\u5206\uff0c\u5e76\u5b58\u50a8\u5728\u961f\u5217\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u961f\u5217\u7684get\u65b9\u6cd5\u6309\u9700\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528\u5f02\u6b65\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5f02\u6b65\u83b7\u53d6\u6570\u636e\u3002\u5f02\u6b65\u751f\u6210\u5668\u662f\u4e00\u79cd\u975e\u5e38\u9002\u5408\u8fd9\u79cd\u573a\u666f\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import asyncio<\/p>\n<p>async def data_generator(data):<\/p>\n<p>    half = len(data) \/\/ 2<\/p>\n<p>    await asyncio.sleep(1)  # \u6a21\u62df\u5f02\u6b65\u64cd\u4f5c<\/p>\n<p>    yield data[:half]<\/p>\n<p>    await asyncio.sleep(1)  # \u6a21\u62df\u5f02\u6b65\u64cd\u4f5c<\/p>\n<p>    yield data[half:]<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>async def main():<\/p>\n<p>    data = [1, 2, 3, 4, 5, 6]<\/p>\n<p>    async for part in data_generator(data):<\/p>\n<p>        print(part)<\/p>\n<p>asyncio.run(main())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cdata_generator\u51fd\u6570\u662f\u4e00\u4e2a\u5f02\u6b65\u751f\u6210\u5668\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528async for\u5faa\u73af\u6309\u9700\u5f02\u6b65\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c<strong>\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u636e\u5206\u4e24\u6b21\u8fd4\u56de\uff0c\u5305\u62ec\u4f7f\u7528yield\u751f\u6210\u5668\u3001\u5206\u6b65\u8fd4\u56de\u3001\u4f7f\u7528\u4e24\u4e2a\u8fd4\u56de\u503c\u3001\u4f7f\u7528\u8fed\u4ee3\u5668\u3001\u4f7f\u7528\u961f\u5217\u548c\u4f7f\u7528\u5f02\u6b65\u751f\u6210\u5668<\/strong>\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5206\u6279\u8fd4\u56de\u6570\u636e\u7684\u529f\u80fd\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u751f\u6210\u5668\u6765\u5b9e\u73b0\u6570\u636e\u7684\u5206\u6279\u8fd4\u56de\u3002\u751f\u6210\u5668\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u8ba9\u51fd\u6570\u5728\u6267\u884c\u65f6\u6682\u505c\u5e76\u4fdd\u5b58\u72b6\u6001\uff0c\u4ece\u800c\u5728\u4e0b\u4e00\u6b21\u8c03\u7528\u65f6\u7ee7\u7eed\u6267\u884c\u3002\u8fd9\u6837\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u907f\u514d\u4e00\u6b21\u6027\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528\u5206\u6279\u8fd4\u56de\u65f6\uff0c\u5982\u4f55\u63a7\u5236\u6bcf\u6279\u8fd4\u56de\u7684\u6570\u636e\u91cf\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5728\u751f\u6210\u5668\u4e2d\u4f7f\u7528\u53c2\u6570\u6765\u6307\u5b9a\u6bcf\u6279\u8fd4\u56de\u7684\u6570\u636e\u91cf\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u63a5\u6536\u6570\u636e\u548c\u6bcf\u6279\u7684\u5927\u5c0f\u4f5c\u4e3a\u53c2\u6570\uff0c\u901a\u8fc7\u5faa\u73af\u548c<code>yield<\/code>\u8bed\u53e5\u5206\u6279\u8fd4\u56de\u6570\u636e\u3002\u8fd9\u6837\u53ef\u4ee5\u7075\u6d3b\u5730\u8c03\u6574\u6bcf\u6279\u7684\u5927\u5c0f\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5e93\u6765\u7b80\u5316\u6570\u636e\u7684\u5206\u6279\u8fd4\u56de\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>itertools<\/code>\u6a21\u5757\u4e2d\u7684<code>islice<\/code>\u51fd\u6570\uff0c\u6216\u8005<code>pandas<\/code>\u5e93\u4e2d\u7684<code>DataFrame<\/code>\u5207\u7247\u529f\u80fd\u6765\u7b80\u5316\u5206\u6279\u8fd4\u56de\u7684\u6570\u636e\u5904\u7406\u3002<code>itertools.islice<\/code>\u5141\u8bb8\u4f60\u4ece\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u5207\u7247\uff0c\u800c<code>pandas<\/code>\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u7ec4\u548c\u6279\u91cf\u5904\u7406\uff0c\u9002\u5408\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u636e\u5206\u4e24\u6b21\u8fd4\u56de\uff0c\u4f7f\u7528yield\u751f\u6210\u5668\u3001\u5206\u6b65\u8fd4\u56de\u3001\u4f7f\u7528\u4e24\u4e2a\u8fd4\u56de\u503c\u3002\u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1101644,"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\/1101635"}],"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=1101635"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101635\/revisions"}],"predecessor-version":[{"id":1101645,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101635\/revisions\/1101645"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1101644"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1101635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1101635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1101635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}