[英]Formatting nested lists/tuples
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? 如果不在此代码行的末尾添加字母(A,B或C),我会得到的,所以,我想,也许可以以其他方式添加它吗?
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) 出乎意料的问题...我真的想将输出写入.csv文件,所以我使用print代替打印(目前,我选择了使用list并附加字母的选项,因为这对我来说更清楚)
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. 如果它的长度总是3,则应查看
itertools
模块。 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. 另外,我希望您实际上不使用
for z in range(len(results()[0])):
,因为那样会重新运行结果函数,从而重新计算整个列表。 Just FYI. 仅供参考。
The complete program: 完整程序:
def results():
return [[(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)]]
obj = 'A', 'B', 'C'
for n in results():
for z in range(len(results()[0])):
k = list(n[z])
k.append(obj[z])
print(','.join(map(str, k)))
The output: 输出:
0.0,1.0,2.0,A
1.0,2.0,0.0,B
2.0,1.0,0.0,C
1.63249862945e-06,1.00000027208,1.99999927445,A
1.0,1.99999855599,9.62671341171e-07,B
1.99999571241,1.0000007146,9.52797527942e-07,C
0.000164885263819,1.00002748255,1.99992671164,A
0.999999981018,1.99985414922,9.72390384323e-05,B
1.99956691488,1.00007218369,9.62532545798e-05,C
This was checked with python 2.5, 2.6, 2.7 and 3.2. 使用python 2.5、2.6、2.7和3.2对此进行了检查。
The implementation adds the letter to the list. 该实现将字母添加到列表中。 (The list constructor is needed, because a tuple is constant and cannot be changed.)
(列表构造器是必需的,因为元组是常量,不能更改。)
Replace your call to str in the map call to calling a wee function: 将map调用中的str调用替换为调用wee函数:
def foo(lst):
return ' '.join(map(str,lst))
obj = 'A', 'B', 'C'
for n in results():
for z in range(len(results()[0])):
k = n[z], obj[z]
print '\t'.join(map(foo, k))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.