Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

susanjrobertson Group Title

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"])

  • 2 years ago
  • 2 years ago

  • This Question is Closed
  1. TuringTest Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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

    • 2 years ago
  2. susanjrobertson Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

    • 2 years ago
  3. TuringTest Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    could you post a link to the exercise?

    • 2 years ago
  4. susanjrobertson Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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

    • 2 years ago
  5. snark Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    how about [x for x in ln list if x isinstance of(...] - now i'll go read the problem....

    • 2 years ago
  6. snark Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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]

    • 2 years ago
  7. snark Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    ooops [x for x in r if isinstance(x,int)] ...threw an extra x in....

    • 2 years ago
  8. mmailliw Group Title
    Best Response
    You've already chosen the best response.
    Medals 1

    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".

    • 2 years ago
  9. lh4219 Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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

    • 2 years ago
  10. jgraves Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    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]

    • 2 years ago
  11. susanjrobertson Group Title
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks all for your help. :)

    • 2 years ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.