用逗号分隔大量数字

时间:2019-06-19 18:51:12

标签: python python-3.x

我刚刚完成了一些代码,但意识到一旦我开始输入更大的数字,一切就会变得有点混乱和混乱。 我决定从python显示数字到现实生活中如何写数字:

示例:从45670824,567,082560867956560,867,956,等等。

但是尝试了一段时间之后,我什么也没想到。只是想知道,如果有人知道解决方案可能是什么?

1 个答案:

答案 0 :(得分:-2)

>>> qs = Tweet.objects.filter(
...     tweet_id__in=[949763170863865857, 961432484620787712]
... ).annotate(
...     vector=SearchVector('text')
... )
>>> 
>>> for tweet in qs:
...     print(f'Doc text: {tweet.text}')
...     print(f'From db:  {tweet.text_search_vector}')
...     print(f'From qs:  {tweet.vector}\n')
... 
Doc text: @Espngreeny Run your 3rd and long play and  compete for a chance on third down.
From db:  '3rd':4 'chanc':12 'compet':9 'espngreeni':1 'long':6 'play':7 'run':2 'third':14
From qs:  '3rd':4 'a':11 'and':5,8 'chance':12 'compete':9 'down':15 'espngreeny':1 'for':10 'long':6 'on':13 'play':7 'run':2 'third':14 'your':3

Doc text: No chance. It was me complaining about Girl Scout cookies. <url-removed-for-stack-overflow>
From db:  '/aggcqwddbh':13 'chanc':2 'complain':6 'cooki':10 'girl':8 'scout':9 't.co':12 't.co/aggcqwddbh':11
From qs:  '/aggcqwddbh':13 'about':7 'chance':2 'complaining':6 'cookies':10 'girl':8 'it':3 'me':5 'no':1 'scout':9 't.co':12 't.co/aggcqwddbh':11 'was':4

打印:

n1 = 4567082
n2 = 560867956
n3 = 120

def add_char(n, char):
    sn = str(n)
    rn = sn[::-1]
    return char.join(rn[s:s+3] for s in range(0, len(sn), 3))[::-1]

print(add_char(n1, ','))
print(add_char(n2, ','))
print(add_char(n3, ','))
相关问题