{"id":2541,"date":"2022-08-16T18:09:19","date_gmt":"2022-08-16T10:09:19","guid":{"rendered":"http:\/\/cnliutz.uicp.io\/?p=2541"},"modified":"2022-08-16T18:09:19","modified_gmt":"2022-08-16T10:09:19","slug":"%e6%9f%a5%e6%89%be%e6%95%b0%e5%ad%97%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%ef%bc%8c%e6%95%b0%e5%ad%97%e4%b9%8b%e5%92%8c%e7%9b%b8%e7%ad%89%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"http:\/\/g1n29wqq.ipyingshe.net:5347\/?p=2541","title":{"rendered":"\u67e5\u627e\u6570\u5b57\u5b57\u7b26\u4e32\u4e2d\uff0c\u6570\u5b57\u4e4b\u548c\u76f8\u7b49\u7684\u5b57\u7b26\u4e32"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\n'''\n\u3010\u7a0b\u5e8f\u529f\u80fd\u3011\n\u5bfb\u627e\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u7684\u6240\u6709\u6570\u5b57\u7684\u670b\u53cb\u6570\u5b57\uff0c\u670b\u53cb\u6570\u5b57\u5373\u6570\u503c\u4e4b\u548c\u76f8\u540c\u7684\u6570\u5b57\u5b57\u7b26\u4e32\u4f8b\u5982134\u548c224\uff0c\u5e76\u5c06\u5404\u670b\u53cb\u6570\u5b57\u7ec4\u5b58\u5165\u540c\u4e00\u5217\u8868\u4e2d\uff0c\n\u540c\u4e00\u7ec4\u670b\u53cb\u6570\u5b57\u9700\u6309\u6570\u5b57\u5b57\u7b26\u7684\u987a\u5e8f\u4ece\u5c0f\u5230\u5927\u6392\u5217\uff0c\u591a\u4e2a\u6570\u5b57\u670b\u53cb\u7ec4\u5219\u6309\u6570\u503c\u4e4b\u548c\u4ece\u5c0f\u5230\u5927\u987a\u5e8f\u6392\u5217\u3002\n\n\u3010\u6d4b\u8bd5\u6570\u636e\u4e0e\u8fd0\u884c\u7ed3\u679c\u3011\n\n\u8f93\u5165\uff1a\n143,267,342,562,224,134,276,252\n\n\u8f93\u51fa\uff1a\n\n&#91;'134', '143', '224']\n&#91;'252', '342']\n&#91;'562']\n&#91;'267', '276']\n'''\ndef findNumFriends(s):\n    s = s.split(',')\n    print(s)\n    d,result = {},&#91;]\n    for num in s:\n        sumNum = sum(map(int, num))\n        print(\"sumNim=\",sumNum)    \n        if sumNum in d:\n            d&#91;sumNum] += &#91;num]\n            print(sumNum,d&#91;sumNum])\n        else:\n            d&#91;sumNum] = &#91;num]\n            print(sumNum,d&#91;sumNum])\n\n    # \u66f4Pythonic\u7684\u5199\u6cd5\u4e3a\uff1a\n    #d&#91;sumNum] = d.get(sumNum, &#91;]) + &#91;num]\n    print(\"d:\",*d)\n    lst = sorted(d.items())  # key = lambda d:d&#91;0]\u4e3a\u9ed8\u8ba4\u60c5\u51b5\uff0c\u53ef\u7701\u7565\n    print(\"lst:\",*lst)\n    result = map(sorted, &#91;x&#91;1] for x in lst])\n    return result\n\ns = '143,267,342,562,224,134,276,252'\nresult = findNumFriends(s)\nprint(*result, sep='\\n')    # *result\u7528\u4e8e\u89e3\u5305\n\n\n\n'''\n\u5bf9\u4e8e\u8fd9\u4e2a\u95ee\u9898\u5b9a\u4e49\u4e00\u4e2a\u5904\u7406\u51fd\u6570\uff0c\u5176\u601d\u8def\u53ef\u4ee5\u8003\u8651\u5982\u4e0b\uff1a\n\u5904\u7406\u6570\u636e\uff1a\u7528\u5b57\u7b26\u4e32\u7684split()\u65b9\u6cd5\u5c06\u6570\u5b57\u5b57\u7b26\u5206\u9694\u540e\u5b58\u5982\u5217\u8868\u4e2d\uff0c\u5982\u679c\u60f3\u8f6c\u6362\u6210int\u7c7b\u578b\u53ef\u4ee5\u7528map()\u51fd\u6570\u8f85\u52a9\u5904\u7406\uff1b\n1\ns = s.split(',')\n2. \u7edf\u8ba1\uff1a\u5229\u7528Python\u4e2dfor\u5faa\u73af\u7b80\u6d01\u6709\u6548\u7684\u5e8f\u5217\u9879\u8fed\u4ee3\u601d\u8def\u904d\u5386\u6bcf\u4e00\u4e2a\u6570\u5b57\u5b57\u7b26\uff0c\u5e76\u5bf9\u6bcf\u4e00\u4e2a\u6570\u5b57\u5b57\u7b26\u8fdb\u884c\u548c\u7684\u8ba1\u7b97\uff0c\u8fd9\u91cc\u7528\u4e24\u4e2afor\u5faa\u73af\u5373\u53ef\uff1b\nfor num in s: \n      sumNum = sum(map(int, num))\n3. \u4fdd\u5b58\u7edf\u8ba1\u7ed3\u679c\uff1a\u56e0\u4e3a\u540c\u4e00\u4e2a\u6570\u5b57\u548c\u53ef\u80fd\u5bf9\u5e94\u591a\u4e2a\u6570\u5b57\u5b57\u7b26\u4e32\uff0c\u6240\u4ee5\u53ef\u4ee5\u5229\u7528\u5b57\u5178\u7684\u6620\u5c04\u7279\u5f81\u5c06\u7ed3\u679c\u5b58\u5165\u5b57\u5178\u4e2d\uff1b\nd, result = {}, &#91;]\nif sumNum in d:\n              d&#91;sumNum] += &#91;num]\n        else:\n              d&#91;sumNum] = &#91;num]\n# \u66f4Pythonic\u7684\u5199\u6cd5\u4e3a\uff1a\nd&#91;sumNum] = d.get(sumNum, &#91;]) + &#91;num]\n4.\u6392\u5e8f\uff1a\u6ce8\u610f\u6b64\u5904\u6709\u4e24\u4e2a\u6392\u5e8f\uff0c\u4e00\u662f\u57fa\u4e8e\u6570\u5b57\u548c\u6392\u5e8f\uff0c\u4e8c\u662f\u540c\u4e00\u6570\u5b57\u548c\u5404\u6570\u5b57\u7684\u6392\u5e8f\uff0c\u6b64\u5904\u5e94\u8003\u8651\u591a\u7528Python\u63d0\u4f9b\u7684\u51fd\u6570\u3002\u9488\u5bf9\u6d4b\u8bd5\u6570\u636e\uff0c\n\u5b57\u5178d\u4e2d\u7684\u6570\u636e\u5f62\u5982\uff1a\n{8: &#91;'143', '224', '134'], 15: &#91;'267', '276'], 9: &#91;'342', '252'], 13: &#91;'562']}\n\n\u57fa\u4e8e\u6570\u5b57\u548c\u7684\u6392\u5e8f\u53ef\u4ee5\u4f7f\u7528sorted()\u51fd\u6570\uff1a\nlst = sorted(d.items())  # key = lambda d:d&#91;0]\u4e3a\u9ed8\u8ba4\u60c5\u51b5\uff0c\u53ef\u7701\u7565\n\u6392\u5e8f\u5b8c\u540elst\u4e2d\u7684\u7ed3\u679c\u5f62\u5982\uff1a\n&#91;(8, &#91;'143', '224', '134']), (9, &#91;'342', '252']), (13, &#91;'562']), (15, &#91;'267', '276'])]\n\n\u8981\u63a5\u7740\u5bf9\u6bcf\u4e00\u4e2a\u503c\uff08\u591a\u4e2a\u5217\u8868\uff09\u8fdb\u884c\u6392\u5e8f\uff0c\u6b64\u5904\u53ef\u4ee5\u663e\u5f0f\u5730\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\uff0c\u66f4Python\u5316\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u9690\u5c04\u51fd\u6570map()\uff0c\n\u5c06sorted\u51fd\u6570\u4f5c\u7528\u5728lst\u7684\u6bcf\u4e00\u4e2a\u952e\u5bf9\u5e94\u7684\u503c\u4e0a\uff0c\u53ef\u7528\u5982\u4e0b\u4ee3\u7801\uff1a\nresult = map(sorted, &#91;x&#91;1] for x in lst])\n\u8fd9\u6837\u5b9e\u73b0\u66f4\u4fbf\u6377\u3002\u6700\u540e\u51fd\u6570\u5c06\u7ed3\u679c\u8fd4\u56de\u5373\u53ef\u3002\u5728\u4e3b\u6a21\u5757\u4e2d\u8f93\u5165s\u5e76\u4f20\u9012\u7ed9\u5f62\u53c2s\uff0c\u8c03\u7528\u51fd\u6570\u5373\u53ef\u5f97\u5230\u6700\u7ec8\u7684\u7ed3\u679c\u3002\n\nresult = findNumFriends(s)\nprint(*result, sep='\\n')    # *result\u7528\u4e8e\u89e3\u5305\n\n\u901a\u8fc7\u5982\u4e0a\u8fd9\u4e2a\u4f8b\u5b50\u5927\u5bb6\u662f\u4e0d\u662f\u8fdb\u4e00\u6b65\u7406\u89e3\u4e86Pythonic\u7684\u7f16\u7a0b\u601d\u60f3\u4e86\u5462\uff1f\n\u5728\u65e5\u5e38\u7684\u4f7f\u7528\u4e2d\u4e00\u5b9a\u8981\u591a\u591a\u53bb\u601d\u8003\u548c\u4f7f\u7528\uff0c\u4e5f\u53ef\u4ee5\u591a\u638c\u63e1\u4e00\u4e9b\u6807\u51c6\u5e93\u7684\u4f7f\u7528\uff0c\n\u4f8b\u5982\u5047\u8bbe\u8981\u5224\u65ad\u67d0\u4e00\u4e2a\u5b57\u7b26\u662f\u5426\u662f\u5b57\u6bcd\uff0c\u9664\u4e86\u501f\u52a9\u5185\u5efa\u7684\u51fd\u6570\/\u65b9\u6cd5\u548cASCII\u503c\u5f97\u8f6c\u6362\u5916\u8fd8\u53ef\u4ee5\u501f\u52a9string\u6a21\u5757\u6765\u5b8c\u6210\uff1a\n'''\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,10],"tags":[],"class_list":["post-2541","post","type-post","status-publish","format-standard","hentry","category-2","category-python"],"_links":{"self":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/2541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2541"}],"version-history":[{"count":0,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/2541\/revisions"}],"wp:attachment":[{"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2541"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/g1n29wqq.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}