{"id":173844,"date":"2020-03-23T08:15:49","date_gmt":"2020-03-23T00:15:49","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=173844"},"modified":"2020-03-13T10:45:53","modified_gmt":"2020-03-13T02:45:53","slug":"python-list-comprehension","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/python-list-comprehension.html","title":{"rendered":"Python\u4e2d\u7684\u5217\u8868\u7406\u89e3\u5171\u540c\u5b66\u4e60"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5217\u8868\u7406\u89e3\u901a\u5e38\u5728Python\u4e2d\u7528\u4e8e\u7f16\u5199\u5355\u884c\u8bed\u53e5\uff0c\u8fd9\u4e9b\u8bed\u53e5\u901a\u8fc7\u5faa\u73af\u8bbf\u95ee\u53ef\u8fed\u4ee3\u5bf9\u8c61\u6765\u521b\u5efa\u65b0\u5217\u8868\u6216\u5b57\u5178\u3002\u672c\u6587\u5c06\u9996\u5148\u4ecb\u7ecd\u6709\u5173for\u5faa\u73af\u5728Python\u4e2d\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u7136\u540e\u8bf4\u660e\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5217\u8868\u7406\u89e3\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5217\u8868\u7406\u89e3\u901a\u5e38\u5728Python\u4e2d\u7528\u4e8e\u7f16\u5199\u5355\u884c\u8bed\u53e5\uff0c\u8fd9\u4e9b\u8bed\u53e5\u901a\u8fc7\u5faa\u73af\u8bbf\u95ee\u53ef\u8fed\u4ee3\u5bf9\u8c61\u6765\u521b\u5efa\u65b0\u5217\u8868\u6216\u5b57\u5178\u3002\u672c\u6587\u5c06\u9996\u5148\u4ecb\u7ecd\u6709\u5173for\u5faa\u73af\u5728Python\u4e2d\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u7136\u540e\u8bf4\u660e\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5217\u8868\u7406\u89e3\u3002<\/p>\n

\"\"<\/p>\n

Python\u4e2d\u7684for\u5faa\u73af<\/strong><\/span><\/div>\n

Python\u4e2d\u7684for\u5faa\u73af\u8bed\u53e5\u6309\u987a\u5e8f\u904d\u5386\u4efb\u4f55\u5bf9\u8c61\u3001\u5217\u8868\u3001\u5b57\u7b26\u4e32\u7b49\u7684\u6210\u5458\u3002\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u76f8\u6bd4\uff0c\u5b83\u7684\u8bed\u6cd5\u66f4\u52a0\u7b80\u6d01\uff0c\u4e0d\u9700\u8981\u624b\u52a8\u5b9a\u4e49\u8fed\u4ee3\u6b65\u9aa4\u5e76\u5f00\u59cb\u8fed\u4ee3\u3002\u5c3d\u7ba1\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u4f7f\u5176\u884c\u4e3a\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u76f8\u540c(\u672c\u6587\u4e0d\u4f1a\u8be6\u7ec6\u8ba8\u8bba)\u3002\u60a8\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u8bf8\u5982continue\u3001break\u3001pass\u7b49\u8bed\u53e5\u5bf9for\u5faa\u73af\u8fdb\u884c\u4e00\u4e9b\u63a7\u5236\u3002\u4ee5\u4e0b\u662fPython\u4e2dfor\u5faa\u73af\u7684\u7b80\u5355\u793a\u4f8b\uff1a<\/p>\n

for x in range(10): \r\n  print(x)for x in range(10):  print(x) \r\n<\/pre>\n

\u4e0a\u9762\u7684for\u5faa\u73af\u5c06\u8f93\u51fa\u4ece0\u52309\u7684\u5341\u4e2a\u6570\u5b57\u3002<\/p>\n

\u5217\u8868\u7406\u89e3<\/strong><\/span><\/div>\n

\u5217\u8868\u7406\u89e3\u53ea\u4e0d\u8fc7\u662f\u4e00\u79cd\u5728\u5355\u7ebf\u8bed\u53e5\u4e2d\u7f16\u5199\u591a\u884cfor\u5faa\u73af\u7684\u7b80\u5355\u65b9\u6cd5\u3002\u4e0b\u9762\u7684\u5217\u8868\u7406\u89e3\u793a\u4f8b\u5c06\u901a\u8fc7\u5728\u5176\u4e2d\u5305\u542b\u6240\u6709\u201c x\u201d\u503c\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u5176\u540d\u79f0\u4e3a[0\uff0c1\uff0c2\uff0c3\uff0c4\uff0c5\uff0c6\uff0c7\uff0c8\uff0c9]\u3002<\/p>\n

numbers = [x for x in range(10)] \r\nprint (numbers)numbers = [x for x in range(10)]print (numbers) \r\n<\/pre>\n

\u8bf7\u6ce8\u610f\uff0c\u5217\u8868\u7406\u89e3\u59cb\u7ec8\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u5217\u8868\uff0c\u5e76\u4e14\u4e0d\u4f1a\u4fee\u6539\u8868\u8fbe\u5f0f\u4e2d\u4f7f\u7528\u7684\u539f\u59cb\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\u5178\u578b\u7684\u5217\u8868\u7406\u89e3\u8868\u8fbe\u5f0f\u5fc5\u987b\u5177\u6709\u201c for\u201d\u5b50\u53e5\uff0c\u5e76\u4e14\u53ef\u4ee5\u540e\u63a5\u201c if\u201d\u548c\u201c else\u201d\u6761\u4ef6\u8bed\u53e5\u3002\u5728\u4e0d\u4f7f\u7528\u5217\u8868\u7406\u89e3\u7684\u60c5\u51b5\u4e0b\uff0c\u4ee5\u4e0a\u793a\u4f8b\u5c06\u6309\u7167\u4ee5\u4e0b\u65b9\u5f0f\u7f16\u5199\uff1a<\/p>\n

numbers = [] \r\nfor x in range(10): \r\n  numbers.append(x) \r\n \r\nprint (numbers)numbers = []for x in range(10):  numbers.append(x)print (numbers) \r\n<\/pre>\n
\u6027\u80fd\u548c\u53ef\u8bfb\u6027<\/strong><\/span><\/div>\n

\u5217\u8868\u7406\u89e3\u6bd4for\u5faa\u73af\u5feb\u3002\u4f46\u662f\uff0c\u9664\u975e\u60a8\u8981\u904d\u5386\u6210\u5343\u4e0a\u4e07\u7684\u9879\u76ee\uff0c\u5426\u5219\u60a8\u4e0d\u4f1a\u6ce8\u610f\u5230\u6027\u80fd\u4e0a\u7684\u91cd\u5927\u6539\u8fdb\u3002\u5c3d\u7ba1\u5217\u8868\u7406\u89e3\u4e3a\u5faa\u73af\u7f16\u5199\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u6cd5\uff0c\u4f46\u662f\u590d\u6742\u7684\u8868\u8fbe\u5f0f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u8f83\u5dee\u548c\u5197\u957f\u3002\u4fdd\u6301\u4ee3\u7801\u53ef\u8bfb\u6027\u5f88\u91cd\u8981\uff0c\u9664\u975e\u60a8\u7684\u7a0b\u5e8f\u9700\u8981\u8fbe\u5230\u6700\u5927\u7684\u6027\u80fd\u3002<\/p>\n

\u793a\u4f8b\uff1a\u5bf9\u5b57\u5178\u548c\u96c6\u5408\u4f7f\u7528\u5217\u8868\u7406\u89e3\u8bed\u6cd5<\/strong><\/span><\/div>\n

python\u5b57\u5178\u662f\u952e-\u503c\u5bf9\u4e2d\u5b9a\u4e49\u7684\u5143\u7d20\u7684\u96c6\u5408\uff0c\u800c\u96c6\u5408\u662f\u4e0d\u5141\u8bb8\u91cd\u590d\u7684\u552f\u4e00\u503c\u7684\u96c6\u5408\u3002\u5217\u8868\u7406\u89e3\u4e5f\u53ef\u4ee5\u4e0ePython\u5b57\u5178\u548c\u96c6\u5408\u4e00\u8d77\u4f7f\u7528\u3002\u8bed\u6cd5\u7565\u6709\u4e0d\u540c\uff0c\u73b0\u5728\u5fc5\u987b\u4f7f\u7528\u82b1\u62ec\u53f7\uff0c\u800c\u4e0d\u662f\u5c06\u8868\u8fbe\u5f0f\u7528\u65b9\u62ec\u53f7\u62ec\u8d77\u6765\u3002\u60a8\u8fd8\u5c06\u83b7\u5f97\u4e00\u4e2a\u65b0\u7684dictionary \/ set\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u65b0\u7684\u5217\u8868\u3002<\/p>\n

data = {\"city\": \"new york\", \"name\": \"john doe\"}  \r\nformatted_data = {k: v.title() for k,v in data.items()}  \r\nprint (formatted_data)data = {\"city\": \"new york\", \"name\": \"john doe\"}formatted_data = {k: v.title() for k,v in data.items()}print (formatted_data) \r\n<\/pre>\n

\u4e0a\u9762\u7684\u793a\u4f8b\u5c06\u5b57\u7b26\u4e32\u503c\u8f6c\u6362\u4e3atitle case\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a\u201c formatted_data\u201d\u7684\u65b0\u5b57\u5178\uff0c\u5176\u8f93\u51fa\u4e3a\uff1a{'city'\uff1a'New York'\uff0c'name'\uff1a'John Doe'}\u3002\u60a8\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5728\u5de6\u4fa7\u6307\u5b9a\u73b0\u6709\u7684\u5b57\u5178\u53d8\u91cf\u6765\u66f4\u6539dictionary \/ set in-place\u3002<\/p>\n

data = {\"city\": \"new york\", \"name\": \"john doe\"}  \r\ndata = {k: v.title() for k,v in data.items()}  \r\nprint (data)data = {\"city\": \"new york\", \"name\": \"john doe\"}data = {k: v.title() for k,v in data.items()}print (data) \r\n<\/pre>\n

\u5982\u679c\u4e0d\u4f7f\u7528\u5b57\u5178\u7406\u89e3\uff0c\u4ee3\u7801\u5c06\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

data = {\"city\": \"new york\", \"name\": \"john doe\"} \r\n \r\nformatted_data = {} \r\n \r\nfor k, v in data.items(): \r\n  formatted_data[k] = v.title() \r\n \r\nprint (formatted_data)data = {\"city\": \"new york\", \"name\": \"john doe\"}formatted_data = {}for k, v in data.items():  formatted_data[k] = v.title()print (formatted_data) \r\n<\/pre>\n

\u7531\u4e8e\u96c6\u5408\u4e2d\u6ca1\u6709\u952e\u503c\u5bf9\uff0c\u56e0\u6b64\u53ef\u4ee5\u6309\u7167\u4e0e\u5217\u8868\u7406\u89e3\u76f8\u540c\u7684\u65b9\u5f0f\u5b9a\u4e49\u96c6\u5408\u7406\u89e3\u3002\u552f\u4e00\u7684\u533a\u522b\u662f\u82b1\u62ec\u53f7\u7684\u4f7f\u7528\u3002<\/p>\n

\u793a\u4f8b\uff1a\u5217\u8868\u7406\u89e3\u4e2d\u7684\u591a\u4e2aFor\u5faa\u73af<\/strong><\/span><\/div>\n

\u4e0a\u9762\u63d0\u5230\u7684\u5217\u8868\u7406\u89e3\u793a\u4f8b\u662f\u57fa\u672c\u7684\uff0c\u5e76\u4f7f\u7528\u5355\u4e2a\u201c for\u201d\u8bed\u53e5\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u591a\u4e2afor\u5faa\u73af\u548c\u6761\u4ef6\u201c if\u201d\u8bed\u53e5\u7684\u793a\u4f8b\u3002<\/p>\n

adjectives = [\"Disco\", \"Eoan\", \"Focal\", \"Artful\"]  \r\nanimals = [\"Dingo\", \"Ermine\", \"Fossa\", \"Beaver\"]  \r\ncodenames = [x + \" \" + y for x in adjectives for y in animals if y.startswith(x[0])]  \r\nprint (codenames)adjectives = [\"Disco\", \"Eoan\", \"Focal\", \"Artful\"]animals = [\"Dingo\", \"Ermine\", \"Fossa\", \"Beaver\"]codenames = [x + \" \" + y for x in adjectives for y in animals if y.startswith(x[0])]print (codenames) \r\n<\/pre>\n

\u8be5\u4ee3\u7801\u5c06\u663e\u793a['Disco Dingo'\uff0c'Eoan Ermine'\uff0c'Focal Fossa']\u4f5c\u4e3a\u8f93\u51fa\u3002\u4e24\u4e2afor\u5faa\u73af\u904d\u5386\u201c\u5f62\u5bb9\u8bcd\u201d\u548c\u201c\u52a8\u7269\u201d\u5217\u8868\uff0c\u5e76\u4e14\u4e24\u4e2a\u5355\u8bcd\u7684\u9996\u5b57\u6bcd\u76f8\u540c\u65f6\uff0c\u5b83\u4eec\u7684\u6210\u5458\u4f7f\u7528\u7a7a\u683c\u8fde\u63a5\u5728\u4e00\u8d77\u3002\u4e0d\u4f7f\u7528\u5217\u8868\u7406\u89e3\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

adjectives = [\"Disco\", \"Eoan\", \"Focal\", \"Artful\"] \r\nanimals = [\"Dingo\", \"Ermine\", \"Fossa\", \"Beaver\"] \r\n \r\ncodenames = [] \r\n \r\nfor x in adjectives: \r\n  for y in animals: \r\n    if y.startswith(x[0]): \r\n      codenames.append(x + \" \" + y) \r\n \r\nprint (codenames)adjectives = [\"Disco\", \"Eoan\", \"Focal\", \"Artful\"]animals = [\"Dingo\", \"Ermine\", \"Fossa\", \"Beaver\"]codenames = []for x in adjectives:  for y in animals:    if y.startswith(x[0]):      codenames.append(x + \" \" + y)print (codenames) \r\n<\/pre>\n
\u793a\u4f8b\uff1a\u5e26\u6709if-else\u5b50\u53e5\u7684\u5217\u8868\u7406\u89e3<\/strong><\/span><\/div>\n

\u4e0b\u9762\u7684\u793a\u4f8b\u5c06\u663e\u793a\u5217\u8868\u7406\u89e3\u4e2dif\u548celse\u8bed\u53e5\u7684\u7528\u6cd5\u3002<\/p>\n

number_list = [1, 2, 3, 4] \r\nanother_list = [5, 6, 7, 8] \r\n \r\nresult = [True  if (x + y) % 2 == 0 else False for x in number_list for y in another_list] \r\n \r\nprint (result)number_list = [1, 2, 3, 4]another_list = [5, 6, 7, 8]result = [True  if (x + y) % 2 == 0 else False for x in number_list for y in another_list]print (result) \r\n<\/pre>\n

\u5728\u904d\u5386\u4e24\u4e2a\u5217\u8868\u65f6\uff0c\u4e0a\u9762\u7684\u5217\u8868\u7406\u89e3\u68c0\u67e5\u8fd9\u5bf9\u5143\u7d20\u7684\u603b\u548c\u662f\u5426\u4e3a\u5076\u6570\u3002\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\u5c06\u4e3a\u60a8\u663e\u793a[True\uff0cFalse\uff0cTrue\uff0cFalse\uff0cFalse\uff0cTrue\uff0cFalse\uff0cTrue\uff0cTrue\uff0cFalse\uff0cTrue\uff0cFalse\uff0cFalse\uff0cTrue\uff0cTrue\uff0cFalse\uff0cTrue]\u4f5c\u4e3a\u8f93\u51fa\u3002\u4e0d\u4f7f\u7528\u5217\u8868\u7406\u89e3\uff0c\u4ee3\u7801\u4f1a\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

number_list = [1, 2, 3, 4] \r\nanother_list = [5, 6, 7, 8] \r\nresult = [] \r\n \r\nfor x in number_list: \r\n  for y in another_list: \r\n    if (x + y) % 2 == 0: \r\n      result.append(True) \r\n    else: \r\n      result.append(False) \r\n \r\nprint (result)number_list = [1, 2, 3, 4]another_list = [5, 6, 7, 8]result = []for x in number_list:  for y in another_list:    if (x + y) % 2 == 0:      result.append(True)    else:      result.append(False)print (result) \r\n<\/pre>\n
\u7ed3\u8bba<\/strong><\/span><\/div>\n

\u5217\u8868\u7406\u89e3\u63d0\u4f9b\u4e86\u4e00\u79cd\u7f16\u5199\u7b80\u6d01\u7684\u5faa\u73af\u8bed\u53e5\u7684\u597d\u65b9\u6cd5\u3002\u4f46\u662f\uff0c\u5982\u679c\u4f7f\u7528\u591a\u4e2a\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5\uff0c\u5b83\u4eec\u4f1a\u5f88\u5feb\u53d8\u5f97\u590d\u6742\u4e14\u96be\u4ee5\u7406\u89e3\u3002\u6700\u540e\uff0c\u4f1a\u8ba9\u7a0b\u5e8f\u5458\u6bd4\u8f83\u8212\u9002\uff0c\u4f46\u662f\u901a\u5e38\u6700\u597d\u7f16\u5199\u663e\u5f0f\u7684\uff0c\u6613\u8bfb\u4e14\u6613\u4e8e\u8c03\u8bd5\u7684\u4ee3\u7801\uff0c\u800c\u4e0d\u8981\u8fc7\u591a\u4f7f\u7528\u901f\u8bb0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5217\u8868\u7406\u89e3\u901a\u5e38\u5728Python\u4e2d\u7528\u4e8e\u7f16\u5199\u5355\u884c\u8bed\u53e5\uff0c\u8fd9\u4e9b\u8bed\u53e5\u901a\u8fc7\u5faa\u73af\u8bbf\u95ee\u53ef\u8fed\u4ee3\u5bf9\u8c61\u6765\u521b\u5efa\u65b0\u5217\u8868\u6216\u5b57\u5178\u3002\u672c\u6587\u5c06\u9996\u5148\u4ecb\u7ecd […]<\/p>\n","protected":false},"author":1898,"featured_media":173848,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[153],"class_list":["post-173844","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=173844"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173844\/revisions"}],"predecessor-version":[{"id":173853,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/173844\/revisions\/173853"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/173848"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=173844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=173844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=173844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}