Python:re.findall的单独结果

时间:2013-03-22 21:44:10

标签: python findall

我正在使用re.findall,结果是这样的:

[(u'! mais', u'! - but', u'0.0625')]

我想将“元组”的每个部分存储在不同的变量中,例如

french = u'! mais'
english = u'! - but'
prob = u'0.0625'

我还希望将字符串u'0.0625'转换为浮点数。 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

如果您只有一个元组的列表:

french, english, prob = result[0]

如果你有多个元组,你可能想要循环它们:

for result in result:
    french, english, prob = result
    # do something with this particular french, english, prob

但是如果你想要制作三个列表,那也很容易:

frenchies, englishers, probs = zip(*results)

无论哪种方式,您都可以通过调用prob将<{1}}转换为浮动:

float

或者,如果你有这个清单:

prob = float(prob)