格式化嵌套列表/元组 [英] Formatting nested lists/tuples
问题描述
[[(())从一个函数中得到一个输出(1.0,1.0,2.0),(1.0,2.0,0.0),(2.0,1.0,0.0)],[(1.6324986294474886e-06,1.000000272083105,1.9999992744450537),(1.0,1.9999985559929883,9.626713411705526e-07),(1.9999957124111243) 1.000000714598146,9.527975279416402e-07)] ...................... [(0.00016488526381860965,1.0000274825531668,1.9999267116402146),(0.9999999810184469,1.9998541492231847,9.723903843230245e-05 ),(1.9995669148822666,1.000072183688789,962532545797885e-05)]]
我不喜欢这个结构的输出,但它是非常方便的使用它的函数,它返回它。
但我需要格式化输出看起来这样:
0.0 1.0 2.0 A
1.0 2.0 0.0 B
2.0 1.0 0.0 C
1.6324986294474886e-06 1.000000272083105 1.9999992744450537 A
1.0 1.9999985559929883 9.626713411705526e-07 B
1.99999571241112 43 1.000000714598146 9.527975279416402e-07 C
我有这样的代码:
$ b $ results()[0])):
k = n [z],obj [z]
print'\t'.join(map(str,k))
('results'是函数的名字,它返回大的列表)
$ p
$ b pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 0.0)B
(2.0,1.0,0.0)C
(1.6324986294474886e-06,1.000000272083105,1.9999992744450537)A
(1.0,1.9999985559929883,9.626713411705526e-07)B
1.9999957124111243,1.000000714598146,9.527975279416402e-07)C
我可以得到这个,如果我不加字母(A,B或C)在这个代码行的末尾,所以,我想,也许以不同的方式添加它?
无论如何,希望你的帮助!
提前感谢!
一个意外的问题...我真的想把输出写入一个.csv文件,所以不用打印我用(for现在我已经选择了使用列表和附加信件的选项,因为它对我来说更加清楚)
with open( (b)(b)(b)(b)(b)(c)(b) ])):
k = list(n [z])
k.append(obj [z])
writer.writerows('\t'.join(map(str,k )))
而且它实际上并没有正常工作,我只得到这个:
0
。
0
1
。
0
2
。
0
A
。
。
。
为什么我得到这么奇怪的格式,但不是我用打印得到的?这对我来说是相当震撼的...
如果它总是长度为3, code> itertools 模块。例如:
pre $ import itertools
for num,itertools.izip中的字母(itertools.chain.from_iterable(bigList) ,itertools.cycle('ABC')):
print'%f\t%s'%(num,letter)
另外,我希望你在范围(len(results()[0]))中实际上并不使用:将重新运行结果函数,重新计算整个列表。只是供参考。
I have an output (a long one!) from some function, which looks in this way:
[[(0.0, 1.0, 2.0), (1.0, 2.0, 0.0), (2.0, 1.0, 0.0)], [(1.6324986294474886e-06, 1.000000272083105, 1.9999992744450537), (1.0, 1.9999985559929883, 9.626713411705526e-07), (1.9999957124111243, 1.000000714598146, 9.527975279416402e-07)], ......................, [(0.00016488526381860965, 1.0000274825531668, 1.9999267116402146), (0.9999999810184469, 1.9998541492231847, 9.723903843230245e-05), (1.9995669148822666, 1.000072183688789, 9.62532545797885e-05)]]
I don't like the structure of the output, but it is very convenient to use it in the function, which returns it.
But i need to format the output to look this way:
0.0 1.0 2.0 A
1.0 2.0 0.0 B
2.0 1.0 0.0 C
1.6324986294474886e-06 1.000000272083105 1.9999992744450537 A
1.0 1.9999985559929883 9.626713411705526e-07 B
1.9999957124111243 1.000000714598146 9.527975279416402e-07 C
I have this code:
obj = 'A', 'B', 'C'
for n in results():
for z in range(len(results()[0])):
k = n[z], obj[z]
print '\t'.join(map(str, k))
('results' is the name of the function, which returns the big list)
It gives me this:
(0.0, 1.0, 2.0) A
(1.0, 2.0, 0.0) B
(2.0, 1.0, 0.0) C
(1.6324986294474886e-06, 1.000000272083105, 1.9999992744450537) A
(1.0, 1.9999985559929883, 9.626713411705526e-07) B
(1.9999957124111243, 1.000000714598146, 9.527975279416402e-07) C
I can get this, if I don't add the letter (A, B or C) in the end of the line with this code, so, I thought, maybe to add it somehow differently?
Anyway, hoping for your help! Thanks in advance!
An unexpected problem... I really want to write the output into a .csv file, so instead of print I use (for now I've chosen the option of using list and appending the letter, as it is more clear to me)
with open('table.csv', 'wb') as f:
writer = csv.writer(f)
for n in results():
for z in range(len(res[0])):
k = list(n[z])
k.append(obj[z])
writer.writerows ('\t'.join(map(str, k)))
And it doesn't actually work properly, I only get this:
0
.
0
1
.
0
2
.
0
A
.
.
.
Why do I get so strange formatting, but not what I get with print? It is quite shocking to me...
If its always going to be of length 3, you should look into the itertools
module. Something like:
import itertools
for num, letter in itertools.izip(itertools.chain.from_iterable(bigList), itertools.cycle('ABC')):
print '%f\t%s' % (num, letter)
Also, I hope youre not actually using for z in range(len(results()[0])):
, as that will rerun the results function, recalculating the entire list. Just FYI.
这篇关于格式化嵌套列表/元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!