2012-06-24 33 views
7

我有一個整數列表 - 是否有一個庫將它們轉換爲純文本排名? IE: 1,2,3 - >「第一,第二,第三」?是否有一個庫將整數轉換爲第一/第二/第三

+1

http://www.daniweb.com/software-development/python/code/216839/number-to-word-converter-python –

+1

當我開始上大學的時候,我們在第一天的任務之一就是完成這個任務Java的。如果你在編程方面有點經驗,這不應該超過30分鐘。 –

+1

30分鐘?真的大聲笑..你的意思是1分鐘.... http://jsfiddle.net/cmvLS/(javascript) – ncubica

回答

5

你打算去多高? (難道你指望高於,說:「二十」?)

也許你只需要一個dict

nth = { 
    1: "first", 
    2: "second", 
    3: "third", 
    4: "fourth" 
    # etc 
} 
+3

它只是變得更容易後,「二十」:) –

2

根據您的情況,您可能會發現它很有保留整數,並追加「 st「,」nd「,」rd「和」th「。如果是這樣,這裏是一個簡單的算法:

注意:這個算法是用Java編寫的。我不知道Python。任何想用Python重寫的人,都是我的客人。

public static String appendInt(int number) { 
    String value = String.valueOf(number); 
    if(value.length() > 1) { 
     // Check for special case: 11 - 13 are all "th". 
     // So if the second to last digit is 1, it is "th". 
     char secondToLastDigit = value.charAt(value.length()-2); 
     if(secondToLastDigit == '1') 
      return "th"; 
    } 
    char lastDigit = value.charAt(value.length()-1); 
    switch(lastDigit) { 
     case '1': 
      return "st"; 
     case '2': 
      return "nd"; 
     case '3': 
      return "rd"; 
     default: 
      return "th"; 
    } 
} 

所以

System.out.println(1 + appendInt(1)); 
System.out.println(2 + appendInt(2)); 
System.out.println(3 + appendInt(3)); 
System.out.println(4 + appendInt(4)); 
System.out.println(5 + appendInt(5)); 
System.out.println(11 + appendInt(11)); 
System.out.println(21 + appendInt(21)); 

顯示

1st 
2nd 
3rd 
4th 
5th 
11th 
21st 
+0

這是一個很棒的方式去了解它。 – answerSeeker

3

不能因爲分ryvantage的帖子發表評論,但我寫的Python同一代碼:

def appendInt(num): 
    if num > 9: 
     secondToLastDigit = str(num)[-2] 
     if secondToLastDigit == '1': 
      return 'th' 
    lastDigit = num % 10 
    if (lastDigit == 1): 
     return 'st' 
    elif (lastDigit == 2): 
     return 'nd' 
    elif (lastDigit == 3): 
     return 'rd' 
    else: 
     return 'th' 



def appendInt2(num): 
    value = str(num) 
    if len(value) > 1: 
     secondToLastDigit = value[-2] 
     if secondToLastDigit == '1': 
      return 'th' 
    lastDigit = value[-1] 
    if (lastDigit == '1'): 
     return 'st' 
    elif (lastDigit == '2'): 
     return 'nd' 
    elif (lastDigit == '3'): 
     return 'rd' 
    else: 
     return 'th' 

的其次是更直接的反式特徵研,但我發現,第一個變化是相當快:

週五8月28日11時48分13秒2015年導致

  300000005 function calls in 151.561 seconds 

    Ordered by: call count, name/file/line 

    ncalls tottime percall cumtime percall filename:lineno(function) 
100000000 6.674 0.000 6.674 0.000 {len} 
100000000 43.064 0.000 43.064 0.000 test.py:7(appendInt) 
100000000 66.664 0.000 73.339 0.000 test.py:22(appendInt2) 
     1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} 
     1 0.000 0.000 151.561 151.561 <string>:1(<module>) 
     1 0.000 0.000 151.561 151.561 test.py:51(main) 
     1 16.737 16.737 59.801 59.801 test.py:39(test1) 
     1 18.421 18.421 91.759 91.759 test.py:45(test2) 
+0

數組[-2]是Python中倒數第二個數值? *酷* – ryvantage

2

類似於@Hugh Bothwellwas following,只是沒有使用dict(因爲你的脈項已很好的和數值),我已經把下面「一個班輪」

ordinal=lambda x:["first","second","third","fourth","fifth","sixth","seventh","eighth","ninth","tenth","eleventh","twelfth"][x-1] 

涵蓋所有情況下可達12若y你需要更高的(數百等),那麼你可能會需要更強大的東西(與我想象的遞歸等)。

0

一個很好的和簡單的重寫:

def num_to_ith(num): 
    """1 becomes 1st, 2 becomes 2nd, etc.""" 
    value    = str(num) 
    before_last_digit = value[-2] 
    last_digit  = value[-1] 
    if len(value) > 1 and before_last_digit == '1': return value +'th' 
    if last_digit == '1': return value + 'st' 
    if last_digit == '2': return value + 'nd' 
    if last_digit == '3': return value + 'rd' 
    return value + 'th' 
9

蛇皮inflect封裝具有用於將標記成序數的方法:

import inflect 
p = inflect.engine() 

for i in range(1,25,5): 
    print(p.ordinal(i)) 

顯示:

1st 
6th 
11th 
16th 
21st 
+1

哈哈,真棒!這**是對問題的正確答案。 – Atcold

+0

哇,我正在尋找! – answerSeeker

+0

'p.number_to_words(p.ordinal(i))'會顯示爲'first','second','third'等。 –

0

我給這代碼利用列表,整數模數來查找第一位數字和整數位數除以檢查是否爲ru勒11通13.

def stringify(number): 
     suffix = ["th","st","nd","rd","th","th","th","th","th","th"] 
     return str(number) + suffix[number % 10] if str(number // 10)[-1] != 1 else str(number) + "th"   

最後一行更易讀:

if str(number // 10)[-1] != '1':  
     return str(number) + suffix[number % 10] 
    else: 
     return str(number) + "th" 

基本上,我檢查第二位爲1 *,它表示一個可能的第11,第12,第13規則斷路器。對於規則管理者,該列表被除以10(模數運算符)的餘數所引用。

我還沒有做過代碼效率的任何測試,並且檢查長度是可讀性更強,但是這是有效的。

*(STR(數目)[ - 2]。如果輸入是一個數字產生IndexError使用對整數除法地板總是導致至少1個位數因此字符串將始終具有一個索引-1)

相關問題