program tip

천 단위 구분 기호로 쉼표로 숫자를 인쇄하는 방법은 무엇입니까?

radiobox 2020. 10. 2. 21:57
반응형

천 단위 구분 기호로 쉼표로 숫자를 인쇄하는 방법은 무엇입니까?


파이썬 2.6.1 에서 쉼표를 천 단위 구분 기호로 사용 하여 정수를 인쇄하려고합니다 . 예를 들어 숫자 12345671,234,567. 어떻게하면 되나요? Google에서 많은 예를 보았지만 가장 간단한 실용적인 방법을 찾고 있습니다.

마침표와 쉼표 사이를 결정하기 위해 로케일별로 다를 필요는 없습니다. 나는 합리적으로 가능한 한 단순한 것을 선호합니다.


로케일을 인식하지 못함

'{:,}'.format(value)  # For Python ≥2.7
f'{value:,}'  # For Python ≥3.7

로케일 인식

import locale
locale.setlocale(locale.LC_ALL, '')  # Use '' for auto, or force e.g. to 'en_US.UTF-8'

'{:n}'.format(value)  # For Python ≥2.7
f'{value:n}'  # For Python ≥3.7

참고

형식 사양 미니 언어 ,

','옵션은 천 단위 구분 기호에 쉼표를 사용함을 나타냅니다. 로케일 인식 구분 기호의 경우 'n'대신 정수 표시 유형을 사용하십시오.


나는 이것을 작동시켰다.

>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US')
'en_US'
>>> locale.format("%d", 1255000, grouping=True)
'1,255,000'

물론 국제화 지원 필요 하지 않지만 명확하고 간결하며 기본 제공 라이브러리를 사용합니다.

추신 : "% d"는 일반적인 % 스타일 포맷터입니다. 포맷터는 하나만 가질 수 있지만 필드 너비 및 정밀도 설정 측면에서 필요한 것은 무엇이든 될 수 있습니다.

PPS locale일할 수 없다면 Mark의 대답을 수정 한 버전을 제안합니다.

def intWithCommas(x):
    if type(x) not in [type(0), type(0L)]:
        raise TypeError("Parameter must be an integer.")
    if x < 0:
        return '-' + intWithCommas(-x)
    result = ''
    while x >= 1000:
        x, r = divmod(x, 1000)
        result = ",%03d%s" % (r, result)
    return "%d%s" % (x, result)

재귀는 부정적인 경우에 유용하지만 쉼표 당 하나의 재귀는 나에게 약간 과도하게 보입니다.


비효율 성과 가독성 때문에 이길 수 없습니다.

>>> import itertools
>>> s = '-1234567'
>>> ','.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('-,','-')

다음은 관련없는 부분을 제거하고 약간 정리 한 후 로케일 그룹화 코드입니다.

(다음은 정수에서만 작동합니다)

def group(number):
    s = '%d' % number
    groups = []
    while s and s[-1].isdigit():
        groups.append(s[-3:])
        s = s[:-3]
    return s + ','.join(reversed(groups))

>>> group(-23432432434.34)
'-23,432,432,434'

여기에 이미 좋은 답변이 있습니다. 나중에 참조 할 수 있도록 추가하고 싶습니다. 파이썬 2.7에는 천 단위 구분자에 대한 형식 지정자가있을 것입니다. 파이썬 문서 에 따르면 다음 과 같이 작동합니다.

>>> '{:20,.2f}'.format(f)
'18,446,744,073,709,551,616.00'

python3.1에서 다음과 같이 동일한 작업을 수행 할 수 있습니다.

>>> format(1234567, ',d')
'1,234,567'

파이썬 3.6에서 f- 문자열로 다음과 같이 쉽게 할 수 있다고 아무도 언급하지 않았다는 것에 놀랐습니다.

>>> num = 10000000
>>> print(f"{num:,d}")
10,000,000

... 여기서 콜론 뒤의 부분은 형식 지정자입니다. 쉼표는 원하는 구분 문자이므로 f"{num:_d}"쉼표 대신 밑줄을 사용합니다.

이는 format(num, ",d")이전 버전의 Python 3 에서 사용하는 것과 동일 합니다.


다음은 한 줄 정규식 대체입니다.

re.sub("(\d)(?=(\d{3})+(?!\d))", r"\1,", "%d" % val)

inegral 출력에 대해서만 작동합니다.

import re
val = 1234567890
re.sub("(\d)(?=(\d{3})+(?!\d))", r"\1,", "%d" % val)
# Returns: '1,234,567,890'

val = 1234567890.1234567890
# Returns: '1,234,567,890'

또는 4 자리 미만의 부동 소수점의 경우 형식 지정자를 %.3f다음 으로 변경하십시오 .

re.sub("(\d)(?=(\d{3})+(?!\d))", r"\1,", "%.3f" % val)
# Returns: '1,234,567,890.123'

주의 : 소수점 이하 자릿수를 그룹화하려고 시도하기 때문에 3 자리 이상의 소수점은 올바르게 작동하지 않습니다.

re.sub("(\d)(?=(\d{3})+(?!\d))", r"\1,", "%.5f" % val)
# Returns: '1,234,567,890.12,346'

작동 원리

그것을 분해 해보자 :

re.sub(pattern, repl, string)

pattern = \
    "(\d)           # Find one digit...
     (?=            # that is followed by...
         (\d{3})+   # one or more groups of three digits...
         (?!\d)     # which are not followed by any more digits.
     )",

repl = \
    r"\1,",         # Replace that one digit by itself, followed by a comma,
                    # and continue looking for more matches later in the string.
                    # (re.sub() replaces all matches it finds in the input)

string = \
    "%d" % val      # Format the string as a decimal to begin with

이것이 제가 수레를 위해하는 일입니다. 솔직히 어떤 버전에서 작동하는지 잘 모르겠지만 2.7을 사용하고 있습니다.

my_number = 4385893.382939491

my_string = '{:0,.2f}'.format(my_number)

반환 : 4,385,893.38

업데이트 : 최근에이 형식에 문제가 발생했습니다 (정확한 이유를 말할 수 없음). 다음을 삭제하여 수정할 수있었습니다 0.

my_string = '{:,.2f}'.format(my_number)

'{:n}'.format( value )로케일 표현 에도 사용할 수 있습니다 . 이것이 로케일 솔루션을위한 가장 간단한 방법이라고 생각합니다.

자세한 내용 thousandsPython DOC 에서 검색하십시오 .

통화의 locale.currency경우 플래그를 설정하여을 사용할 수 있습니다 grouping.

암호

import locale

locale.setlocale( locale.LC_ALL, '' )
locale.currency( 1234567.89, grouping = True )

산출

'Portuguese_Brazil.1252'
'R$ 1.234.567,89'

이것에 대한 표준 라이브러리 함수가 있어야한다고 확신하지만 재귀를 사용하여 직접 작성하는 것이 재미 있었으므로 여기에 내가 생각 해낸 것입니다.

def intToStringWithCommas(x):
    if type(x) is not int and type(x) is not long:
        raise TypeError("Not an integer!")
    if x < 0:
        return '-' + intToStringWithCommas(-x)
    elif x < 1000:
        return str(x)
    else:
        return intToStringWithCommas(x / 1000) + ',' + '%03d' % (x % 1000)

다른 사람이 표준 방법을 찾으면 대신 사용해야합니다.


Ian Schneider의 답변을 약간 확장 :

사용자 지정 천 단위 구분 기호를 사용하려는 경우 가장 간단한 솔루션은 다음과 같습니다.

'{:,}'.format(value).replace(',', your_custom_thousands_separator)

'{:,.2f}'.format(123456789.012345).replace(',', ' ')

이와 같은 독일어 표현을 원하면 조금 더 복잡해집니다.

('{:,.2f}'.format(123456789.012345)
          .replace(',', ' ')  # 'save' the thousands separators 
          .replace('.', ',')  # dot to comma
          .replace(' ', '.')) # thousand separators to dot

댓글 에서 activestate 레시피 498181 까지 다음과 같이 수정했습니다.

import re
def thous(x, sep=',', dot='.'):
    num, _, frac = str(x).partition(dot)
    num = re.sub(r'(\d{3})(?=\d)', r'\1'+sep, num[::-1])[::-1]
    if frac:
        num += dot + frac
    return num

그것은 정규 표현식 기능을 사용 내다(?=\d)그들에 쉼표가 얻을 '후'숫자를 세 자리의 확인 만 그룹을 만들 수 있습니다. 이 시점에서 문자열이 반대이기 때문에 'after'라고 말합니다.

[::-1] 그냥 문자열을 뒤집습니다.


파이썬 3

-

정수 (소수 없음) :

"{:,d}".format(1234567)

-

부동 소수점 (소수점 포함) :

"{:,.2f}".format(1234567)

여기서 앞 f의 숫자는 소수점 이하 자릿수 지정합니다.

-

보너스

Indian lakhs / crores 넘버링 시스템 (12,34,567)을위한 빠르고 더러운 스타터 기능 :

https://stackoverflow.com/a/44832241/4928578


받아 들여지는 대답은 괜찮지 만 실제로는 format(number,','). 더 쉽게 해석하고 기억할 수 있습니다.

https://docs.python.org/3/library/functions.html#format


Python 버전 2.6에서 다음을 수행 할 수 있습니다.

def format_builtin(n):
    return format(n, ',')

Python 버전 <2.6 및 참고 용으로 다음 두 가지 수동 솔루션이 있습니다. 부동 소수점을 정수로 바꾸지 만 음수는 올바르게 작동합니다.

def format_number_using_lists(number):
    string = '%d' % number
    result_list = list(string)
    indexes = range(len(string))
    for index in indexes[::-3][1:]:
        if result_list[index] != '-':
            result_list.insert(index+1, ',')
    return ''.join(result_list)

여기서 주목해야 할 몇 가지 사항 :

  • 이 줄 : string = '% d'% number 는 숫자를 문자열로 아름답게 변환하고, 음수를 지원하며, 부동 소수점에서 분수를 삭제하여 정수로 만듭니다.
  • 이 슬라이스 인덱스 [::-3] 는 끝부터 시작하여 각 세 번째 항목을 반환하므로 마지막 항목을 제거하기 위해 다른 슬라이스 [1 :] 을 사용했습니다. 마지막 숫자 뒤에 쉼표가 필요하지 않기 때문입니다.
  • l [index]! = '-' 가 음수를 지원하는 데 사용되는 경우이 조건 은 빼기 기호 뒤에 쉼표를 삽입하지 마십시오.

그리고 더 하드 코어 버전 :

def format_number_using_generators_and_list_comprehensions(number):
    string = '%d' % number
    generator = reversed( 
        [
            value+',' if (index!=0 and value!='-' and index%3==0) else value
            for index,value in enumerate(reversed(string))
        ]
    )
    return ''.join(generator)

저는 Python 초보자이지만 숙련 된 프로그래머입니다. 저는 Python 3.5를 가지고 있으므로 쉼표 만 사용할 수 있지만 그럼에도 불구하고 이것은 흥미로운 프로그래밍 연습입니다. 부호없는 정수의 경우를 고려하십시오. 천 단위 구분 기호를 추가하는 가장 읽기 쉬운 Python 프로그램은 다음과 같습니다.

def add_commas(instr):
    out = [instr[0]]
    for i in range(1, len(instr)):
        if (len(instr) - i) % 3 == 0:
            out.append(',')
        out.append(instr[i])
    return ''.join(out)

목록 이해력을 사용할 수도 있습니다.

add_commas(instr):
    rng = reversed(range(1, len(instr) + (len(instr) - 1)//3 + 1))
    out = [',' if j%4 == 0 else instr[-(j - j//4)] for j in rng]
    return ''.join(out)

이것은 더 짧고 하나의 라이너가 될 수 있지만 그것이 작동하는 이유를 이해하려면 정신 체조를해야합니다. 두 경우 모두 다음을 얻습니다.

for i in range(1, 11):
    instr = '1234567890'[:i]
    print(instr, add_commas(instr))
1 1
12 12
123 123
1234 1,234
12345 12,345
123456 123,456
1234567 1,234,567
12345678 12,345,678
123456789 123,456,789
1234567890 1,234,567,890

프로그램을 이해하고 싶다면 첫 번째 버전이 더 현명한 선택입니다.


다음은 수레에서도 작동하는 것입니다.

def float2comma(f):
    s = str(abs(f)) # Convert to a string
    decimalposition = s.find(".") # Look for decimal point
    if decimalposition == -1:
        decimalposition = len(s) # If no decimal, then just work from the end
    out = "" 
    for i in range(decimalposition+1, len(s)): # do the decimal
        if not (i-decimalposition-1) % 3 and i-decimalposition-1: out = out+","
        out = out+s[i]      
    if len(out):
        out = "."+out # add the decimal point if necessary
    for i in range(decimalposition-1,-1,-1): # working backwards from decimal point
        if not (decimalposition-i-1) % 3 and decimalposition-i-1: out = ","+out
        out = s[i]+out      
    if f < 0:
        out = "-"+out
    return out

사용 예 :

>>> float2comma(10000.1111)
'10,000.111,1'
>>> float2comma(656565.122)
'656,565.122'
>>> float2comma(-656565.122)
'-656,565.122'

Python 2.5+ 및 Python 3 용 라이너 1 개 (positive int 전용) :

''.join(reversed([x + (',' if i and not i % 3 else '') for i, x in enumerate(reversed(str(1234567)))]))

이것은 쉼표와 함께 돈을

def format_money(money, presym='$', postsym=''):
    fmt = '%0.2f' % money
    dot = string.find(fmt, '.')
    ret = []
    if money < 0 :
        ret.append('(')
        p0 = 1
    else :
        p0 = 0
    ret.append(presym)
    p1 = (dot-p0) % 3 + p0
    while True :
        ret.append(fmt[p0:p1])
        if p1 == dot : break
        ret.append(',')
        p0 = p1
        p1 += 3
    ret.append(fmt[dot:])   # decimals
    ret.append(postsym)
    if money < 0 : ret.append(')')
    return ''.join(ret)

이 코드의 python 2 및 python 3 버전이 있습니다. 나는 그 질문이 파이썬 2에 대해 요청되었다는 것을 알고 있지만 이제 (8 년 후) 사람들은 아마도

파이썬 3을 사용할 것입니다. 파이썬 3 코드 :

import random
number = str(random.randint(1, 10000000))
comma_placement = 4
print('The original number is: {}. '.format(number))
while True:
    if len(number) % 3 == 0:
        for i in range(0, len(number) // 3 - 1):
            number = number[0:len(number) - comma_placement + 1] + ',' + number[len(number) - comma_placement + 1:]
            comma_placement = comma_placement + 4
    else:
        for i in range(0, len(number) // 3):
            number = number[0:len(number) - comma_placement + 1] + ',' + number[len(number) - comma_placement + 1:]
    break
print('The new and improved number is: {}'.format(number))        


Python 2 코드 : (편집. python 2 코드가 작동하지 않습니다. 구문이 다른 것 같습니다.)

import random
number = str(random.randint(1, 10000000))
comma_placement = 4
print 'The original number is: %s.' % (number)
while True:
    if len(number) % 3 == 0:
        for i in range(0, len(number) // 3 - 1):
            number = number[0:len(number) - comma_placement + 1] + ',' + number[len(number) - comma_placement + 1:]
            comma_placement = comma_placement + 4
    else:
        for i in range(0, len(number) // 3):
            number = number[0:len(number) - comma_placement + 1] + ',' + number[len(number) - comma_placement + 1:]
    break
print 'The new and improved number is: %s.' % (number) 

python 2.5를 사용하고 있으므로 기본 제공 형식에 액세스 할 수 없습니다.

Django 코드 intcomma (아래 코드에서 intcomma_recurs)를 살펴보고 재귀 적이며 모든 실행에서 정규식을 컴파일하는 것도 좋지 않기 때문에 비효율적이라는 것을 깨달았습니다. django는 실제로 이런 종류의 저수준 성능에 초점을 맞추지 않기 때문에 '문제'가 필요하지 않습니다. 또한 성능 차이가 10 배나 될 것으로 예상했지만 3 배 정도 느립니다.

호기심 때문에 몇 가지 버전의 intcomma를 구현하여 regex를 사용할 때 성능 이점이 무엇인지 확인했습니다. 내 테스트 데이터는이 작업에 약간의 이점이 있다고 결론 지지만 놀랍게도 그다지 많지 않습니다.

나는 또한 내가 의심 한 것을보고 기뻤다. 정규식이없는 경우에는 역 xrange 접근 방식을 사용할 필요가 없지만 ~ 10 % 성능의 비용으로 코드가 약간 더 좋아 보인다.

또한 전달하는 것이 문자열이고 다소 숫자처럼 보인다고 가정합니다. 그렇지 않으면 결과가 결정되지 않습니다.

from __future__ import with_statement
from contextlib import contextmanager
import re,time

re_first_num = re.compile(r"\d")
def intcomma_noregex(value):
    end_offset, start_digit, period = len(value),re_first_num.search(value).start(),value.rfind('.')
    if period == -1:
        period=end_offset
    segments,_from_index,leftover = [],0,(period-start_digit) % 3
    for _index in xrange(start_digit+3 if not leftover else start_digit+leftover,period,3):
        segments.append(value[_from_index:_index])
        _from_index=_index
    if not segments:
        return value
    segments.append(value[_from_index:])
    return ','.join(segments)

def intcomma_noregex_reversed(value):
    end_offset, start_digit, period = len(value),re_first_num.search(value).start(),value.rfind('.')
    if period == -1:
        period=end_offset
    _from_index,segments = end_offset,[]
    for _index in xrange(period-3,start_digit,-3):
        segments.append(value[_index:_from_index])
        _from_index=_index
    if not segments:
        return value
    segments.append(value[:_from_index])
    return ','.join(reversed(segments))

re_3digits = re.compile(r'(?<=\d)\d{3}(?!\d)')
def intcomma(value):
    segments,last_endoffset=[],len(value)
    while last_endoffset > 3:
        digit_group = re_3digits.search(value,0,last_endoffset)
        if not digit_group:
            break
        segments.append(value[digit_group.start():last_endoffset])
        last_endoffset=digit_group.start()
    if not segments:
        return value
    if last_endoffset:
        segments.append(value[:last_endoffset])
    return ','.join(reversed(segments))

def intcomma_recurs(value):
    """
    Converts an integer to a string containing commas every three digits.
    For example, 3000 becomes '3,000' and 45000 becomes '45,000'.
    """
    new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', str(value))
    if value == new:
        return new
    else:
        return intcomma(new)

@contextmanager
def timed(save_time_func):
    begin=time.time()
    try:
        yield
    finally:
        save_time_func(time.time()-begin)

def testset_xsimple(func):
    func('5')

def testset_simple(func):
    func('567')

def testset_onecomma(func):
    func('567890')

def testset_complex(func):
    func('-1234567.024')

def testset_average(func):
    func('-1234567.024')
    func('567')
    func('5674')

if __name__ == '__main__':
    print 'Test results:'
    for test_data in ('5','567','1234','1234.56','-253892.045'):
        for func in (intcomma,intcomma_noregex,intcomma_noregex_reversed,intcomma_recurs):
            print func.__name__,test_data,func(test_data)
    times=[]
    def overhead(x):
        pass
    for test_run in xrange(1,4):
        for func in (intcomma,intcomma_noregex,intcomma_noregex_reversed,intcomma_recurs,overhead):
            for testset in (testset_xsimple,testset_simple,testset_onecomma,testset_complex,testset_average):
                for x in xrange(1000): # prime the test
                    testset(func)
                with timed(lambda x:times.append(((test_run,func,testset),x))):
                    for x in xrange(50000):
                        testset(func)
    for (test_run,func,testset),_delta in times:
        print test_run,func.__name__,testset.__name__,_delta

다음은 테스트 결과입니다.

intcomma 5 5
intcomma_noregex 5 5
intcomma_noregex_reversed 5 5
intcomma_recurs 5 5
intcomma 567 567
intcomma_noregex 567 567
intcomma_noregex_reversed 567 567
intcomma_recurs 567 567
intcomma 1234 1,234
intcomma_noregex 1234 1,234
intcomma_noregex_reversed 1234 1,234
intcomma_recurs 1234 1,234
intcomma 1234.56 1,234.56
intcomma_noregex 1234.56 1,234.56
intcomma_noregex_reversed 1234.56 1,234.56
intcomma_recurs 1234.56 1,234.56
intcomma -253892.045 -253,892.045
intcomma_noregex -253892.045 -253,892.045
intcomma_noregex_reversed -253892.045 -253,892.045
intcomma_recurs -253892.045 -253,892.045
1 intcomma testset_xsimple 0.0410001277924
1 intcomma testset_simple 0.0369999408722
1 intcomma testset_onecomma 0.213000059128
1 intcomma testset_complex 0.296000003815
1 intcomma testset_average 0.503000020981
1 intcomma_noregex testset_xsimple 0.134000062943
1 intcomma_noregex testset_simple 0.134999990463
1 intcomma_noregex testset_onecomma 0.190999984741
1 intcomma_noregex testset_complex 0.209000110626
1 intcomma_noregex testset_average 0.513000011444
1 intcomma_noregex_reversed testset_xsimple 0.124000072479
1 intcomma_noregex_reversed testset_simple 0.12700009346
1 intcomma_noregex_reversed testset_onecomma 0.230000019073
1 intcomma_noregex_reversed testset_complex 0.236999988556
1 intcomma_noregex_reversed testset_average 0.56299996376
1 intcomma_recurs testset_xsimple 0.348000049591
1 intcomma_recurs testset_simple 0.34600019455
1 intcomma_recurs testset_onecomma 0.625
1 intcomma_recurs testset_complex 0.773999929428
1 intcomma_recurs testset_average 1.6890001297
1 overhead testset_xsimple 0.0179998874664
1 overhead testset_simple 0.0190000534058
1 overhead testset_onecomma 0.0190000534058
1 overhead testset_complex 0.0190000534058
1 overhead testset_average 0.0309998989105
2 intcomma testset_xsimple 0.0360000133514
2 intcomma testset_simple 0.0369999408722
2 intcomma testset_onecomma 0.207999944687
2 intcomma testset_complex 0.302000045776
2 intcomma testset_average 0.523000001907
2 intcomma_noregex testset_xsimple 0.139999866486
2 intcomma_noregex testset_simple 0.141000032425
2 intcomma_noregex testset_onecomma 0.203999996185
2 intcomma_noregex testset_complex 0.200999975204
2 intcomma_noregex testset_average 0.523000001907
2 intcomma_noregex_reversed testset_xsimple 0.130000114441
2 intcomma_noregex_reversed testset_simple 0.129999876022
2 intcomma_noregex_reversed testset_onecomma 0.236000061035
2 intcomma_noregex_reversed testset_complex 0.241999864578
2 intcomma_noregex_reversed testset_average 0.582999944687
2 intcomma_recurs testset_xsimple 0.351000070572
2 intcomma_recurs testset_simple 0.352999925613
2 intcomma_recurs testset_onecomma 0.648999929428
2 intcomma_recurs testset_complex 0.808000087738
2 intcomma_recurs testset_average 1.81900000572
2 overhead testset_xsimple 0.0189998149872
2 overhead testset_simple 0.0189998149872
2 overhead testset_onecomma 0.0190000534058
2 overhead testset_complex 0.0179998874664
2 overhead testset_average 0.0299999713898
3 intcomma testset_xsimple 0.0360000133514
3 intcomma testset_simple 0.0360000133514
3 intcomma testset_onecomma 0.210000038147
3 intcomma testset_complex 0.305999994278
3 intcomma testset_average 0.493000030518
3 intcomma_noregex testset_xsimple 0.131999969482
3 intcomma_noregex testset_simple 0.136000156403
3 intcomma_noregex testset_onecomma 0.192999839783
3 intcomma_noregex testset_complex 0.202000141144
3 intcomma_noregex testset_average 0.509999990463
3 intcomma_noregex_reversed testset_xsimple 0.125999927521
3 intcomma_noregex_reversed testset_simple 0.126999855042
3 intcomma_noregex_reversed testset_onecomma 0.235999822617
3 intcomma_noregex_reversed testset_complex 0.243000030518
3 intcomma_noregex_reversed testset_average 0.56200003624
3 intcomma_recurs testset_xsimple 0.337000131607
3 intcomma_recurs testset_simple 0.342000007629
3 intcomma_recurs testset_onecomma 0.609999895096
3 intcomma_recurs testset_complex 0.75
3 intcomma_recurs testset_average 1.68300008774
3 overhead testset_xsimple 0.0189998149872
3 overhead testset_simple 0.018000125885
3 overhead testset_onecomma 0.018000125885
3 overhead testset_complex 0.0179998874664
3 overhead testset_average 0.0299999713898

이것은 PEP에 따라 파이썬으로 구워집니다-> https://www.python.org/dev/peps/pep-0378/

천 단위 구분 기호로 정수를 표시하려면 format (1000, ', d') 만 사용하십시오.

PEP에 설명 된 더 많은 형식이 있습니다.


다음은 정수에 대해 작동하는 생성기 함수를 사용하는 또 다른 변형입니다.

def ncomma(num):
    def _helper(num):
        # assert isinstance(numstr, basestring)
        numstr = '%d' % num
        for ii, digit in enumerate(reversed(numstr)):
            if ii and ii % 3 == 0 and digit.isdigit():
                yield ','
            yield digit

    return ''.join(reversed([n for n in _helper(num)]))

그리고 여기에 테스트가 있습니다.

>>> for i in (0, 99, 999, 9999, 999999, 1000000, -1, -111, -1111, -111111, -1000000):
...     print i, ncomma(i)
... 
0 0
99 99
999 999
9999 9,999
999999 999,999
1000000 1,000,000
-1 -1
-111 -111
-1111 -1,111
-111111 -111,111
-1000000 -1,000,000

하위 클래스 long(또는 float또는 무엇이든). 이렇게하면 수학 연산 (및 기존 코드)에서 숫자를 계속 사용할 수 있지만 터미널에서 모두 멋지게 인쇄되므로 매우 실용적입니다.

>>> class number(long):

        def __init__(self, value):
            self = value

        def __repr__(self):
            s = str(self)
            l = [x for x in s if x in '1234567890']
            for x in reversed(range(len(s)-1)[::3]):
                l.insert(-x, ',')
            l = ''.join(l[1:])
            return ('-'+l if self < 0 else l) 

>>> number(-100000)
-100,000
>>> number(-100)
-100
>>> number(-12345)
-12,345
>>> number(928374)
928,374
>>> 345

이탈리아:

>>> import locale
>>> locale.setlocale(locale.LC_ALL,"")
'Italian_Italy.1252'
>>> f"{1000:n}"
'1.000'

수레의 경우 :

float(filter(lambda x: x!=',', '1,234.52'))
# returns 1234.52

int의 경우 :

int(filter(lambda x: x!=',', '1,234'))
# returns 1234

참고 URL : https://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators

반응형