{"id":3938,"date":"2023-12-06T13:25:56","date_gmt":"2023-12-06T05:25:56","guid":{"rendered":"http:\/\/cnliutz.ipyingshe.net\/?p=3938"},"modified":"2023-12-06T14:35:22","modified_gmt":"2023-12-06T06:35:22","slug":"%e9%ab%98%e6%95%88%e7%9a%84%e5%8d%95%e8%a1%8cpython%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"http:\/\/cnliutz.wicp.vip\/?p=3938","title":{"rendered":"\u9ad8\u6548\u7684\u5355\u884cpython\u811a\u672c"},"content":{"rendered":"\n<p> https:\/\/m.toutiao.com\/is\/i8JUWns6\/<br> 10 \u4e2a\u9ad8\u6548\u7684\u5355\u884c Python \u811a\u672c &#8211; \u4eca\u65e5\u5934\u6761<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># -*- coding: utf-8 -*-\r\n\"\"\"\r\nCreated on Wed Dec  6 13:42:00 2023\r\n\r\n@author: czliu\r\n\"\"\"\r\n\r\n# 1. \u5e73\u65b9\u5217\u8868\u63a8\u5bfc\r\n# \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u6cd5\u8ba1\u7b97\u4ece 1 \u5230 10 \u7684\u6570\u5b57\u5e73\u65b9\r\n\r\nsquares = &#91;x**2 for x in range(1, 11)]\r\nprint(squares)\r\n# 2.\u6c42\u5076\u6570\r\n# \u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u4ece\u5217\u8868\u4e2d\u7b5b\u9009\u5076\u6570\u3002\u8fd8\u53ef\u4ee5\u51fa\u4e8e\u5176\u4ed6\u76ee\u7684\u4fee\u6539\u6b64\u4ee3\u7801\u3002\r\n\r\neven_numbers = &#91;x for x in range(1, 11) if x % 2 == 0]\r\nprint(even_numbers)\r\n# 3. \u4ea4\u6362\u53d8\u91cf\r\n# \u901a\u5e38\uff0c\u4ea4\u6362\u53d8\u91cf\u7684\u503c\u9700\u8981\u53e6\u4e00\u4e2a\u53d8\u91cf\u548c\u4e00\u5806\u884c\uff0c\u4f46\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u5728\u4e00\u884c\u4e2d\u6267\u884c\u6b64\u64cd\u4f5c\u3002\r\na=23\r\nb=25\r\na, b = b, a\r\nprint(a,b)\r\n# 4.\u53cd\u8f6c\u5b57\u7b26\u4e32\r\n# \u6709\u65f6\u9700\u8981\u53cd\u8f6c\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u53e6\u4e00\u4e2a\u53d8\u91cf\u548c\u4e00\u4e2afor\u5faa\u73af\u6765\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u8fd9\u6709\u70b9\u590d\u6742\u3002\u53ef\u4ee5\u7528\u5207\u7247\u6765\u4ee3\u66ff\r\n\r\nreversed_string = \"Hello, World!\"&#91;::-1]\r\nprint(reversed_string)\r\n# 5. \u8ba1\u7b97\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u5355\u8bcd\u51fa\u73b0\u6b21\u6570\r\n# \u6709\u4e00\u4e2a\u53e5\u5b50\u5217\u8868\uff0c\u9700\u8981\u68c0\u67e5\u6709\u591a\u5c11\u4e2a X \u5355\u8bcd\u3002\r\nsentences = &#91;\"]this\",\" world \",\"is\",\"not\",\" that\",\"world\",\"all world is \",\"word\",\" in \",\"sentence!\"]\r\n# for sentence in sentences:\r\n#     print(sentence)\r\nword_count = sum(1 for sentence in sentences if \"world\" in sentence)\r\nprint(word_count)\r\n# 6. \u7528\u4e8e\u6392\u5e8f\u7684 Lambda \u51fd\u6570\r\n# \u53ef\u4ee5\u4f7f\u7528 Lamba \u51fd\u6570\u6839\u636e\u7279\u5b9a\u952e\u5bf9\u5b57\u5178\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002sortin\r\n#worlds =  {'a': 1, 'b': 2, 'b': '3'}\r\ndict_list = {'a':28,'b':25,'c':76}\r\n# sorted_list = sorted(dict_list, key=lambda x: x&#91;'key'])\r\nsorted_list = sorted(dict_list.items(), key=lambda x: x&#91;1])\r\nprint(type(sorted_list))\r\nprint(sorted_list)\r\n# 7. \u5728\u5217\u8868\u4e2d\u67e5\u627e\u552f\u4e00\u5143\u7d20\r\n# \u53ef\u4ee5\u4f7f\u7528 set \u4ece\u5217\u8868\u4e2d\u83b7\u53d6\u552f\u4e00\u5143\u7d20\r\n\r\noriginal_list = &#91;12,34,12,45,'t','w','w']\r\nunique_elements = list(set(original_list))\r\n# 8. \u68c0\u67e5\u56de\u6587\r\n# \u56de\u6587\u662f\u4e00\u4e2a\u5355\u8bcd\u3001\u77ed\u8bed\u6216\u53e5\u5b50\uff0c\u5176\u5411\u540e\u8bfb\u6cd5\u4e0e\u5411\u524d\u8bfb\u6cd5\u76f8\u540c\u3002\u901a\u8fc7\u53cd\u8f6c\u6765\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u56de\u6587\u3002\r\nstring = 'abcdcba'\r\nis_palindrome = string == string&#91;::-1]\r\nprint(is_palindrome)\r\n\r\n# 9. \u5728 Sentece \u4e2d\u98a0\u5012\u5355\u8bcd\r\n# \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u548c\u529f\u80fd\u98a0\u5012\u53e5\u5b50\u4e2d\u5355\u8bcd\u7684\u987a\u5e8f\u3002join\r\nsentence = \"this world is not that world,all world is word in sentence!\"\r\nreversed_sentence = ' '.join(sentence.split()&#91;::-1])\r\nprint(reversed_sentence)\r\n# 10. \u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u56de\u6587\uff08\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff09\uff1a\r\n# \u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u56de\u6587\uff0c\u5ffd\u7565\u5927\u5c0f\u5199\uff0c\u901a\u8fc7\u53cd\u8f6c\u5b83\u548c\u51fd\u6570\u3002\r\nmy_string = 'AbcdcBa'\r\nis_palindrome = my_string.lower() == my_string&#91;::-1].lower()\r\nprint(is_palindrome)\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/m.toutiao.com\/is\/i8JUWns6\/ 10 \u4e2a\u9ad8 <span class=\"readmore\"><a href=\"http:\/\/cnliutz.wicp.vip\/?p=3938\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,10],"tags":[],"class_list":["post-3938","post","type-post","status-publish","format-standard","hentry","category-2","category-python"],"_links":{"self":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/3938","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3938"}],"version-history":[{"count":3,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/3938\/revisions"}],"predecessor-version":[{"id":3942,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/3938\/revisions\/3942"}],"wp:attachment":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3938"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}