|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Constructing output into columns& rows
Hi, I have written this but the result i get is a list
a=[1,2,3] b=[13,14,15] for i in a: for j in b: print i,i-2-((i*2)+(i*j)) I would like to have the output to be in coloumns (=i) and rows (=i-2-((i*2)+(i*j))). Can someone help me pls. thanks in advance! |
|
#2
|
|||
|
|||
|
Quote:
You have written that and the result you get is a list. I'm not trying to be a jerk and nitpicking your grammar, I'm drawing attention to the point that the output is what you tell it to be. You're getting a list because your program is structured to print the outputs one after the other. And from that understanding, the answer appears - you can't just ask the program you have to print as a table, because it's a list printing program through and through. You have to rebuild your program into being a program that inherently prints a table output. The main options are: - Instead of calculating an answer, printing it, moving on; calculate a row of answers, print them, then move on. - Instead of calculating and printing, calculate all the answers and store them, then go through the store and print the results as a table. You might use nested lists [[1,2,3], [1,2,3], [1,2,3]] and the pprint module, perhaps. - Instead of calculating and printing where the cursor happens to be, calculate then find a way of moving the cursor so it will print wherever you want, then print the output. Not sure how to do this easily, though. |
|
#3
|
|||||
|
|||||
|
It is hard to tell from your description, but maybe you want something as simple as this:
python Code:
This will output the results as a 3 x 3 matrix: -16 -17 -18 -30 -32 -34 -44 -47 -50 The comma at the end of the first print statement stops it printing a newline, and the second print statement starts the next line. Dave PS please put your code in [code] or [highlight] blocks to preserve the indentation. See the 'Asking for help' thread at the top of the forum. Last edited by DevCoach : June 27th, 2009 at 03:43 AM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Constructing output into columns& rows |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|