Formatting nested lists/tuples

2019-09-09 05:54发布

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...

3条回答
戒情不戒烟
2楼-- · 2019-09-09 06:26

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.

查看更多
成全新的幸福
3楼-- · 2019-09-09 06:32

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.

The implementation adds the letter to the list. (The list constructor is needed, because a tuple is constant and cannot be changed.)

查看更多
手持菜刀,她持情操
4楼-- · 2019-09-09 06:33

Replace your call to str in the map call to calling a wee function:

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))
查看更多
登录 后发表回答