{"id":3248,"date":"2023-04-05T16:47:21","date_gmt":"2023-04-05T08:47:21","guid":{"rendered":"http:\/\/192.168.31.200\/?p=3248"},"modified":"2023-04-05T16:47:23","modified_gmt":"2023-04-05T08:47:23","slug":"python%e4%bb%a3%e7%a0%81%e7%89%87%e6%ae%b5","status":"publish","type":"post","link":"http:\/\/xnw60rlg.ipyingshe.net:5347\/?p=3248","title":{"rendered":"Python\u4ee3\u7801\u7247\u6bb5"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#\u7ebf\u7a0b\n# \u7b2c\u4e00\u4e2a\u5de5\u4f5c\u51fd\u6570\ndef worker1():\n    for i in range(5):\n        print(\"Worker 1: \", i)\n        time.sleep(1)\n\n# \u7b2c\u4e8c\u4e2a\u5de5\u4f5c\u51fd\u6570\ndef worker2():\n    for i in range(15):\n        print(\"Worker 2: \", i)\n\n# \u521b\u5efa\u4e24\u4e2a\u7ebf\u7a0b\nt1 = threading.Thread(target=worker1)\nt2 = threading.Thread(target=worker2)\n\n# \u542f\u52a8\u7ebf\u7a0b\nt1.start()\nt2.start()\n\n# \u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f\nt1.join()\nt2.join()\n<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/h1>\n\n\n\n<p>\u8fd9\u4e2a\u65b9\u4fbf\u7684\u4ee3\u7801\u7247\u6bb5\u5c06\u8ba9\u4f60\u5728Python\u4e2d\u683c\u5f0f\u5316\u4f60\u7684\u5b57\u7b26\u4e32\u3002\u4e0b\u9762\u6211\u5206\u4eab\u4e86\u4e09\u79cd\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5b57\u7b26\u4e32\u683c\u5f0f\u5316\nname = \"Wick\"\nage = \"24\"\n\n# \u4f7f\u7528 f-\u5b57\u7b26\u4e32\nprint(f\"Hello {name}, you are {age} years old\")\n\n# \u4f7f\u7528 .format() \u65b9\u6cd5\nprint(\"Hello {}, you are {} years old\".format(name, age))\n\n# \u4f7f\u7528 %s \u683c\u5f0f\u7b26\nprint(\"Hello %s, you are %s years old\" % (name, age))\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5904\u7406CSV\n\nimport csv\n\n# \u8bfb\u53d6CSV\nwith open('mydata.csv', 'r') as file:\n    r = csv.reader(file)  # \u521b\u5efaCSV reader\u5bf9\u8c61\n    for row in r:         # \u904d\u5386\u6bcf\u4e00\u884c\n        print(row)        # \u8f93\u51fa\u6bcf\u4e00\u884c\u6570\u636e\n\n# \u5199\u5165CSV\nwith open('mydata.csv', 'w', newline='') as file:\n    w = csv.writer(file)  # \u521b\u5efaCSV writer\u5bf9\u8c61\n    w.writerow(&#91;'name', 'age'])     # \u5199\u5165\u4e00\u884c\u6570\u636e\n    w.writerow(&#91;'Mathew', '23'])    <\/code><\/pre>\n\n\n\n<p># \u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f# \u4f20\u7edf\u65b9\u6cd5data = [100, 200, 300, 400, 500]result = []for x in data:    result.append(x * 20)print(result)  # [2000, 4000, 6000, 8000, 10000]<\/p>\n\n\n\n<p># \u63a8\u5bfc\u5f0f\u65b9\u6cd5data = [100, 200, 300, 400, 500]result = [x * 20 for x in data]print(result)  # [2000, 4000, 6000, 8000, 10000]<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u6620\u5c04\u548c\u8fc7\u6ee4\n# \u4f7f\u7528map\ndef mul(n):\n    return n * n\n\nnum = &#91;1, 2, 3, 4, 5]\nresult = list(map(mul, num)) # \u4f7f\u7528map\u51fd\u6570\u5bf9\u5217\u8868num\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528mul\u51fd\u6570\uff0c\u8fd4\u56de\u65b0\u7684\u5217\u8868\nprint(result) # &#91;1, 4, 9, 16, 25]\n\n# \u4f7f\u7528filter\ndef is_odd(n):\n    return n % 2 == 1\n\nnum = &#91;1, 2, 3, 4, 5]\nresult = list(filter(is_odd, num)) # \u4f7f\u7528filter\u51fd\u6570\u5bf9\u5217\u8868num\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528is_odd\u51fd\u6570\uff0c\u8fc7\u6ee4\u51fa\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u8fd4\u56de\u65b0\u7684\u5217\u8868\nprint(result) # &#91;1, 3, 5]<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4f7f\u7528\u751f\u6210\u5668\ndef iterate(n):\n    i = 0\n    while i &lt; n:\n        yield i\n        i += 1\n\nfor i in iterate(10):\n    print(i)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># \u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\n# \u666e\u901a\u65b9\u5f0f\ndata = {'a': 1, 'b': 2, 'c': 3, 'd': 4}\nresult = {}\nfor key, value in data.items(): # \u904d\u5386\u5b57\u5178data\u4e2d\u7684\u952e\u503c\u5bf9\n    if value &gt; 2: # \u5982\u679c\u503c\u5927\u4e8e2\n        result&#91;key] = value # \u5c06\u952e\u503c\u5bf9\u6dfb\u52a0\u5230\u7ed3\u679c\u5b57\u5178result\u4e2d\nprint(result) # {'c': 3, 'd': 4}\n\n# \u63a8\u5bfc\u5f0f\u65b9\u5f0f\ndata = {'a': 1, 'b': 2, 'c': 3, 'd': 4}\nresult = {key: value for key, value in data.items() if value &gt; 2} # \u4f7f\u7528\u5b57\u5178\u63a8\u5bfc\u5f0f\uff0c\u7b5b\u9009\u51fa\u503c\u5927\u4e8e2\u7684\u952e\u503c\u5bf9\uff0c\u5e76\u6784\u9020\u65b0\u7684\u5b57\u5178result\nprint(result) # {'c': 3, 'd': 4}\n\n# \u683c\u5f0f\u5316\u8f93\u51fa\n\u8f93\u51fa\u7ed3\u679c\uff1a\n{'c': 3, 'd': 4}\n{'c': 3, 'd': 4}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># \u6b63\u5219\u8868\u8fbe\u5f0f\nimport re\n# \u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u5339\u914d\u7684\u6a21\u5f0f\nstring = \"A string with numbers 1234567890\"\nresult = re.search(r\"\\d\\d\\d\\d\\d\\d\\d\\d\\d\\d\", string) # \u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u5339\u914d\u7684\u6a21\u5f0f\uff0c\u8be5\u6a21\u5f0f\u8868\u793a10\u4e2a\u6570\u5b57\u7684\u5e8f\u5217\nprint(result.group()) # \u6253\u5370\u5339\u914d\u5230\u7684\u7ed3\u679c\n# \u67e5\u627e\u7535\u5b50\u90ae\u4ef6\u5730\u5740\nstring = \"My email is xyz@email.com\"\nresult = re.search(r\"\\w+@\\w+\\.\\w+\", string) # \u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u5339\u914d\u7684\u6a21\u5f0f\uff0c\u8be5\u6a21\u5f0f\u8868\u793a\u4e00\u4e2a\u6216\u591a\u4e2a\u5355\u8bcd\u5b57\u7b26\u3001@\u7b26\u53f7\u3001\u4e00\u4e2a\u6216\u591a\u4e2a\u5355\u8bcd\u5b57\u7b26\u3001.\u7b26\u53f7\u548c\u4e00\u4e2a\u6216\u591a\u4e2a\u5355\u8bcd\u5b57\u7b26\nprint(result.group()) # \u6253\u5370\u5339\u914d\u5230\u7684\u7ed3\u679c\n\n# \\w+ \u8868\u793a\u4e00\u4e2a\u6216\u591a\u4e2a\u5355\u8bcd\u5b57\u7b26\n# \\w* \u8868\u793a\u96f6\u4e2a\u6216\u591a\u4e2a\u5355\u8bcd\u5b57\u7b26\n# \\w? \u8868\u793a\u96f6\u4e2a\u6216\u4e00\u4e2a\u5355\u8bcd\u5b57\u7b26\n# \\d+ \u8868\u793a\u4e00\u4e2a\u6216\u591a\u4e2a\u6570\u5b57\n# a-z \u8868\u793a\u4ecea\u5230z\u7684\u5c0f\u5199\u5b57\u6bcd\n# A-Z \u8868\u793a\u4eceA\u5230Z\u7684\u5927\u5199\u5b57\u6bcd\n# \\s+ \u8868\u793a\u4e00\u4e2a\u6216\u591a\u4e2a\u7a7a\u683c\u5b57\u7b26\n\n# \u683c\u5f0f\u5316\u8f93\u51fa\n\u8f93\u51fa\u7ed3\u679c\uff1a\n1234567890\nxyz@email.com<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316 \u8fd9\u4e2a\u65b9\u4fbf\u7684\u4ee3\u7801\u7247\u6bb5\u5c06\u8ba9\u4f60\u5728Python\u4e2d\u683c\u5f0f\u5316\u4f60\u7684\u5b57\u7b26\u4e32\u3002\u4e0b\u9762\u6211\u5206 <span class=\"readmore\"><a href=\"http:\/\/xnw60rlg.ipyingshe.net:5347\/?p=3248\">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,1],"tags":[],"class_list":["post-3248","post","type-post","status-publish","format-standard","hentry","category-2","category-python","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3248"}],"version-history":[{"count":1,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3248\/revisions"}],"predecessor-version":[{"id":3249,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/3248\/revisions\/3249"}],"wp:attachment":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3248"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}