Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8843797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:19:24+00:00 2026-06-14T11:19:24+00:00

I’m a beginner to Python and am teaching myself list comprehensions. I’ve been doing

  • 0

I’m a beginner to Python and am teaching myself list comprehensions. I’ve been doing well with almost all of the for-loop code I’ve been translating to list comprehension, but am very stuck on what I thought was a fairly simple loop.

n = 10000

def sim(y):
  count = 0
  for i in range(10000):
    if 0.9 <= y[i] <= 1.8:
        count += 1
  probability = count/10000.0
  print("P(a < x <= b) : {0:8.4f}".format(probability))


print ("\t case: \n"),sim([0.25 if random() < 0.8 else 1.5 for r in range(n)])

So far I’ve been trying variations on the following but it’s all getting errors related to the use of lists such as “‘int’ object is unsubscriptable” and “unsupported operand type(s) for +: ‘int’ and ‘list'”.

def sim(y):
  c4 = sum([y for range(y) in range(len(y)) if 0.9 < y[i] <= 1.8])/10000
  print("P(a < x <= b) : {0:8.4f}".format(c4))

The purpose is to basically take the parameter passed to sim() and iterate over the length of it while incrementing by 1 for only those values found true by the condition between 0.9 and 1.8. I’m trying to check each of the n randoms for that condition. Then sum only those that are true.

By the way, the answer should work out around 0.2 — if you want to check it just think about 1.5 being the only way to fit between 0.9 and 1.8.

I appreciate your patience as I’m learning.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:19:26+00:00Added an answer on June 14, 2026 at 11:19 am

    You still need to provide an expression for each loop, and your for y in section is rather out of hand. The following works:

    c4 = sum(1 for i in y if 0.9 < i <= 1.8) / 10000.0
    

    This is the equivalent of:

    count = 0
    for i in y:
        if 0.9 < i <= 1.8:
            count += 1
    c4 = count / 10000.0
    

    Perhaps the 10000.0 should be float(len(y)), but that’s not entirely clear from your example.

    We use 1000.0 or float(len(y)) to avoid using integer division, which would result in 0 as the answer. Alternatively, you can use from __future__ import division to make the / division operator use float division by default, see PEP 238.

    Note that I made it a generator expression for you, no need to store a list first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.