【发布时间】:2020-11-21 00:50:06
【问题描述】:
> #!/usr/bin/env python
>
> import inflect
>
> p = inflect.engine()
s1 = ''
>
> word = 'so do 1st or do 2nd or 3rd byte 4 th so do 5th longest word 3765 word 50 but 7th'
> list_word = list(word)
> print (list_word)
>
> for(m= 0; m <list_word.len(); m++):
> if list_word[m].isalpha():
> s1 += i + ''
> elif list_word[m].isnumeric():
> if (list_word[m+1].isnumeric()):
> continue
> elif (list_word[m+1]+list_word[m+2] == 'st'):
> s1 += first + ''
> m += 2
> elif (list_word[m+1]+list_word[m+2] == 'nd'):
> s1 += second + ''
> m += 2
> elif (list_word[m+1]+list_word[m+2] == 'rd'):
> s1 += third + ''
> m += 2
> elif (list_word[m+1]+list_word[m+2] == 'th'):
> if (list_word[m] == '5'):
> s1 += fifth + ''
> m += 2
> else :
> s1 += p.number_to_words(list_word[m]) + ''
> m += 2
> elif (list_word[m+1].isnumeric()):
> continue
> else:
> s1 += p.number_to_words(list_word[m]) + ''
> else:
> s1 += ' '
>
我需要将数字转换为单词并仅包含字母组成一个完整的字符串。但问题是这种 for 循环 不能在 Python 中使用,我需要在某些地方迭代到下一个字符。请给我一些关于这方面的建议。 或者如果有人可以建议任何其他方法。
【问题讨论】:
-
也许是
for m in list_word:?或者如果您需要索引值,for m in range(len(list_word)): -
我试过了,但看起来我的代码变得更复杂了。
标签: python for-loop scripting alphanumeric