{"id":932898,"date":"2024-12-26T18:06:40","date_gmt":"2024-12-26T10:06:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/932898.html"},"modified":"2024-12-26T18:06:42","modified_gmt":"2024-12-26T10:06:42","slug":"python%e5%a6%82%e4%bd%95%e8%bf%ad%e4%bb%a3%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/932898.html","title":{"rendered":"python\u5982\u4f55\u8fed\u4ee3\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070541\/de7f0221-f079-4440-a031-ea2490e728a0.webp\" alt=\"python\u5982\u4f55\u8fed\u4ee3\u503c\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u8fed\u4ee3\u503c\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u751f\u6210\u5668\u3001\u8fed\u4ee3\u5668\u7b49\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff0cfor\u5faa\u73af\u662f\u6700\u5e38\u7528\u4e14\u7b80\u6d01\u7684\u65b9\u5f0f\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u4ee5\u5176\u7b80\u6d01\u6027\u548c\u7075\u6d3b\u6027\u8457\u79f0\u3002\u5728Python\u4e2d\uff0c\u8fed\u4ee3\u662f\u4e00\u4e2a\u6838\u5fc3\u6982\u5ff5\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u6570\u636e\u7ed3\u6784\u548c\u7b97\u6cd5\u4e2d\u3002<strong>\u4f7f\u7528for\u5faa\u73af\u53ef\u4ee5\u8f7b\u677e\u5730\u904d\u5386\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u5178\u3001\u96c6\u5408\u548c\u5b57\u7b26\u4e32\u7b49\u6570\u636e\u7ed3\u6784\u3002<\/strong>\u5728\u8fd9\u4e00\u8fc7\u7a0b\u4e2d\uff0cPython\u5185\u7f6e\u7684<code>iter()<\/code>\u548c<code>next()<\/code>\u51fd\u6570\u4e5f\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7075\u6d3b\u7684\u8fed\u4ee3\u65b9\u5f0f\uff0c\u7279\u522b\u9002\u7528\u4e8e\u81ea\u5b9a\u4e49\u590d\u6742\u8fed\u4ee3\u884c\u4e3a\u7684\u573a\u666f\u3002\u901a\u8fc7\u4e86\u89e3\u548c\u638c\u63e1\u8fd9\u4e9b\u8fed\u4ee3\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u7f16\u7a0b\u7684\u6548\u7387\u548c\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8Python\u4e2d\u51e0\u79cd\u5e38\u89c1\u7684\u8fed\u4ee3\u65b9\u5f0f\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001FOR\u5faa\u73af<\/h3>\n<\/p>\n<p><p>For\u5faa\u73af\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u8fed\u4ee3\u5de5\u5177\u4e4b\u4e00\uff0c\u7b80\u6d01\u4e14\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h4>1. \u5217\u8868\u7684\u8fed\u4ee3<\/h4>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5217\u8868\u662f\u4e00\u4e2a\u6709\u5e8f\u7684\u96c6\u5408\uff0c\u9002\u5408\u4f7f\u7528for\u5faa\u73af\u8fdb\u884c\u8fed\u4ee3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fruits = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>for fruit in fruits:<\/p>\n<p>    print(fruit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cfor\u5faa\u73af\u4f9d\u6b21\u8fed\u4ee3\u5217\u8868<code>fruits<\/code>\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b57\u5178\u7684\u8fed\u4ee3<\/h4>\n<\/p>\n<p><p>\u5b57\u5178\u662fPython\u4e2d\u975e\u5e38\u91cd\u8981\u7684\u6570\u636e\u7ed3\u6784\u4e4b\u4e00\uff0c\u4f7f\u7528for\u5faa\u73af\u53ef\u4ee5\u8fed\u4ee3\u952e\u3001\u503c\u6216\u8005\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">person = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 25, &#39;city&#39;: &#39;New York&#39;}<\/p>\n<p>for key in person:<\/p>\n<p>    print(key, person[key])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cfor\u5faa\u73af\u8fed\u4ee3\u5b57\u5178<code>person<\/code>\u7684\u952e\uff0c\u5e76\u901a\u8fc7\u952e\u8bbf\u95ee\u76f8\u5e94\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h4>3. \u4f7f\u7528enumerate()\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>enumerate()<\/code>\u51fd\u6570\u5728\u8fed\u4ee3\u8fc7\u7a0b\u4e2d\u63d0\u4f9b\u7d22\u5f15\uff0c\u7279\u522b\u9002\u5408\u9700\u8981\u540c\u65f6\u8bbf\u95ee\u7d22\u5f15\u548c\u503c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">names = [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;]<\/p>\n<p>for index, name in enumerate(names):<\/p>\n<p>    print(index, name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>enumerate()<\/code>\uff0c\u53ef\u4ee5\u5728\u8fed\u4ee3\u5217\u8868\u65f6\u540c\u65f6\u83b7\u53d6\u7d22\u5f15\u548c\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001WHILE\u5faa\u73af<\/h3>\n<\/p>\n<p><p>While\u5faa\u73af\u63d0\u4f9b\u4e86\u4e00\u79cd\u6839\u636e\u6761\u4ef6\u8fed\u4ee3\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u4e0d\u786e\u5b9a\u5faa\u73af\u6b21\u6570\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528while\u5faa\u73af\u53ef\u4ee5\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u6267\u884c\u8fed\u4ee3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">count = 0<\/p>\n<p>while count &lt; 5:<\/p>\n<p>    print(&quot;Count is:&quot;, count)<\/p>\n<p>    count += 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cwhile\u5faa\u73af\u5728<code>count<\/code>\u5c0f\u4e8e5\u65f6\u6301\u7eed\u8fed\u4ee3\uff0c\u5e76\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u589e\u52a0<code>count<\/code>\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h4>2. \u65e0\u9650\u5faa\u73af<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u8bbe\u7f6e\u4e00\u4e2a\u6c38\u8fdc\u4e3a\u771f\u7684\u6761\u4ef6\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u901a\u5e38\u4e0e\u6761\u4ef6\u5224\u65ad\u548c<code>break<\/code>\u8bed\u53e5\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">while True:<\/p>\n<p>    response = input(&quot;Type &#39;exit&#39; to quit: &quot;)<\/p>\n<p>    if response == &#39;exit&#39;:<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>break<\/code>\u8bed\u53e5\u7ec8\u6b62\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5217\u8868\u63a8\u5bfc\u5f0f<\/h3>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u7b80\u6d01\u7684\u521b\u5efa\u5217\u8868\u7684\u65b9\u6cd5\uff0c\u5728\u904d\u5386\u65f6\u540c\u65f6\u8fdb\u884c\u5143\u7d20\u7684\u8fc7\u6ee4\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u548c\u8fc7\u6ee4\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u548c\u8fc7\u6ee4\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares = [x2 for x in range(10)]<\/p>\n<p>even_squares = [x for x in squares if x % 2 == 0]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u521b\u5efa\u4e00\u4e2a\u5e73\u65b9\u6570\u5217\u8868\uff0c\u5e76\u4ece\u4e2d\u7b5b\u9009\u51fa\u5076\u6570\u3002<\/p>\n<\/p>\n<p><h4>2. \u5d4c\u5957\u5217\u8868\u63a8\u5bfc\u5f0f<\/h4>\n<\/p>\n<p><p>\u5217\u8868\u63a8\u5bfc\u5f0f\u53ef\u4ee5\u5d4c\u5957\u4f7f\u7528\uff0c\u9002\u5408\u5904\u7406\u591a\u7ef4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p>flat_list = [num for row in matrix for num in row]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u4e00\u4e2a\u4e8c\u7ef4\u5217\u8868\u5c55\u5e73\u6210\u4e00\u7ef4\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u5668\u63d0\u4f9b\u4e86\u4e00\u79cd\u60f0\u6027\u8fed\u4ee3\u7684\u65b9\u5f0f\uff0c\u5728\u9700\u8981\u65f6\u751f\u6210\u5143\u7d20\uff0c\u9002\u5408\u5904\u7406\u5927\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u51fd\u6570\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\u8fd4\u56de\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def count_up_to(n):<\/p>\n<p>    count = 1<\/p>\n<p>    while count &lt;= n:<\/p>\n<p>        yield count<\/p>\n<p>        count += 1<\/p>\n<p>counter = count_up_to(5)<\/p>\n<p>for number in counter:<\/p>\n<p>    print(number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u751f\u6210\u5668\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u4ece1\u5230n\u7684\u6570\u5b57\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><h4>2. \u751f\u6210\u5668\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u4e0e\u5217\u8868\u63a8\u5bfc\u5f0f\u7c7b\u4f3c\uff0c\u4f46\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">squares_gen = (x2 for x in range(10))<\/p>\n<p>for square in squares_gen:<\/p>\n<p>    print(square)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u521b\u5efa\u4e86\u4e00\u4e2a\u751f\u6210\u5668\u5bf9\u8c61\uff0c\u7528\u4e8e\u8fed\u4ee3\u5e73\u65b9\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u8fed\u4ee3\u5668<\/h3>\n<\/p>\n<p><p>\u8fed\u4ee3\u5668\u662fPython\u4e2d\u5b9e\u73b0\u81ea\u5b9a\u4e49\u8fed\u4ee3\u884c\u4e3a\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u81ea\u5b9a\u4e49\u8fed\u4ee3\u5668<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u5305\u542b<code>__iter__()<\/code>\u548c<code>__next__()<\/code>\u65b9\u6cd5\u7684\u7c7b\u6765\u521b\u5efa\u81ea\u5b9a\u4e49\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class MyRange:<\/p>\n<p>    def __init__(self, start, end):<\/p>\n<p>        self.current = start<\/p>\n<p>        self.end = end<\/p>\n<p>    def __iter__(self):<\/p>\n<p>        return self<\/p>\n<p>    def __next__(self):<\/p>\n<p>        if self.current &gt;= self.end:<\/p>\n<p>            r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se StopIteration<\/p>\n<p>        current = self.current<\/p>\n<p>        self.current += 1<\/p>\n<p>        return current<\/p>\n<p>my_range = MyRange(1, 5)<\/p>\n<p>for num in my_range:<\/p>\n<p>    print(num)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u7684\u81ea\u5b9a\u4e49\u8fed\u4ee3\u5668\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5185\u7f6e\u51fd\u6570iter()\u548cnext()<\/h4>\n<\/p>\n<p><p><code>iter()<\/code>\u548c<code>next()<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u53ef\u8fed\u4ee3\u5bf9\u8c61\u8f6c\u6362\u4e3a\u8fed\u4ee3\u5668\uff0c\u5e76\u624b\u52a8\u63a7\u5236\u8fed\u4ee3\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">numbers = [1, 2, 3, 4, 5]<\/p>\n<p>iterator = iter(numbers)<\/p>\n<p>while True:<\/p>\n<p>    try:<\/p>\n<p>        number = next(iterator)<\/p>\n<p>        print(number)<\/p>\n<p>    except StopIteration:<\/p>\n<p>        break<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>iter()<\/code>\u548c<code>next()<\/code>\u51fd\u6570\u624b\u52a8\u8fed\u4ee3\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u8fed\u4ee3\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u9ad8\u6548\u5730\u5904\u7406\u5404\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u6027\u80fd\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u503c\u7684\u8fed\u4ee3\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u8fed\u4ee3\u503c\u901a\u5e38\u662f\u901a\u8fc7\u5faa\u73af\u5b9e\u73b0\u7684\u3002\u6700\u5e38\u7528\u7684\u8fed\u4ee3\u7ed3\u6784\u662f<code>for<\/code>\u5faa\u73af\u548c<code>while<\/code>\u5faa\u73af\u3002<code>for<\/code>\u5faa\u73af\u53ef\u4ee5\u76f4\u63a5\u904d\u5386\u5e8f\u5217\uff08\u5982\u5217\u8868\u3001\u5143\u7ec4\u3001\u5b57\u7b26\u4e32\u7b49\uff09\uff0c\u800c<code>while<\/code>\u5faa\u73af\u5219\u662f\u5728\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u65f6\u6301\u7eed\u6267\u884c\u3002\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>for item in iterable:<\/code>\uff0c\u5176\u4e2d<code>iterable<\/code>\u53ef\u4ee5\u662f\u4efb\u4f55\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u786e\u4fdd\u5728\u8fed\u4ee3\u65f6\u5408\u7406\u5730\u5904\u7406\u8fb9\u754c\u6761\u4ef6\uff0c\u4ee5\u907f\u514d\u65e0\u9650\u5faa\u73af\u6216\u9519\u8bef\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528\u751f\u6210\u5668\u6765\u8fed\u4ee3\u503c\uff1f<\/strong><br \/>\u751f\u6210\u5668\u662fPython\u4e2d\u4e00\u79cd\u5f3a\u5927\u7684\u8fed\u4ee3\u5de5\u5177\uff0c\u4f7f\u7528<code>yield<\/code>\u5173\u952e\u5b57\u5b9a\u4e49\u3002\u4e0e\u5e38\u89c4\u51fd\u6570\u4e0d\u540c\uff0c\u751f\u6210\u5668\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\u8fd4\u56de\u4e00\u4e2a\u503c\u5e76\u4fdd\u7559\u5176\u72b6\u6001\u3002\u4f7f\u7528\u751f\u6210\u5668\u7684\u597d\u5904\u5728\u4e8e\u5b83\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u56e0\u4e3a\u5b83\u4eec\u6309\u9700\u751f\u6210\u503c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u52a0\u8f7d\u6240\u6709\u503c\u3002\u8981\u4f7f\u7528\u751f\u6210\u5668\uff0c\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u5e76\u5728\u5176\u4e2d\u4f7f\u7528<code>yield<\/code>\uff0c\u7136\u540e\u901a\u8fc7<code>next()<\/code>\u51fd\u6570\u6216<code>for<\/code>\u5faa\u73af\u6765\u8fed\u4ee3\u5176\u4ea7\u751f\u7684\u503c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u5b57\u5178\u7684\u8fed\u4ee3\uff1f<\/strong><br \/>\u5b57\u5178\u662f\u4e00\u79cd\u952e\u503c\u5bf9\u96c6\u5408\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u8fed\u4ee3\u3002\u4f7f\u7528<code>for<\/code>\u5faa\u73af\u53ef\u4ee5\u76f4\u63a5\u8fed\u4ee3\u5b57\u5178\u7684\u952e\uff0c\u4f7f\u7528<code>dict.items()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u540c\u65f6\u83b7\u53d6\u952e\u548c\u503c\u3002\u4f8b\u5982\uff0c<code>for key, value in my_dict.items():<\/code>\u5c06\u904d\u5386\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\u503c\u5bf9\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5b57\u5178\u7684\u8fed\u4ee3\u987a\u5e8f\u662f\u63d2\u5165\u987a\u5e8f\uff0c\u8fd9\u5728Python 3.7\u53ca\u4ee5\u540e\u7684\u7248\u672c\u4e2d\u662f\u4fdd\u8bc1\u7684\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u548c\u5904\u7406\u5b57\u5178\u4e2d\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u8fed\u4ee3\u503c\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528for\u5faa\u73af\u3001while\u5faa\u73af\u3001\u5217\u8868\u63a8\u5bfc\u5f0f\u3001\u751f\u6210\u5668\u3001\u8fed\u4ee3\u5668\u7b49\u3002\u5bf9\u4e8e\u5927 [&hellip;]","protected":false},"author":3,"featured_media":932902,"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\/932898"}],"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=932898"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932898\/revisions"}],"predecessor-version":[{"id":932905,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/932898\/revisions\/932905"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/932902"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=932898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=932898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=932898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}