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

  • SEARCH
  • Home
  • 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 6703473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:10:32+00:00 2026-05-26T07:10:32+00:00

In the following code, when I print the row in the first loop after

  • 0

In the following code, when I print the row in the first loop after doing a bunch of manipulations with it I see the results that I want. However, after I exit the first loop, I find that I get a different result in the variable Dataset. I know this is a scoping issue but I cannot figure out what the problem is and how to get my desired result which is shown with the first “print” statement. Thanks for your help

import random
random.seed(1234567)
Key=[[.5,.5]]
Dataset=[[0]+[0]*1]*int(10/2) +[[1]+[0]*1]*int(10/2)

print "results I need"
for row in Dataset:
    response=row[0]
    for i in xrange(len(Key)):
        if random.random() < Key[i][response]: 
            row[i+1]=response
        else: 
            row[i+1]=1-response
    print row

print "Results I get"
for row in Dataset:
    print row
  • 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-05-26T07:10:32+00:00Added an answer on May 26, 2026 at 7:10 am
    • Python has reference semantics. Your original Dataset contains multiple references to the common [0, 0] and [1, 0] sublists. (I’m also at a loss to understand why you’re making the initialization so complex.)

    You could fix this by using [[0, 0] for i in xrange(5)] + [[1, 0] for i in xrange(5)].

    • The rest of your logic is also needlessly complex. We can use a conditional expression to merge the two assignments together, and we should be using direct iteration instead of iterating over an artificial list of indices (xrange).

    That looks like:

    for row in Dataset:
        response = row[0]
        for k in Key:
            row[i + 1] = response if random.random() < k[response] else 1 - response
    
    • However, this is still not the solution we’re looking for. There’s really no reason to put all those zeros in the initialization data that will be overwritten and never read in the loop. The [i + 1] indexing is also clumsy. A simpler solution: start with a list of just the first values for the sublists, and within the loop, construct the rest of each sublist (we can do this with a list comprehension) and prepend the first element. Instead of replacing the elements of the Dataset in place, we can use another list comprehension to bind them together.

    That gives us:

    import random
    random.seed(1234567)
    Key=[[.5,.5]]
    Initial_Conditions=[0]*int(10/2) +[1]*int(10/2)
    
    Dataset = [
        [i] + [
            i if random.random() < k[i] else 1 - i
            for k in Key
        ]
        for i in Initial_Conditions
    ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code print Starting stage 1<br> # Something that takes about
I expected the following code to print 8, 111 and 999. I supposed that
Let's say I have following code cursor = connection.cursor() cursor.execute(query) after that point I
Why does the following code print ‘read(): Resource temporarily unavailable’ 80% of the time?
What will the following code print? print ‘’four’’ * 200;
How to print multiply flowdocumets in a batch? Following code should print different documents
Boy, this one is really weird. I expect the following code to print 1990,
Any idea why the following code doesn't print the amount of characters in the
How to print the following code to a .txt file y = '10.1.1.' #
The following code produces the output xyz a = %w{x y z} print a.to_s

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.