改变打印语句并删除长小数点

时间:2011-11-26 12:22:43

标签: python

我的代码中有一个print语句,它生成以下列表。这条线是,

grp = itertools.groupby(phonemeList, lambda i: i[1] is None)
phList = [tuple(i[1]) for i in grp if not i[0]] 

print "\nsyllable groups separated"
print '\n\n'.join(str(l) for l in phList)

输出是,

syllable groups separated
(('pau', [(0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), [0.0, 0.0],  [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('s', [(0.0, 0.5), [0.10000000000000001, 1.0], (0.0, 1.0), [0.0, 0.0], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('aa', [(0.0, 0.29999999999999999), [0.10000000000000001, 0.80000000000000004], (0.0, 1.0), [0.0, 1.0], (0.0, 0.5), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

(('m', [[0.0, 0.0], [0.0, 0.0], (0.0, 1.0), [0.0, 0.80000000000000004], (0.0, 1.0), [1.0, 1.0], [0.5, 0.5], [1.0, 1.0], [1.0, 1.0]]), ('ih', [(0.0, 0.29999999999999999), [0.10000000000000001, 0.80000000000000004], [0.10000000000000001, 0.5], [0.0, 0.80000000000000004], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

(('k', [(0.0, 0.29999999999999999), [0.10000000000000001, 0.80000000000000004], (0.0, 1.0), [0.0, 0.80000000000000004], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('l', [(0.0, 0.69999999999999996), [0.10000000000000001, 0.80000000000000004], (0.0, 1.0), [0.0, 0.40000000000000002], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('eh', [(0.0, 0.40000000000000002), [0.10000000000000001, 0.80000000000000004], (0.0, 1.0), [0.0, 1.0], (0.0, 0.5), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('k', [(0.0, 0.29999999999999999), [0.10000000000000001, 0.80000000000000004], (0.0, 1.0), [0.0, 0.80000000000000004], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

(('t', [[0.0, 0.0], [0.10000000000000001, 0.80000000000000004], (0.0, 1.0), [0.0, 0.40000000000000002], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('aa', [(0.0, 0.29999999999999999), [0.10000000000000001, 0.80000000000000004], (0.0, 1.0), [0.0, 1.0], (0.0, 0.5), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

(('n', [(0.0, 0.40000000000000002), [0.10000000000000001, 1.0], (0.0, 1.0), [0.0, 0.0], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('ih', [(0.0, 0.29999999999999999), [0.10000000000000001, 0.80000000000000004], [0.10000000000000001, 0.5], [0.0, 0.80000000000000004], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('t', [[0.0, 0.0], [0.10000000000000001, 0.80000000000000004], (0.0, 1.0), [0.0, 0.40000000000000002], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('pau', [(0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

(('g', [(0.0, 0.5), [0.10000000000000001, 0.80000000000000004], (0.0, 1.0), [0.0, 0.80000000000000004], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('eh', [(0.0, 0.40000000000000002), [0.10000000000000001, 0.80000000000000004], (0.0, 1.0), [0.0, 1.0], (0.0, 0.5), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('l', [(0.0, 0.69999999999999996), [0.10000000000000001, 0.80000000000000004], (0.0, 1.0), [0.0, 0.40000000000000002], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

(('v', [[0.29999999999999999, 1.0], [0.0, 0.0], (0.0, 1.0), (0.10000000000000001, 1.0), (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('ae', [(0.0, 0.29999999999999999), [0.10000000000000001, 0.80000000000000004], [0.10000000000000001, 0.80000000000000004], [0.0, 1.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

(('n', [(0.0, 0.40000000000000002), [0.10000000000000001, 1.0], (0.0, 1.0), [0.0, 0.0], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('ih', [(0.0, 0.29999999999999999), [0.10000000000000001, 0.80000000000000004], [0.10000000000000001, 0.5], [0.0, 0.80000000000000004], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('k', [(0.0, 0.29999999999999999), [0.10000000000000001, 0.80000000000000004], (0.0, 1.0), [0.0, 0.80000000000000004], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('pau', [(0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

(('aa', [(0.0, 0.29999999999999999), [0.10000000000000001, 0.80000000000000004], (0.0, 1.0), [0.0, 1.0], (0.0, 0.5), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]),)

(('p', [[0.0, 0.0], [0.0, 0.0], (0.0, 1.0), [0.0, 0.80000000000000004], (0.0, 1.0), [1.0, 1.0], [0.5, 0.5], [1.0, 1.0], [1.0, 1.0]]), ('l', [(0.0, 0.69999999999999996), [0.10000000000000001, 0.80000000000000004], (0.0, 1.0), [0.0, 0.40000000000000002], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('ao', [(0.0, 0.29999999999999999), [0.10000000000000001, 0.80000000000000004], (0.0, 1.0), [0.0, 1.0], (0.0, 0.5), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('iy', [(0.0, 0.29999999999999999), [0.10000000000000001, 0.80000000000000004], [0.10000000000000001, 1.0], [0.0, 0.80000000000000004], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('k', [(0.0, 0.29999999999999999), [0.10000000000000001, 0.80000000000000004], (0.0, 1.0), [0.0, 0.80000000000000004], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), ('pau', [(0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

这里的组用外圆括号()分隔。但即使在小组内部也有不止一个字母。如何以新的方式获取它们?我还想摆脱所有这些小数位。输出应仅显示最多1个小数点。虽然输入只有1个小数点,但python只在输出中显示长小数点。输出应该如下所示,

syllable groups separated
(('pau', [(0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('s', [(0.0, 0.5), [0.1, 1.0], (0.0, 1.0), [0.0, 0.0], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('aa', [(0.0, 0.3), [0.1, 0.8], (0.0, 1.0), [0.0, 1.0], (0.0, 0.5), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

(('m', [[0.0, 0.0], [0.0, 0.0], (0.0, 1.0), [0.0, 0.8], (0.0, 1.0), [1.0, 1.0], [0.5, 0.5], [1.0, 1.0], [1.0, 1.0]]), 
 ('ih', [(0.0, 0.3), [0.1, 0.8], [0.1, 0.5], [0.0, 0.8], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

(('k', [(0.0, 0.3), [0.1, 0.8], (0.0, 1.0), [0.0, 0.8], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('l', [(0.0, 0.7), [0.1, 0.8], (0.0, 1.0), [0.0, 0.4], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('eh', [(0.0, 0.4), [0.1, 0.8], (0.0, 1.0), [0.0, 1.0], (0.0, 0.5), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('k', [(0.0, 0.3), [0.1, 0.8], (0.0, 1.0), [0.0, 0.8], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

(('t', [[0.0, 0.0], [0.1, 0.8], (0.0, 1.0), [0.0, 0.4], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('aa', [(0.0, 0.3), [0.1, 0.8], (0.0, 1.0), [0.0, 1.0], (0.0, 0.5), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

(('n', [(0.0, 0.4), [0.1, 1.0], (0.0, 1.0), [0.0, 0.0], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('ih', [(0.0, 0.3), [0.1, 0.8], [0.1, 0.5], [0.0, 0.8], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('t', [[0.0, 0.0], [0.1, 0.8], (0.0, 1.0), [0.0, 0.4], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('pau', [(0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

(('g', [(0.0, 0.5), [0.1, 0.8], (0.0, 1.0), [0.0, 0.8], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('eh', [(0.0, 0.4), [0.1, 0.8], (0.0, 1.0), [0.0, 1.0], (0.0, 0.5), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('l', [(0.0, 0.7), [0.1, 0.8], (0.0, 1.0), [0.0, 0.4], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

(('v', [[0.3, 1.0], [0.0, 0.0], (0.0, 1.0), (0.1, 1.0), (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('ae', [(0.0, 0.3), [0.1, 0.8], [0.1, 0.8], [0.0, 1.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

(('n', [(0.0, 0.4), [0.1, 1.0], (0.0, 1.0), [0.0, 0.0], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('ih', [(0.0, 0.3), [0.1, 0.8], [0.1, 0.5], [0.0, 0.8], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('k', [(0.0, 0.3), [0.1, 0.8], (0.0, 1.0), [0.0, 0.8], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('pau', [(0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

(('aa', [(0.0, 0.3), [0.1, 0.8], (0.0, 1.0), [0.0, 1.0], (0.0, 0.5), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]),)

(('p', [[0.0, 0.0], [0.0, 0.0], (0.0, 1.0), [0.0, 0.8], (0.0, 1.0), [1.0, 1.0], [0.5, 0.5], [1.0, 1.0], [1.0, 1.0]]), 
 ('l', [(0.0, 0.7), [0.1, 0.8], (0.0, 1.0), [0.0, 0.4], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('ao', [(0.0, 0.3), [0.1, 0.8], (0.0, 1.0), [0.0, 1.0], (0.0, 0.5), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('iy', [(0.0, 0.3), [0.1, 0.8], [0.1, 1.0], [0.0, 0.8], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('k', [(0.0, 0.3), [0.1, 0.8], (0.0, 1.0), [0.0, 0.8], (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]), 
 ('pau', [(0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0]]))

1 个答案:

答案 0 :(得分:2)

使用格式字符串。

>>> print "%.1f" % 1.234
1.2