{"id":2402,"date":"2015-11-07T15:15:22","date_gmt":"2015-11-07T07:15:22","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2402"},"modified":"2015-11-07T15:15:22","modified_gmt":"2015-11-07T07:15:22","slug":"%e5%9c%a8python%e4%b8%ad%e5%af%b9%e6%95%b4%e6%95%b0%e7%94%a8%e5%8d%83%e5%88%86%e4%bd%8d%e8%a1%a8%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2402.html","title":{"rendered":"\u5728Python\u4e2d\u5bf9\u6574\u6570\u7528\u5343\u5206\u4f4d\u8868\u793a"},"content":{"rendered":"<p>\u300e\u5373\u6bcf\u9694\u4e09\u4f4d\u6570\u5b57\u52a0\u4e00\u4e2a\u9017\u53f7\u300f<\/p>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<ul>\n<li>python integer Thousand points representation<\/li>\n<li>python Thousand points representation<\/li>\n<\/ul>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/1823058\/how-to-print-number-with-commas-as-thousands-separators\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/1823058\/how-to-print-number-with-commas-as-thousands-separators<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/3909457\/whats-the-easiest-way-to-add-commas-to-an-integer-in-python\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/3909457\/whats-the-easiest-way-to-add-commas-to-an-integer-in-python<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/13082620\/how-can-i-print-a-float-with-thousands-separators\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/13082620\/how-can-i-print-a-float-with-thousands-separators<\/a><\/li>\n<\/ul>\n<h6>\u53c2\u8003\u89e3\u7b54\uff1a<\/h6>\n<p>I too, prefer the &#8220;simplest practical way&#8221;. For &gt;= 2.7:<\/p>\n<pre class=\"lang:default decode:true \">\"{:,}\".format(value)\n\n#\u4e3e\u4f8b\u5982\u4e0b\nIn [1]: aint = 9876543201\n\nIn [2]: type(aint), aint\nOut[2]: (int, 9876543201)\n\nIn [3]: \"{:,}\".format(aint)\nOut[3]: '9,876,543,201'<\/pre>\n<p><a style=\"line-height: 1.5;\" href=\"http:\/\/docs.python.org\/library\/string.html#format-specification-mini-language\">http:\/\/docs.python.org\/library\/string.html#format-specification-mini-language<\/a><\/p>\n<p>==<\/p>\n<p>I got this to work:<\/p>\n<pre class=\"lang:default decode:true\">&gt;&gt;&gt; import locale\n&gt;&gt;&gt; locale.setlocale(locale.LC_ALL, 'en_US')\n'en_US'\n&gt;&gt;&gt; locale.format(\"%d\", 1255000, grouping=True)\n'1,255,000'<\/pre>\n<p>Sure, you don&#8217;t\u00a0need\u00a0internationalization support, but it&#8217;s clear, concise, and uses a built-in library.<\/p>\n<p>P.S. That &#8220;%d&#8221; is the usual %-style formatter. You can have only one formatter, but it can be whatever you need in terms of field width and precision settings.<\/p>\n<p>P.P.S. If you can&#8217;t get\u00a0locale\u00a0to work, I&#8217;d suggest a modified version of Mark&#8217;s answer:<\/p>\n<pre class=\"lang:default decode:true\">def intWithCommas(x):\n    if type(x) not in [type(0), type(0L)]:\n        raise TypeError(\"Parameter must be an integer.\")\n    if x &lt; 0:\n        return '-' + intWithCommas(-x)\n    result = ''\n    while x &gt;= 1000:\n        x, r = divmod(x, 1000)\n        result = \",%03d%s\" % (r, result)\n    return \"%d%s\" % (x, result)<\/pre>\n<p>Recursion is useful for the negative case, but one recursion per comma seems a bit excessive to me.<\/p>\n<p>==<\/p>\n<p>For inefficiency and unreadability it&#8217;s hard to beat:<\/p>\n<pre class=\"lang:default decode:true \">&gt;&gt;&gt; import itertools\n&gt;&gt;&gt; s = '-1234567'\n&gt;&gt;&gt; ','.join([\"%s%s%s\" % (x[0], x[1] or '', x[2] or '') for x in itertools.izip_longest(s[::-1][::3], s[::-1][1::3], s[::-1][2::3])])[::-1].replace('-,','-')<\/pre>\n<p>=EOF=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u300e\u5373\u6bcf\u9694\u4e09\u4f4d\u6570\u5b57\u52a0\u4e00\u4e2a\u9017\u53f7\u300f \u641c\u7d22\u5173\u952e\u5b57\uff1a python integer Thousand points re [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[8,19],"class_list":["post-2402","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-python","tag-tips"],"views":4406,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2402"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2402\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}