私信  •  关注

Charif DZ

Charif DZ 最近创建的主题
Charif DZ 最近回复了
s = r'abc123d, hello 3.1415926, this is my book'
print re.findall(r'-?[0-9]+(?:\.[0-9]*)?|-?\.[0-9]+',s)

你不需要 escape 使用时两次 raw mode .

输出: ['123', '3.1415926']

返回类型还将是 strings 。如果希望返回类型为 integers floats 使用 map

import re,ast
s = r'abc123d, hello 3.1415926, this is my book'
print map(ast.literal_eval,re.findall(r'-?[0-9]+(?:\.[0-9]*)?|-?\.[0-9]+',s))

输出: [123, 3.1415926]

5 年前
回复了 Charif DZ 创建的主题 » 如何在python中将int打印为words seq

试试这个:

table=['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
number = input()

# loop over the input string to get number by number
for x in number:
     # now get the string representation from its index in table
    print(table[int(x)], end=' ')

输出:

684
six eight four

586493
five eight six four nine three