473,466 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

List of lists

How do I extract a list of lists from a user defined function and print the
results as strings for each list?
Jul 18 '05 #1
5 4275
On Fri, 27 Jun 2003 01:44:42 GMT, Mike wrote:
How do I extract a list of lists from a user defined function
Depends how that function is returning its values. The most obvious,
and simplest way to do what you describe, is to have the function return
a list of lists, as its return value.

If you mean something else, you'll have to be more descriptive of what
you actually want to do.
and print the results as strings for each list?


The 'repr()' method of any object will return a printable string of the
object's representation.

--
\ "Kill myself? Killing myself is the last thing I'd ever do." |
`\ -- Homer, _The Simpsons_ |
_o__) |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B
Jul 18 '05 #2
Ok, I'm doin an exercise and I need to expand on test.py. I'm pretty much
done with the exercise except I need to print out the questions and answers
that are in the function.

here is the snippet:

def get_questions():
return [["What color is the daytime sky on a clear day?","blue"],\
["What is the answer to life, the universe and
everything?","42"],\
["What is a three letter word for mouse trap?","cat"]]

How do I get the lists from this?

oh here is the whole program;

## This program runs a test of knowledge

true = 1
false = 0

# First get the test questions
# Later this will be modified to use file io.
def get_questions():
# notice how the data is stored as a list of lists
return [["What color is the daytime sky on a clear day?","blue"],\
["What is the answer to life, the universe and
everything?","42"],\
["What is a three letter word for mouse trap?","cat"]]
# This will test a single question
# it takes a single question in
# it returns true if the user typed the correct answer, otherwise false
def check_question(question_and_answer):
#extract the question and the answer from the list
question = question_and_answer[0]
answer = question_and_answer[1]
# give the question to the user
given_answer = raw_input(question)
# compare the user's answer to the testers answer
if answer == given_answer:
print "Correct"
return true
else:
print "Incorrect, correct was:",answer
return false
# This will run through all the questions
def run_test(questions):
if len(questions) == 0:
print "No questions were given."
# the return exits the function
return
index = 0
right = 0
while index < len(questions):
#Check the question
if check_question(questions[index]):
right = right + 1
#go to the next question
index = index + 1
#notice the order of the computation, first multiply, then divide
print "You got ",right*100/len(questions),"% right out
of",len(questions)

#now lets run the questions
run_test(get_questions())
## This program runs a test of knowledge

true = 1
false = 0

# First get the test questions
# Later this will be modified to use file io.
def get_questions():
# notice how the data is stored as a list of lists
return [["What color is the daytime sky on a clear day?","blue"],\
["What is the answer to life, the universe and
everything?","42"],\
["What is a three letter word for mouse trap?","cat"]]
# This will test a single question
# it takes a single question in
# it returns true if the user typed the correct answer, otherwise false
def check_question(question_and_answer):
#extract the question and the answer from the list
question = question_and_answer[0]
answer = question_and_answer[1]
# give the question to the user
given_answer = raw_input(question)
# compare the user's answer to the testers answer
if answer == given_answer:
print "Correct"
return true
else:
print "Incorrect, correct was:",answer
return false
# This will run through all the questions
def run_test(questions):
if len(questions) == 0:
print "No questions were given."
# the return exits the function
return
index = 0
right = 0
while index < len(questions):
#Check the question
if check_question(questions[index]):
right = right + 1
#go to the next question
index = index + 1
#notice the order of the computation, first multiply, then divide
print "You got ",right*100/len(questions),"% right out
of",len(questions)

#now lets run the questions
run_test(get_questions())
Jul 18 '05 #3
Sorry, must of hit crtl v a couple times.
Jul 18 '05 #4
On Fri, 27 Jun 2003 03:15:56 GMT, Mike wrote:
def get_questions():
return [["What color is the daytime sky on a clear day?","blue"],\
["What is the answer to life, the universe and
everything?","42"],\
["What is a three letter word for mouse trap?","cat"]]


These should not be a list of lists. To refer to a previous c.l.python
discussion, "for homogeneous data, use a list; for heterogeneous data,
use a tuple".

Thus, each question-and-answer pair is heterogeneous: it matters which
is which, and which position each is in; and extending it with more
items doesn't have any meaning.

On the other hand, a list of question-and-answer pairs is homogeneous:
each can be treated like any other question-and-answer pair, and the
list of them could be indefinitely extended or contracted without
distorting the meaning of the list.

So, get_questions() is better done with:

def get_questions():
return [
( "What colour is a clear daytime sky?", "blue" ),
( "What is the answer to the ultimate question?", "42" ),
( "What is a three-letter word for mouse trap?", "cat" ),
]

(Note that placing a comma even after the last item in a list, allows
you to extend the list in the code without having a missing comma by
accident.)

Then, you iterate over the list of question-and-answer pairs, and get a
tuple of (question, answer) each time:
for (question, answer) in get_questions():

print question, answer

What colour is a clear daytime sky? blue
What is the answer to the ultimate question? 42
What is a three-letter word for mouse trap? cat

--
\ "Homer, where are your clothes?" "Uh... dunno." "You mean Mom |
`\ dresses you every day?!" "I guess; or one of her friends." -- |
_o__) Lisa & Homer, _The Simpsons_ |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B
Jul 18 '05 #5
On 27 Jun 2003 13:04:53 +0950, Ben Finney wrote:
>>> for (question, answer) in get_questions(): print question, answer


Dang, messed up the indentation when I pasted. This has the correct
indentation:
for (question, answer) in get_questions():

print question, answer

What colour is a clear daytime sky? blue
What is the answer to the ultimate question? 42
What is a three-letter word for mouse trap? cat

--
\ "If life deals you lemons, why not go kill someone with the |
`\ lemons (maybe by shoving them down his throat)." -- Jack Handey |
_o__) |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted...
1
by: Glen Able | last post by:
Hi, I have a collection of lists, something like this: std::list<Obj*> lists; Now I add an Obj to one of the lists: Obj* gary; lists.push_front(gary);
1
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
11
by: Neo | last post by:
Hi Frns, Could U plz. suggest me how can i reverse a link list (without recursion)??? here is my code (incomplete): #include<stdio.h>
4
by: MJ | last post by:
Hi I have written a prog for reversing a linked list I have used globle pointer Can any one tell me how I can modify this prog so that I dont have to use extra pointer Head1. When I reverse a LL...
2
by: Cox | last post by:
Hello: My address jsmith435@cox.net is subscribed to at least the PHP General mailing list. I have for days now been trying to unsubscribe from all PHP mail lists. I have followed the...
16
by: Michael M. | last post by:
How to find the longst element list of lists? I think, there should be an easier way then this: s1 = s2 = s3 = if len(s1) >= len(s2) and len(s1) >= len(s3): sx1=s1 ## s1 ist längster
19
by: Dongsheng Ruan | last post by:
with a cell class like this: #!/usr/bin/python import sys class Cell: def __init__( self, data, next=None ): self.data = data
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
36
by: pereges | last post by:
Hi, I am wondering which of the two data structures (link list or array) would be better in my situation. I have to create a list of rays for my ray tracing program. the data structure of ray...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.