2. Dims\u4f5c\u4e3a\u53d8\u91cf<\/strong><\/div>\n\u5728\u5927\u591a\u6570\u8bed\u8a00\u4e2d\uff0c\u4e3a\u4e86\u5c06\u6570\u7ec4\u653e\u5165\u4e00\u7ec4\u53d8\u91cf\u4e2d\u9700\u8fed\u4ee3\u5faa\u73af\u503c\uff0c\u6216\u6309\u4f4d\u7f6e\u8bbf\u95ee\u6697\u70b9\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n
firstdim = array[1]<\/pre>\n\u7136\u800c\uff0c\u5728Python\u4e2d\u6709\u4e00\u79cd\u66f4\u597d\u66f4\u5feb\u7684\u65b9\u6cd5\u3002\u4e3a\u4e86\u5c06\u4e00\u5217\u503c\u6539\u4e3a\u53d8\u91cf\uff0c\u53ef\u4ee5\u7b80\u5355\u5730\u5c06\u53d8\u91cf\u540d\u8bbe\u7f6e\u4e3a\u4e0e\u6570\u7ec4\u957f\u5ea6\u76f8\u540c\u7684\u6570\u7ec4\uff1a<\/p>\n
array = [5, 10, 15, 20] \r\nfive, ten, fift, twent = array<\/pre>\n<\/p>\n
3. \u751f\u6210\u5668\u7684next()\u8fed\u4ee3<\/strong><\/div>\n\u5728\u7f16\u7a0b\u4e2d\u7684\u5927\u591a\u6570\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8bbf\u95ee\u4e00\u4e2a\u7d22\u5f15\uff0c\u5e76\u4f7f\u7528\u8ba1\u6570\u5668\u83b7\u53d6\u4f4d\u7f6e\u6570\u5b57\uff0c\u8ba1\u6570\u5668\u5c06\u53ea\u662f\u4e00\u4e2a\u503c\uff0c\u6dfb\u52a0\u5230\uff1a<\/p>\n
array1 = [5, 10, 15, 20] \r\narray2 = (x ** 2 for x in range(10)) \r\ncounter = 0for i in array1:# This code wouldn't work because 'i' is not in array2. \r\n # i = array2[i] \r\n i = array2[counter] \r\n# ^^^ This code would because we areaccessing the position of i<\/pre>\n\u6211\u4eec\u4e5f\u53ef\u4ee5\u7528next()\u4ee3\u66ff\u5b83\u3002Next\u4f7f\u7528\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u8be5\u8fed\u4ee3\u5668\u5c06\u5f53\u524d\u4f4d\u7f6e\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\uff0c\u5e76\u5728\u540e\u53f0\u8fed\u4ee3\u5217\u8868\uff1a<\/p>\n
g = (x ** 2 for x in range(10)) \r\nprint(next(g)) \r\nprint(next(g))\r\n<\/pre>\n4. \u667a\u80fd\u62c6\u5305<\/strong><\/div>\n\u8fed\u4ee3\u5730\u89e3\u538b\u503c\u53ef\u80fd\u4f1a\u975e\u5e38\u8017\u8d39\u65f6\u529b\uff0cPython\u4e2d\u6709\u51e0\u79cd\u4e0d\u9519\u7684\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u89e3\u538b\u5217\u8868\u7684\u65b9\u6cd5\u3002\u5176\u4e2d\u4e00\u4e2a\u662f*\uff0c\u5b83\u5c06\u586b\u5145\u672a\u5206\u914d\u7684\u503c\u5e76\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230\u53d8\u91cf\u540d\u4e0b\u7684\u65b0\u5217\u8868\u4e2d\u3002<\/p>\n
a, *b, c = [1, 2, 3, 4, 5]<\/pre>\n<\/p>\n
5. \u5217\u4e3e<\/strong><\/div>\n\u4e0d\u4e86\u89e3\u5217\u4e3e\u90a3\u53ef\u4e0d\u592a\u884c\u3002\u5217\u4e3e\u53ef\u4ee5\u83b7\u53d6\u5217\u8868\u4e2d\u67d0\u4e9b\u503c\u7684\u7d22\u5f15\uff0c\u5728\u6570\u636e\u79d1\u5b66\u4e2d\u4f7f\u7528\u6570\u7ec4\u800c\u4e0d\u662f\u6570\u636e\u5e27\u65f6\uff0c\u8fd9\u5c31\u7279\u522b\u6709\u7528\uff1a<\/p>\n
for i,w in enumerate(array): \r\n print(i,w) \r\n<\/pre>\n<\/p>\n
6. \u547d\u540d\u5207\u7247<\/strong><\/div>\nPython\u4e2d\uff0c\u5206\u5272\u5217\u8868\u975e\u5e38\u7b80\u5355\uff0c\u5404\u5f0f\u5404\u6837\u4f18\u79c0\u5de5\u5177\u90fd\u80fd\u505a\u5230\u3002\u7279\u522b\u597d\u7684\u4e00\u70b9\u662f\uff0c\u5b83\u8fd8\u80fd\u591f\u7ed9\u5217\u8868\u547d\u540d\uff0c\u8fd9\u5bf9\u4e8ePython\u4e2d\u7684\u7ebf\u6027\u4ee3\u6570\u7279\u522b\u6709\u7528\uff1a<\/p>\n
a = [0, 1, 2, 3, 4, 5] \r\nLASTTHREE = slice(-3, None) \r\nslice(-3, None, None) \r\nprint(a[LASTTHREE]) \r\n<\/pre>\n<\/p>\n
7. Itertools<\/strong><\/div>\n\u5982\u679c\u6df1\u5165\u5b66\u4e60Python\uff0c\u90a3\u4f60\u80af\u5b9a\u8981\u719f\u6089itertools\u3002itertools\u662f\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5b83\u53ef\u4ee5\u4e0d\u65ad\u5730\u89e3\u51b3\u8fed\u4ee3\u95ee\u9898\u3002\u5b83\u4e0d\u4ec5\u4f7f\u7f16\u5199\u590d\u6742\u5faa\u73af\u5927\u5e45\u5ea6\u53d8\u5bb9\u6613\uff0c\u800c\u4e14\u8fd8\u4f7f\u4ee3\u7801\u66f4\u7b80\u6d01\u5feb\u901f\u3002\u6709\u6570\u767e\u79cdItertools\u7684\u4f7f\u7528\u793a\u4f8b\uff0c\u6765\u770b\u770b\u5176\u4e2d\u4e00\u4e2a\uff1a<\/p>\n
c = [[1, 2], [3, 4], [5, 6]] \r\n# Let's convert this matrix to a 1 dimensional list. \r\nimport itertools as it \r\nnewlist = list(it.chain.from_iterable(c))\r\n<\/pre>\n8. \u5206\u7ec4\u76f8\u90bb\u5217\u8868<\/strong><\/div>\n\u5728for\u5faa\u73af\u4e2d\uff0c\u5bf9\u76f8\u90bb\u5faa\u73af\u8fdb\u884c\u5206\u7ec4\u5f53\u7136\u5f88\u5bb9\u6613\uff0c\u7279\u522b\u662f\u4f7f\u7528zip()\uff0c\u4f46\u8fd9\u80af\u5b9a\u4e0d\u662f\u6700\u597d\u7684\u65b9\u6cd5\u3002\u4e3a\u4e86\u66f4\u8f7b\u677e\u4fbf\u6377\u5730\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u53ef\u4ee5\u7528zip\u7f16\u5199\u4e00\u4e2alambda\u8868\u8fbe\u5f0f\uff0c\u8be5\u8868\u8fbe\u5f0f\u5c06\u5bf9\u76f8\u90bb\u5217\u8868\u8fdb\u884c\u5206\u7ec4\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n
a = [1, 2, 3, 4, 5, 6] \r\ngroup_adjacent = lambda a, k: zip(*([iter(a)] * k)) \r\ngroup_adjacent(a, 3) [(1, 2, 3), (4, 5, 6)] \r\ngroup_adjacent(a, 2) [(1, 2), (3, 4), (5, 6)] \r\ngroup_adjacent(a, 1) \r\n<\/pre>\n<\/p>\n
9. \u8ba1\u6570\u5668<\/strong><\/div>\n\u96c6\u5408\u4e5f\u662f\u6a21\u5757\u4e2d\u5f88\u597d\u7684\u6807\u51c6\u5e93\uff0c\u8fd9\u91cc\u5411\u5927\u5bb6\u4ecb\u7ecd\u7684\u662f\u96c6\u5408\u4e2d\u7684\u8ba1\u6570\u5668\u3002\u4f7f\u7528\u8ba1\u6570\u5668\uff0c\u53ef\u4ee5\u8f7b\u677e\u83b7\u5f97\u4e00\u4e2a\u5217\u8868\u7684\u8ba1\u6570\u3002\u8fd9\u5bf9\u4e8e\u83b7\u53d6\u6570\u636e\u4e2d\u7684\u503c\u603b\u6570\u3001\u6570\u636e\u7684\u7a7a\u8ba1\u6570\uff0c\u4ee5\u53ca\u67e5\u770b\u6570\u636e\u7684\u552f\u4e00\u503c\u975e\u5e38\u6709\u7528\u3002<\/p>\n
\u201c\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u4f7f\u7528Pandas\u5462?\u201d\u4f7f\u7528Pandas\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u65e0\u7591\u4f1a\u56f0\u96be\u5f97\u591a\uff0c\u800c\u4e14\u8fd9\u53ea\u662f\u5728\u90e8\u7f72\u7b97\u6cd5\u65f6\u9700\u8981\u6dfb\u52a0\u5230\u865a\u62df\u73af\u5883\u4e2d\u7684\u53e6\u4e00\u4e2a\u4f9d\u8d56\u9879\u3002\u53e6\u5916\uff0cPython\u4e2d\u7684\u8ba1\u6570\u5668\u7c7b\u578b\u6709\u5f88\u591aPandas\u7cfb\u5217\u6ca1\u6709\u7684\u7279\u6027\uff0c\u8fd9\u4f7f\u5176\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u66f4\u6709\u7528\u3002<\/p>\n
A = collections.Counter([1, 1, 2,2, 3, 3, 3, 3, 4, 5, 6, 7]) \r\nA Counter({3: 4, 1: 2, 2: 2, 4: 1, 5: 1, 6: 1, 7: 1}) \r\nA.most_common(1) [(3, 4)] \r\nA.most_common(3) [(3, 4), (1, 2), (2, 2)] \r\n<\/pre>\n10. \u51fa\u961f<\/strong><\/div>\n\u5982\u4e0b\u6240\u793a\uff0c\u51fa\u961f\u80fd\u8ba9\u4ee3\u7801\u975e\u5e38\u6574\u6d01\uff1a<\/p>\n
import collections \r\nQ = collections.deque() \r\nQ.append(1) \r\nQ.appendleft(2) \r\nQ.extend([3, 4]) \r\nQ.extendleft([5, 6]) \r\nQ.pop() \r\nQ.popleft() \r\nQ.rotate(3) \r\nQ.rotate(-3) \r\nprint(Q) \r\n<\/pre>\n<\/p>\n
\u8fd9\u4e9b\u662f\u7b14\u8005\u4e00\u76f4\u7231\u7528\u7684Python\u6280\u5de7\uff0c\u90fd\u975e\u5e38\u901a\u7528\u548c\u5b9e\u7528\uff0c\u5b9e\u8df5\u4e2d\u603b\u6709\u673a\u4f1a\u80fd\u7528\u5230\u3002Python\u7684\u6807\u51c6\u5e93\u51fd\u6570\u5de5\u5177\u7bb1\u53d8\u5f97\u8d8a\u6765\u8d8a\u591a\u6837\uff0c\u8fd8\u6709\u5f88\u591a\u7b14\u8005\u4e5f\u6ca1\u542c\u8bf4\u8fc7\u7684\u5de5\u5177\u3002\u5b66\u65e0\u6b62\u5883\uff0c\u8fd9\u591a\u4e48\u4ee4\u4eba\u5174\u594b\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"
Python\u770b\u8d77\u6765\u4f3c\u4e4e\u662f\u4e00\u79cd\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u5b66\u4e60\u7684\u7b80\u5355\u8bed\u8a00\uff0c\u4f46\u5b9e\u9645\u4e0a\uff0c\u5b66\u4f1a\u4e0d\u7b49\u4e8e\u7cbe\u901a\uff0cPython\u7684\u201c\u540e\u52b2\u513f\u201d\u8d85\u4e4e […]<\/p>\n","protected":false},"author":1329,"featured_media":195601,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-195593","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/195593","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\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=195593"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/195593\/revisions"}],"predecessor-version":[{"id":195602,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/195593\/revisions\/195602"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/195601"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=195593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=195593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=195593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}