我使用了一个生成器来创建s
s= generator(n)
生成器yeilds(a,b)用于范围(n)中的n。其中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。任何人都可以帮我解决这个问题吗?
答案 0 :(得分: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
答案 1 :(得分:1)
from itertools import chain
'\n'.join('\t'.join(map(str,tuple(chain(*i)))) for i in s)
chain
展平每一行,因此您只需使用制表符连接所有元素,然后使用换行符连接行。
答案 2 :(得分: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]
>>>