LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-20-2012, 01:14 AM   #1
ahzthecat
Member
 
Registered: May 2010
Location: Japan
Distribution: Slackware 13.37, Slackware 14, Wind0z8, Ubuntu 12.10
Posts: 156

Rep: Reputation: 9
Having Trouble Understanding List Comprehensions in Python 2.x


Can someone please help me to understand list comprehensions in Python?

I'm working my way through the MIT Gentle Introduction to Python and I'm stuck on the list comprehensions section.

The first exercise I get.

Code:
# **********  Exercise 2.10 **********
oneten=[1,2,3,4,5,6,7,8,9,10]

cubes=[x**3 for x in oneten]

# Test Cases
##### YOUR CODE HERE #####

print cubes
but the next is to use list comprehension to generate all possible results of two coin tosses (hh, ht, th, tt)

So far I have this, which generates only "hh , tt" and a headache...
Code:
flips=["h", "t"]

allresults=[x+x for x in flips ] 

print allresults
Do I use a loop inside a list comprehension statement, or am I completely on the wrong track?
 
Old 12-20-2012, 02:36 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Without ever seeing Python in my life, I would try

Code:
allresults=[x+y for x in flips for y in flips ]
 
1 members found this post helpful.
Old 12-20-2012, 07:04 PM   #3
ahzthecat
Member
 
Registered: May 2010
Location: Japan
Distribution: Slackware 13.37, Slackware 14, Wind0z8, Ubuntu 12.10
Posts: 156

Original Poster
Rep: Reputation: 9
Talking

Thanks millgates!

I read some more and arrived at this solution
Code:
coina=["h", "t"]
coinb=["h","t"]

allresults2=[x+y for x in coina for y in coinb]
then I went back and tried your suggestion, and it worked a treat. Silly me for not trying that first.
Good thing is, I feel like I understand comprehension syntax a little better now. Cheers!
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Trouble understanding thread synchronization. badmofo666 Programming 4 03-05-2012 02:01 PM
Discussion - Python List Comprehensions; power set? robotsari Programming 3 01-02-2011 03:46 AM
trouble understanding pm task mechanisms sancho1980 Linux - Kernel 0 12-08-2006 05:12 AM
I am having trouble understanding my logwatch? asif2k Linux - Security 1 09-05-2006 06:41 PM
Having Trouble Understanding/Configuring Wine cajunaggie Linux - Software 5 10-28-2004 02:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration