{"id":1256,"date":"2019-07-16T08:28:42","date_gmt":"2019-07-16T08:28:42","guid":{"rendered":"https:\/\/www.ipcpu.com\/?p=1256"},"modified":"2019-11-04T08:29:33","modified_gmt":"2019-11-04T08:29:33","slug":"python-list-comprehensions","status":"publish","type":"post","link":"https:\/\/c.ipcpu.com\/2019\/07\/python-list-comprehensions\/","title":{"rendered":"Python\u4e2d\u7684List Comprehensions(\u5217\u8868\u63a8\u5bfc\u5f0f\/\u5217\u8868\u89e3\u6790\u5f0f)"},"content":{"rendered":"

Python\u4e2d\u7684List Comprehensions(\u5217\u8868\u63a8\u5bfc\u5f0f\/\u5217\u8868\u89e3\u6790\u5f0f).md<\/p>\n

\u4e00\u3001\u5f15\u5165<\/h2>\n

\u521d\u5b66Python\u7684\u540c\u5b66\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u770b\u8d77\u6765\u5f88\u7b80\u6d01\uff0c\u4f46\u53c8\u4e0d\u662f\u5f88\u597d\u7406\u89e3\u7684\u8bed\u53e5\uff0c\u4f8b\u5982\u4e0b\u9762\u7684<\/p>\n

\n
squares = [x * x for x in range(10)]\nprint(squares)<\/code><\/pre>\n<\/div>\n

\u4e0a\u9762\u8fd9\u4e2a\u5199\u6cd5\u5982\u679c\u7528\u666e\u901a\u5199\u6cd5\u5c31\u7b49\u540c\u4e8e\u5982\u4e0b<\/p>\n

\n
squares = []\n\nfor x in range(10):\n    squares.append(x*x)\n\nprint(squares)<\/code><\/pre>\n<\/div>\n

\u5176\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b<\/p>\n

\n
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]<\/code><\/pre>\n<\/div>\n

\u8fd9\u79cd\u5c31\u662fPython\u7684\u5217\u8868\u63a8\u5bfc\u5f0f(List Comprehensions),\u5b83\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u5e76\u4e14\u53ef\u4ee5\u589e\u52a0\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002<\/p>\n

\u8fd9\u4e9b\u5199\u6cd5\u5e38\u88ab\u79f0\u4f5c\u8bed\u6cd5\u7cd6(Syntactic sugar)\u3002<\/p>\n

\u4e8c\u3001\u57fa\u672c\u8bed\u6cd5<\/h2>\n

\u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u521b\u5efa\u5217\u8868\u7684\u65b9\u5f0f\uff0c\u6240\u4ee5\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u8fd4\u56de\u7ed3\u679c\u5c31\u662f\u4e00\u4e2a\u5217\u8868\u3002
\n\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b:<\/p>\n

\n
new_list = [expression(i) for i in old_list if condition(i)]\n\n#*result* = [*transform* *iteration* *filter* ]\n<\/code><\/pre>\n<\/div>\n

\u4e09\u3001\u5e38\u89c1\u4f8b\u5b50<\/h2>\n
\n
x = [i for i in range(10)]\n\n#\u83b7\u53d6\u5176\u4e2d\u7684\u5076\u6570\nprint([i for i in x if i%2==0])\n\n#\u5bf9\u6bcf\u4e2a\u5143\u7d20\u6c42\u5e73\u65b9\nprint([i**2 for i in x])\n\n\n\nwords = [\"this\",\"is\",\"a\",\"list\",\"of\",\"words\"]\n\n#\u53d6\u6bcf\u4e2a\u5355\u8bcd\u7684\u9996\u5b57\u6bcd\nprint( [word[0] for word in words])\n\n\nd = {'a':1,'b':2,'c':3}\n\n#\u7b5b\u9009 value \u503c\nprint([i for i in d.values() if i >1])\n\n#\u83b7\u53d6 key \u503c\u5217\u8868\uff0c\u5e76\u4e14\u53ef\u4ee5\u52a0 if \u5224\u65ad\nprint([i for i in d.keys()])\n<\/code><\/pre>\n<\/div>\n

\u56db\u3001\u6269\u5c55-\u591a\u4e2afor\u7684\u5217\u8868\u63a8\u5bfc\u5f0f<\/h2>\n

\u5217\u8868\u63a8\u5bfc\u5f0f\u4e0d\u662f\u53ea\u80fd\u6709\u4e00\u4e2afor\u5faa\u73af\uff0c\u4f7f\u7528\u591a\u4e2afor\u65f6\u76f8\u5f53\u4e8e\u5d4c\u5957\u7684for\u5faa\u73af\uff0c\u4f8b\u5982<\/p>\n

\n
list=[1,2,3,4,5]\nnewlist=[x*y for x in list for y in range(5) if x > 3]\nprint(newlist)<\/code><\/pre>\n<\/div>\n

\u5176\u542b\u4e49\u5982\u679c\u7528\u539f\u59cb\u8bed\u53e5\u8868\u8fbe\u5982\u4e0b<\/p>\n

\n
nlist=[]\nfor x in list:\n    for y in range(0,5):\n        if x > 3 :\n            nlist.append(x*y)\nprint(nlist)<\/code><\/pre>\n<\/div>\n

\u4e94\u3001\u6269\u5c55-\u5b57\u5178\u63a8\u5bfc\u5f0f<\/h2>\n

\u9664\u4e86\u5217\u8868list\uff0cPython\u4e2d\u7684\u5b57\u5178dict\u4e5f\u652f\u6301\u540c\u6837\u7684\u63a8\u5bfc\u5f0f<\/p>\n

\n
dict = {x: x*x for x in [1, 2, 3]}\nprint(dict)\n\nsome_dict={'a':1, 'b':2, 'c':3}\n#\u5feb\u901f\u5bf9\u6362Key\u548cValue\u7684\u4f4d\u7f6e\nprint({v: k for k, v in some_dict.items()})<\/code><\/pre>\n<\/div>\n

\u65e2\u7136\u5217\u8868\u3001\u5b57\u5178\u90fd\u6709\u63a8\u5bfc\u5f0f\uff0c\u90a3\u5143\u7ec4\u662f\u4e0d\u662f\u4e5f\u6709\u5462\uff1f
\n\u628a[]\u6539\u6210() \u4e0d\u662f\u5c31\u53ef\u4ee5\u4e86\u5462\uff1f\u5f88\u9057\u61be\uff0c\u5982\u679c[]\u6539\u6210()\u5c31\u53d8\u6210\u4e86\u751f\u6210\u5668\u5bf9\u8c61\uff0c\u540e\u9762\u6211\u4eec\u518d\u4ecb\u7ecd\u3002<\/p>\n

\u516d\u3001\u53c2\u8003\u8d44\u6599<\/h2>\n

https:\/\/www.python.org\/dev\/peps\/pep-0202\/<\/a>
\n
https:\/\/juejin.im\/post\/5b3b2d9ce51d45191556b2ef<\/a>
\n
https:\/\/www.fearlazy.com\/index.php\/post\/152.html<\/a><\/p>\n

<\/p>\n

<\/div>\n
<\/div>\n
\n
<\/div>\n
<\/div>\n
<\/div>\n
<\/div>\n
<\/div>\n<\/div>\n
\n
<\/div>\n
<\/div>\n
<\/div>\n
<\/div>\n
<\/div>\n<\/div>\n

<\/wiz_tmp_tag><\/p>\n

\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def<\/a> » Python\u4e2d\u7684List Comprehensions(\u5217\u8868\u63a8\u5bfc\u5f0f\/\u5217\u8868\u89e3\u6790\u5f0f)<\/a><\/p>","protected":false},"excerpt":{"rendered":"

Python\u4e2d\u7684List Comprehensions(\u5217\u8868\u63a8\u5bfc\u5f0f\/\u5217\u8868\u89e3\u6790\u5f0f).md \u4e00\u3001\u5f15\u5165 \u521d\u5b66Python\u7684\u540c\u5b66\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u770b\u8d77\u6765\u5f88\u7b80\u6d01\uff0c\u4f46\u53c8\u4e0d\u662f\u5f88\u597d\u7406\u89e3\u7684\u8bed\u53e5\uff0c\u4f8b\u5982\u4e0b\u9762\u7684 squares = [x * x for x in range(10)] print(squares) \u4e0a\u9762\u8fd9\u4e2a\u5199\u6cd5\u5982\u679c\u7528\u666e\u901a\u5199\u6cd5\u5c31\u7b49\u540c\u4e8e\u5982\u4e0b squares = [] for x in range(10): squares.append(x*x) print(squares) \u5176\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] \u8fd9\u79cd\u5c31\u662fPython\u7684\u5217\u8868\u63a8\u5bfc\u5f0f(List Comprehensions),\u5b83\u53ef\u4ee5\u8ba9\u4ee3\u7801\u66f4\u7b80\u6d01\uff0c\u5e76\u4e14\u53ef\u4ee5\u589e\u52a0\u53ef\u8bfb\u6027\u548c\u6267\u884c\u6548\u7387\u3002 \u8fd9\u4e9b\u5199\u6cd5\u5e38\u88ab\u79f0\u4f5c\u8bed\u6cd5\u7cd6(Syntactic sugar)\u3002 \u4e8c\u3001\u57fa\u672c\u8bed\u6cd5 \u5217\u8868\u63a8\u5bfc\u5f0f\u662f\u4e00\u79cd\u521b\u5efa\u5217\u8868\u7684\u65b9\u5f0f\uff0c\u6240\u4ee5\u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u8fd4\u56de\u7ed3\u679c\u5c31\u662f\u4e00\u4e2a\u5217\u8868\u3002 \u5217\u8868\u63a8\u5bfc\u5f0f\u7684\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b: new_list = [expression(i) for i in old_list if condition(i)] #*result* = [*transform* *iteration* […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[59],"_links":{"self":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1256"}],"collection":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/comments?post=1256"}],"version-history":[{"count":1,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1256\/revisions"}],"predecessor-version":[{"id":1257,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1256\/revisions\/1257"}],"wp:attachment":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/media?parent=1256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/categories?post=1256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/tags?post=1256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}