Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 26th, 2009, 05:18 PM
princessotes princessotes is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 5 princessotes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 26 m 9 sec
Reputation Power: 0
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!

Reply With Quote
  #2  
Old June 26th, 2009, 06:34 PM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 488 sfb User rank is Sergeant (500 - 2000 Reputation Level)sfb User rank is Sergeant (500 - 2000 Reputation Level)sfb User rank is Sergeant (500 - 2000 Reputation Level)sfb User rank is Sergeant (500 - 2000 Reputation Level)sfb User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 9 h 35 m 47 sec
Reputation Power: 20
Quote:
Originally Posted by princessotes
Hi, I have written this but the result i get is a list


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.

Reply With Quote
  #3  
Old June 27th, 2009, 03:39 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,563 DevCoach User rank is General 4th Grade (Above 100000 Reputation Level)DevCoach User rank is General 4th Grade (Above 100000 Reputation Level)DevCoach User rank is General 4th Grade (Above 100000 Reputation Level)DevCoach User rank is General 4th Grade (Above 100000 Reputation Level)DevCoach User rank is General 4th Grade (Above 100000 Reputation Level)DevCoach User rank is General 4th Grade (Above 100000 Reputation Level)DevCoach User rank is General 4th Grade (Above 100000 Reputation Level)DevCoach User rank is General 4th Grade (Above 100000 Reputation Level)DevCoach User rank is General 4th Grade (Above 100000 Reputation Level)DevCoach User rank is General 4th Grade (Above 100000 Reputation Level)DevCoach User rank is General 4th Grade (Above 100000 Reputation Level)DevCoach User rank is General 4th Grade (Above 100000 Reputation Level)DevCoach User rank is General 4th Grade (Above 100000 Reputation Level)DevCoach User rank is General 4th Grade (Above 100000 Reputation Level)DevCoach User rank is General 4th Grade (Above 100000 Reputation Level)DevCoach User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 3 Days 17 h 54 m 37 sec
Reputation Power: 1241
It is hard to tell from your description, but maybe you want something as simple as this:
python Code:
Original - python Code
  1. a=[1,2,3]
  2. b=[13,14,15]
  3. for i in a:
  4.     for j in b:
  5.         print i-2-((i*2)+(i*j)),
  6.     print


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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Constructing output into columns& rows


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek