map(function_to_apply, list_of_inputs)\n<\/code><\/pre>\n\n\n\n\u5927\u591a\u6570\u65f6\u5019\uff0c\u6211\u4eec\u8981\u628a\u5217\u8868\u4e2d\u6240\u6709\u5143\u7d20\u4e00\u4e2a\u4e2a\u5730\u4f20\u9012\u7ed9\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u6536\u96c6\u8f93\u51fa\u3002\u6bd4\u65b9\u8bf4\uff1a<\/p>\n\n\n\n
items = [1, 2, 3, 4, 5]\nsquared = []\nfor i in items:\n squared.append(i**2)\n<\/code><\/pre>\n\n\n\nMap<\/code>\u53ef\u4ee5\u8ba9\u6211\u4eec\u7528\u4e00\u79cd\u7b80\u5355\u800c\u6f02\u4eae\u5f97\u591a\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002\u5c31\u662f\u8fd9\u6837\uff1a<\/p>\n\n\n\nitems = [1, 2, 3, 4, 5]\nsquared = list(map(lambda x: x**2, items))\n<\/code><\/pre>\n\n\n\n\u5927\u591a\u6570\u65f6\u5019\uff0c\u6211\u4eec\u4f7f\u7528\u533f\u540d\u51fd\u6570(lambdas)\u6765\u914d\u5408map<\/code>, \u6240\u4ee5\u6211\u5728\u4e0a\u9762\u4e5f\u662f\u8fd9\u4e48\u505a\u7684\u3002 \u4e0d\u4ec5\u7528\u4e8e\u4e00\u5217\u8868\u7684\u8f93\u5165\uff0c \u6211\u4eec\u751a\u81f3\u53ef\u4ee5\u7528\u4e8e\u4e00\u5217\u8868\u7684\u51fd\u6570\uff01<\/p>\n\n\n\ndef multiply(x):\n return (x*x)\ndef add(x):\n return (x+x)\n\nfuncs = [multiply, add]\nfor i in range(5):\n value = map(lambda x: x(i), funcs)\n print(list(value))\n # \u8bd1\u8005\u6ce8\uff1a\u4e0a\u9762print\u65f6\uff0c\u52a0\u4e86list\u8f6c\u6362\uff0c\u662f\u4e3a\u4e86python2\/3\u7684\u517c\u5bb9\u6027\n # \u5728python2\u4e2dmap\u76f4\u63a5\u8fd4\u56de\u5217\u8868\uff0c\u4f46\u5728python3\u4e2d\u8fd4\u56de\u8fed\u4ee3\u5668\n # \u56e0\u6b64\u4e3a\u4e86\u517c\u5bb9python3, \u9700\u8981list\u8f6c\u6362\u4e00\u4e0b\n\n# Output:\n# [0, 0]\n# [1, 2]\n# [4, 4]\n# [9, 6]\n# [16, 8]<\/code><\/pre>\n\n\n\nFilter<\/code><\/h1>\n\n\n\n\u987e\u540d\u601d\u4e49\uff0cfilter<\/code>\u8fc7\u6ee4\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u5e76\u4e14\u8fd4\u56de\u4e00\u4e2a\u7531\u6240\u6709\u7b26\u5408\u8981\u6c42\u7684\u5143\u7d20\u6240\u6784\u6210\u7684\u5217\u8868\uff0c\u7b26\u5408\u8981\u6c42<\/code>\u5373\u51fd\u6570\u6620\u5c04\u5230\u8be5\u5143\u7d20\u65f6\u8fd4\u56de\u503c\u4e3aTrue. \u8fd9\u91cc\u662f\u4e00\u4e2a\u7b80\u77ed\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\nnumber_list = range(-5, 5)\nless_than_zero = filter(lambda x: x < 0, number_list)\nprint(list(less_than_zero)) \n# \u8bd1\u8005\u6ce8\uff1a\u4e0a\u9762print\u65f6\uff0c\u52a0\u4e86list\u8f6c\u6362\uff0c\u662f\u4e3a\u4e86python2\/3\u7684\u517c\u5bb9\u6027\n# \u5728python2\u4e2dfilter\u76f4\u63a5\u8fd4\u56de\u5217\u8868\uff0c\u4f46\u5728python3\u4e2d\u8fd4\u56de\u8fed\u4ee3\u5668\n# \u56e0\u6b64\u4e3a\u4e86\u517c\u5bb9python3, \u9700\u8981list\u8f6c\u6362\u4e00\u4e0b\n\n# Output: [-5, -4, -3, -2, -1]\n<\/code><\/pre>\n\n\n\n\u8fd9\u4e2afilter<\/code>\u7c7b\u4f3c\u4e8e\u4e00\u4e2afor<\/code>\u5faa\u73af\uff0c\u4f46\u5b83\u662f\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u5e76\u4e14\u66f4\u5feb\u3002<\/p>\n\n\n\n\u6ce8\u610f\uff1a\u5982\u679cmap<\/code>\u548cfilter<\/code>\u5bf9\u4f60\u6765\u8bf4\u770b\u8d77\u6765\u5e76\u4e0d\u4f18\u96c5\u7684\u8bdd\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u770b\u770b\u53e6\u5916\u4e00\u7ae0\uff1a\u5217\u8868\/\u5b57\u5178\/\u5143\u7ec4\u63a8\u5bfc\u5f0f\u3002<\/p>\n\n\n\n\u8bd1\u8005\u6ce8\uff1a\u5927\u90e8\u5206\u60c5\u51b5\u4e0b\u63a8\u5bfc\u5f0f\u7684\u53ef\u8bfb\u6027\u66f4\u597d<\/p><\/blockquote>\n\n\n\n
Reduce<\/code><\/h1>\n\n\n\n\u5f53\u9700\u8981\u5bf9\u4e00\u4e2a\u5217\u8868\u8fdb\u884c\u4e00\u4e9b\u8ba1\u7b97\u5e76\u8fd4\u56de\u7ed3\u679c\u65f6\uff0cReduce<\/code> \u662f\u4e2a\u975e\u5e38\u6709\u7528\u7684\u51fd\u6570\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5f53\u4f60\u9700\u8981\u8ba1\u7b97\u4e00\u4e2a\u6574\u6570\u5217\u8868\u7684\u4e58\u79ef\u65f6\u3002<\/p>\n\n\n\n\u901a\u5e38\u5728 python \u4e2d\u4f60\u53ef\u80fd\u4f1a\u4f7f\u7528\u57fa\u672c\u7684 for \u5faa\u73af\u6765\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\u3002<\/p>\n\n\n\n
\u73b0\u5728\u6211\u4eec\u6765\u8bd5\u8bd5 reduce\uff1a<\/p>\n\n\n\n
from functools import reduce\nproduct = reduce( (lambda x, y: x * y), [1, 2, 3, 4] )\n\n# Output: 24<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u539f\u6587\uff1a https:\/\/docs.pythontab.com\/interpy\/Map_Filter\/Reduc<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,13],"tags":[],"yoast_head":"\n
4.0 Map\uff0cFilter\u548cReduce - Linux\u81ea\u52a8\u5316\u8fd0\u7ef4<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n