anonymous
  • anonymous
Has anyone tried the first list comprehension optional exercise? I'm getting a syntax error and I'm not sure why. My code is: def number_list(random_list): num = [x if isinstance(x, int) in random_list] return num print number_list([2, 4.01, 6, 9.01, "hello", "why"])
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
katieb
  • katieb
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
TuringTest
  • TuringTest
num = [x if isinstance(x, int) in random_list] this line is no good, you need to rephrase it, probably turning it into a few lines
anonymous
  • anonymous
I get that that is the line that is incorrect, but if we are supposed to use a list comprehension to create the list, then it can't be a few lines, can it?
TuringTest
  • TuringTest
could you post a link to the exercise?

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • anonymous
It's the option exercise 2 the first one in this pdf for the course: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-189-a-gentle-introduction-to-programming-using-python-january-iap-2011/assignments/MIT6_189IAP11_hw2.pdf
anonymous
  • anonymous
how about [x for x in ln list if x isinstance of(...] - now i'll go read the problem....
anonymous
  • anonymous
it seems to work: r = ['a', 'w', [1,2,3], 5,'why me?',8] b = [x for x in r if x isinstance(x,int)] [5, 8]
anonymous
  • anonymous
ooops [x for x in r if isinstance(x,int)] ...threw an extra x in....
anonymous
  • anonymous
The syntax for list comprehension is [x for x in SOMETHING if EXPRESSION] where "SOMETHING" is a list and "EXPRESSION" has a boolean value for example: [x for x in [1,2,3] if x > 2] while your list comprehension begins "x if" instead of "x for".
anonymous
  • anonymous
a = ['op', 1.7, 101, 3, 'ate', 10, 35.6] b = [i for i in a if isinstance(i, int)] print b b = [(x,y)for y in range(11) for x in range(-5, 6) if x**2 + 1 == y] print b b = [(x,y)for y in range(11)for x in range(-5, 6) if x**2 + y**2 == 25] print b
anonymous
  • anonymous
I think it is better to say the syntax is [ EXPRESSION for x in LIST if BOOLEAN EXPRESSION] >>> a = [1,2,3] >>> lc = [2*x for x in a] >>> print lc [2, 4, 6]
anonymous
  • anonymous
Thanks all for your help. :)

Looking for something else?

Not the answer you are looking for? Search for more explanations.