1

我使用了一个生成器来创建 s

s= generator(n)

对于范围(n)内的 n,生成器产生(a,b)。其中 a=[w,x] 和 b=[y,z]

打印使用

for i in s:
    print i

这返回:

([0.27704232355167768, 0.44459304959240675], [0.4387731877846518, 0.38108111684466683])
([0.27704232355167768, 0.44459304959240675], [0.6362447250743466, 0.72047209074359853])
([0.27704232355167768, 0.44459304959240675], [0.65419386891877318, 0.025362727486327286])
([0.27704232355167768, 0.44459304959240675], [0.039966264334369672, 0.9662795347591735])

不过我想

0.27704232355167768 0.44459304959240675 0.4387731877846518 0.38108111684466683
0.27704232355167768 0.44459304959240675 0.6362447250743466 0.72047209074359853
0.27704232355167768 0.44459304959240675 0.65419386891877318 0.025362727486327286
0.27704232355167768 0.44459304959240675 0.039966264334369672 0.9662795347591735

我尝试了以下想法的许多变体

print '\n'.join('\t'.join(x) for x in s)

但是为了 n0 有用并且倾向于以相同的格式返回 s。谁能帮我解决这个问题?

4

3 回答 3

1
>>> from itertools import chain
>>> for i in s:
        print '\t'.join(map(str,chain(*i)))


0.277042323552 0.444593049592 0.438773187785 0.381081116845
0.277042323552 0.444593049592 0.636244725074 0.720472090744
0.277042323552 0.444593049592 0.654193868919 0.0253627274863
0.277042323552 0.444593049592 0.0399662643344 0.966279534759
于 2012-04-08T16:44:50.680 回答
1
from itertools import chain
'\n'.join('\t'.join(map(str,tuple(chain(*i)))) for i in s)

chain展平每一行,因此您可以使用选项卡连接所有元素,然后使用换行符连接行。

于 2012-04-08T16:54:02.370 回答
0

不知道你的发电机是什么样子,但看看你的例子,如果你的发电机如下

>>> def generator(n):
    for i in xrange(0,n):
        yield ([0.27704232355167768, 0.44459304959240675], [0.4387731877846518, 0.38108111684466683])
    return

然后你可以像

>>> s=generator(5)
>>> print '\n'.join('\t'.join(map(str,x))  for x in s)
[0.2770423235516777, 0.44459304959240675]   [0.4387731877846518, 0.38108111684466683]
[0.2770423235516777, 0.44459304959240675]   [0.4387731877846518, 0.38108111684466683]
[0.2770423235516777, 0.44459304959240675]   [0.4387731877846518, 0.38108111684466683]
[0.2770423235516777, 0.44459304959240675]   [0.4387731877846518, 0.38108111684466683]
[0.2770423235516777, 0.44459304959240675]   [0.4387731877846518, 0.38108111684466683]
>>> 
于 2012-04-08T16:41:51.427 回答