{"id":650,"date":"2026-03-07T17:32:03","date_gmt":"2026-03-07T09:32:03","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=650"},"modified":"2026-07-13T17:58:16","modified_gmt":"2026-07-13T09:58:16","slug":"python-%e5%ae%b9%e5%99%a8%e5%b8%b8%e7%94%a8%ef%bc%8c%e6%89%81%e5%b9%b3%e5%8c%96%e3%80%81%e8%bd%ac%e9%93%be%e8%a1%a8%e3%80%81%e5%ad%97%e5%85%b8%e5%90%88%e5%b9%b6","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/03\/07\/python-%e5%ae%b9%e5%99%a8%e5%b8%b8%e7%94%a8%ef%bc%8c%e6%89%81%e5%b9%b3%e5%8c%96%e3%80%81%e8%bd%ac%e9%93%be%e8%a1%a8%e3%80%81%e5%ad%97%e5%85%b8%e5%90%88%e5%b9%b6\/","title":{"rendered":"python \u5bb9\u5668\u5e38\u7528\uff0c\u6241\u5e73\u5316\u3001\u8f6c\u94fe\u8868\u3001\u5b57\u5178\u5408\u5e76"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>\u5217\u8868\u6241\u5e73\u5316<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9e\u73b0\u51fd\u6570 <code>flatten<\/code>\uff0c\u63a5\u6536\u4e00\u4e2a\u53ef\u80fd\u5305\u542b\u5d4c\u5957\u5217\u8868\u7684\u5217\u8868\uff08\u5982 <code>[1, [2, 3], [[4], 5]]<\/code>\uff09\uff0c\u8fd4\u56de\u4e00\u4e2a\u5c06\u6240\u6709\u5143\u7d20\u5c55\u5f00\u540e\u7684\u4e00\u7ef4\u5217\u8868\uff08\u5982 <code>[1, 2, 3, 4, 5]<\/code>\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nested1 = &#91;1, &#91;2, 3], &#91;&#91;4], 5]]<br>print(flatten(nested1)) &nbsp;# &#91;1, 2, 3, 4, 5]<br>\u200b<br>nested2 = &#91;&#91;&#91;1]], 2, &#91;3, &#91;4, &#91;5]]]]<br>print(flatten(nested2)) &nbsp;# &#91;1, 2, 3, 4, 5]<br>\u200b<br>print(flatten(&#91;])) &nbsp; &nbsp; &nbsp; # &#91;]<br>print(flatten(&#91;1, 2, 3])) &nbsp;# &#91;1, 2, 3]<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>def flatten(nested):<br> &nbsp;  new_list = &#91;];<br> &nbsp;  for item in nested:<br> &nbsp; &nbsp; &nbsp;  if(type(item) == list):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  new_list.extend(flatten(item))<br> &nbsp; &nbsp; &nbsp;  else:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  new_list.append(item)<br> &nbsp;  return new_list<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4efb\u52a1 2\uff1a\u5217\u8868\/\u5143\u7ec4\u8f6c\u94fe\u8868<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9e\u73b0\u51fd\u6570 <code>to_linked_list<\/code>\uff0c\u63a5\u6536\u4e00\u4e2a\u5217\u8868\u6216\u5143\u7ec4\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u4e00\u4e2a\u94fe\u8868\u7ed3\u6784\u5e76\u8fd4\u56de\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>linked1 = to_linked_list((1, 2, 3))<br>print(linked1)<br># {'value': 1, 'next': {'value': 2, 'next': {'value': 3, 'next': None}}}<br>\u200b<br>linked2 = to_linked_list(&#91;10, 20])<br>print(linked2)<br># {'value': 10, 'next': {'value': 20, 'next': None}}<br>\u200b<br>print(to_linked_list(&#91;])) &nbsp;# None<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>def to_linked_list(data):<br> &nbsp;  if not data:<br> &nbsp; &nbsp; &nbsp;  return None<br> &nbsp;  head = {'value': data&#91;0], 'next': None}<br> &nbsp;  current = head<br> &nbsp;  for item in data&#91;1:]:<br> &nbsp; &nbsp; &nbsp;  current&#91;'next'] = {'value': item, 'next': None}<br> &nbsp; &nbsp; &nbsp;  current = current&#91;'next']<br> &nbsp;  return head &nbsp;<br>\u200b<br>linked1 = to_linked_list((1, 2, 3))<br>print(linked1)<br># {'value': 1, 'next': {'value': 2, 'next': {'value': 3, 'next': None}}}<br>\u200b<br>linked2 = to_linked_list(&#91;10, 20])<br>print(linked2)<br># {'value': 10, 'next': {'value': 20, 'next': None}}<br>\u200b<br>print(to_linked_list(&#91;]))  # None<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4efb\u52a1 3\uff1a\u5b57\u5178\u5408\u5e76<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9e\u73b0\u51fd\u6570 <code>merge_dicts<\/code>\uff0c\u63a5\u6536\u4e0d\u5b9a\u6570\u91cf\u7684\u5b57\u5178\u53c2\u6570\uff0c\u5c06\u5b83\u4eec\u5408\u5e76\u4e3a\u4e00\u4e2a\u5927\u5b57\u5178\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5408\u5e76\u89c4\u5219\uff1a\u6c42\u5e76\u96c6\uff0c\u5982\u679c\u6709\u76f8\u540c\u7684\u952e\uff0c\u540e\u4f20\u5165\u7684\u5b57\u5178\u4e2d\u7684\u503c\u8986\u76d6\u5148\u4f20\u5165\u7684\u5b57\u5178\u4e2d\u7684\u503c\uff08\u6d45\u5408\u5e76\u5373\u53ef\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>d1 = {\"a\": 1, \"b\": &#91;1, 2]}<br>d2 = {\"b\": &#91;3], \"c\": \"hello\"}<br>d3 = {\"a\": 10, \"d\": True}<br>\u200b<br>merged = merge_dicts(d1, d2, d3)<br>print(merged)<br># {'a': 10, 'b': &#91;3], 'c': 'hello', 'd': True}<br>\u200b<br>print(merge_dicts(d1))<br># {'a': 1, 'b': &#91;1, 2]}<br>\u200b<br>print(merge_dicts())<br># {}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4efb\u52a1 3\uff1a\u5b57\u5178\u5408\u5e76<br>def merge_dicts(*dicts):<br> &nbsp;  result = {}<br> &nbsp;  for item in dicts:<br> &nbsp; &nbsp; &nbsp;  result.update(item)<br> &nbsp;  return result<br>\u200b<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5217\u8868\u6241\u5e73\u5316 \u5b9e\u73b0\u51fd\u6570 flatten\uff0c\u63a5\u6536\u4e00\u4e2a\u53ef\u80fd..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,14],"tags":[],"class_list":["post-650","post","type-post","status-publish","format-standard","hentry","category-python","category-back"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/python\/\" rel=\"category tag\">Python<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/\" rel=\"category tag\">\u540e\u7aef<\/a>","tag_info":"\u540e\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=650"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/650\/revisions"}],"predecessor-version":[{"id":651,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/650\/revisions\/651"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}