Note that there are some explanatory texts on larger screens.

plurals
  1. POFormatting nested lists/tuples
    primarykey
    data
    text
    <p>I have an output (a long one!) from some function, which looks in this way:</p> <pre><code>[[(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)]] </code></pre> <p>I don't like the structure of the output, but it is very convenient to use it in the function, which returns it.</p> <p>But i need to format the output to look this way:</p> <pre><code>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 </code></pre> <p>I have this code:</p> <pre><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)) </code></pre> <p>('results' is the name of the function, which returns the big list)</p> <p>It gives me this:</p> <pre><code>(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 </code></pre> <p>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?</p> <p>Anyway, hoping for your help! Thanks in advance!</p> <p>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)</p> <pre><code>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))) </code></pre> <p>And it doesn't actually work properly, I only get this: </p> <pre><code>0 . 0 1 . 0 2 . 0 A . . . </code></pre> <p>Why do I get so strange formatting, but not what I get with print? It is quite shocking to me...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload