Skip to main content
added 112 characters in body
Source Link
David Owens
  • 685
  • 7
  • 29

If I understand correctly, it seems like you are trying to append to a file after each iteration (if your data file already exists), if so use:

f = open("data_file.txt", "a+")

The "a+" indicates you wish to append with any subsequent calls to write. Docs here. If you are doing a bunch of iterations, be sure to open the file outside of any loops.

Of course you could also loop to write each element of the new row after each iteration and then just write a newline with something like this:

#open file
for item in newRow:
  #Your formatting will determine the specifics of this write
  f.write(item)
f.write("\n")

Something like this does what I think you are getting at:

# Use with for file safety
with open('output.txt', 'w') as f:    
    for R in range(no_of_walkers):
        x = 0.0
        for t in range(t_max):
            x = x + np.random.randint(-1, 2)
            f.write(str(x) + ' ')
        f.write('\n')

output.txt

1.0 1.0 1.0 1.0 1.0 0.0 0.0 -1.0 -2.0 -2.0 
0.0 1.0 0.0 0.0 -1.0 0.0 1.0 0.0 1.0 1.0 
-1.0 -1.0 -1.0 -2.0 -1.0 -1.0 -2.0 -1.0 -2.0 -3.0 
-1.0 -1.0 0.0 1.0 0.0 1.0 2.0 1.0 2.0 1.0 
1.0 0.0 -1.0 -2.0 -1.0 -1.0 0.0 1.0 1.0 0.0 

As for efficiency, I'm pretty sure the way you were originally trying to write is better. See this post.

If I understand correctly, it seems like you are trying to append to a file after each iteration (if your data file already exists), if so use:

f = open("data_file.txt", "a+")

The "a+" indicates you wish to append with any subsequent calls to write. Docs here. If you are doing a bunch of iterations, be sure to open the file outside of any loops.

Of course you could also loop to write each element of the new row after each iteration and then just write a newline with something like this:

#open file
for item in newRow:
  #Your formatting will determine the specifics of this write
  f.write(item)
f.write("\n")

Something like this does what I think you are getting at:

# Use with for file safety
with open('output.txt', 'w') as f:    
    for R in range(no_of_walkers):
        x = 0.0
        for t in range(t_max):
            x = x + np.random.randint(-1, 2)
            f.write(str(x) + ' ')
        f.write('\n')

output.txt

1.0 1.0 1.0 1.0 1.0 0.0 0.0 -1.0 -2.0 -2.0 
0.0 1.0 0.0 0.0 -1.0 0.0 1.0 0.0 1.0 1.0 
-1.0 -1.0 -1.0 -2.0 -1.0 -1.0 -2.0 -1.0 -2.0 -3.0 
-1.0 -1.0 0.0 1.0 0.0 1.0 2.0 1.0 2.0 1.0 
1.0 0.0 -1.0 -2.0 -1.0 -1.0 0.0 1.0 1.0 0.0 

As for efficiency, I'm pretty sure the way you were originally trying to write is better.

If I understand correctly, it seems like you are trying to append to a file after each iteration (if your data file already exists), if so use:

f = open("data_file.txt", "a+")

The "a+" indicates you wish to append with any subsequent calls to write. Docs here. If you are doing a bunch of iterations, be sure to open the file outside of any loops.

Of course you could also loop to write each element of the new row after each iteration and then just write a newline with something like this:

#open file
for item in newRow:
  #Your formatting will determine the specifics of this write
  f.write(item)
f.write("\n")

Something like this does what I think you are getting at:

# Use with for file safety
with open('output.txt', 'w') as f:    
    for R in range(no_of_walkers):
        x = 0.0
        for t in range(t_max):
            x = x + np.random.randint(-1, 2)
            f.write(str(x) + ' ')
        f.write('\n')

output.txt

1.0 1.0 1.0 1.0 1.0 0.0 0.0 -1.0 -2.0 -2.0 
0.0 1.0 0.0 0.0 -1.0 0.0 1.0 0.0 1.0 1.0 
-1.0 -1.0 -1.0 -2.0 -1.0 -1.0 -2.0 -1.0 -2.0 -3.0 
-1.0 -1.0 0.0 1.0 0.0 1.0 2.0 1.0 2.0 1.0 
1.0 0.0 -1.0 -2.0 -1.0 -1.0 0.0 1.0 1.0 0.0 

As for efficiency, I'm pretty sure the way you were originally trying to write is better. See this post.

deleted 63 characters in body
Source Link
David Owens
  • 685
  • 7
  • 29

If I understand correctly, it seems like you are trying to append to a file after each iteration (if your data file already exists), if so use:

f = open("data_file.txt", "a+")

The "a+" indicates you wish to append with any subsequent calls to write. Docs here. If you are doing a bunch of iterations, be sure to open the file outside of any loops.

Of course you could also loop to write each element of the new row after each iteration and then just write a newline with something like this:

#open file
for item in newRow:
  #Your formatting will determine the specifics of this write
  f.write(item)
f.write("\n")

Something like this does what I think you are getting at:

# Use with for file safety
with open('output.txt', 'w') as f:    
    for R in range(no_of_walkers):
        x = 0.0
        for t in range(t_max):
            x = x + np.random.randint(-1, 2)
            f.write(str(x) + ' ')
        f.write('\n')

output.txt

1.0 1.0 1.0 1.0 1.0 0.0 0.0 -1.0 -2.0 -2.0 
0.0 1.0 0.0 0.0 -1.0 0.0 1.0 0.0 1.0 1.0 
-1.0 -1.0 -1.0 -2.0 -1.0 -1.0 -2.0 -1.0 -2.0 -3.0 
-1.0 -1.0 0.0 1.0 0.0 1.0 2.0 1.0 2.0 1.0 
1.0 0.0 -1.0 -2.0 -1.0 -1.0 0.0 1.0 1.0 0.0 

As for efficiency, someone with more knowledge will have to comment onI'm pretty sure the best approach ifway you really are running this for a "large" number of walkerswere originally trying to write is better.

If I understand correctly, it seems like you are trying to append to a file after each iteration (if your data file already exists), if so use:

f = open("data_file.txt", "a+")

The "a+" indicates you wish to append with any subsequent calls to write. Docs here. If you are doing a bunch of iterations, be sure to open the file outside of any loops.

Of course you could also loop to write each element of the new row after each iteration and then just write a newline with something like this:

#open file
for item in newRow:
  #Your formatting will determine the specifics of this write
  f.write(item)
f.write("\n")

Something like this does what I think you are getting at:

# Use with for file safety
with open('output.txt', 'w') as f:    
    for R in range(no_of_walkers):
        x = 0.0
        for t in range(t_max):
            x = x + np.random.randint(-1, 2)
            f.write(str(x) + ' ')
        f.write('\n')

output.txt

1.0 1.0 1.0 1.0 1.0 0.0 0.0 -1.0 -2.0 -2.0 
0.0 1.0 0.0 0.0 -1.0 0.0 1.0 0.0 1.0 1.0 
-1.0 -1.0 -1.0 -2.0 -1.0 -1.0 -2.0 -1.0 -2.0 -3.0 
-1.0 -1.0 0.0 1.0 0.0 1.0 2.0 1.0 2.0 1.0 
1.0 0.0 -1.0 -2.0 -1.0 -1.0 0.0 1.0 1.0 0.0 

As for efficiency, someone with more knowledge will have to comment on the best approach if you really are running this for a "large" number of walkers.

If I understand correctly, it seems like you are trying to append to a file after each iteration (if your data file already exists), if so use:

f = open("data_file.txt", "a+")

The "a+" indicates you wish to append with any subsequent calls to write. Docs here. If you are doing a bunch of iterations, be sure to open the file outside of any loops.

Of course you could also loop to write each element of the new row after each iteration and then just write a newline with something like this:

#open file
for item in newRow:
  #Your formatting will determine the specifics of this write
  f.write(item)
f.write("\n")

Something like this does what I think you are getting at:

# Use with for file safety
with open('output.txt', 'w') as f:    
    for R in range(no_of_walkers):
        x = 0.0
        for t in range(t_max):
            x = x + np.random.randint(-1, 2)
            f.write(str(x) + ' ')
        f.write('\n')

output.txt

1.0 1.0 1.0 1.0 1.0 0.0 0.0 -1.0 -2.0 -2.0 
0.0 1.0 0.0 0.0 -1.0 0.0 1.0 0.0 1.0 1.0 
-1.0 -1.0 -1.0 -2.0 -1.0 -1.0 -2.0 -1.0 -2.0 -3.0 
-1.0 -1.0 0.0 1.0 0.0 1.0 2.0 1.0 2.0 1.0 
1.0 0.0 -1.0 -2.0 -1.0 -1.0 0.0 1.0 1.0 0.0 

As for efficiency, I'm pretty sure the way you were originally trying to write is better.

added 1 character in body
Source Link
David Owens
  • 685
  • 7
  • 29

If I understand correctly, it seems like you are trying to append to a file after each iteration (if your data file already exists), if so use:

f = open("data_file.txt", "a+")

The "a+" indicates you wish to append with any subsequent calls to write. Docs here. If you are doing a bunch of iterations, be sure to open the file outside of any loops.

Of course you could also loop to write each element of the new row after each iteration and then just write a newline with something like this:

#open file
for item in newRow:
  #Your formatting will determine the specifics of this write
  f.write(item)
f.write("\n")

Something like this seems to dodoes what I think you are getting at:

# Use with for file safety
with open('output.txt', 'w') as f:    
    for R in range(no_of_walkers):
        x = 0.0
        for t in range(t_max):
            x = x + np.random.randint(-1, 2)
            f.write(str(x) + ' ')
        f.write('\n')

output.txt

1.0 1.0 1.0 1.0 1.0 0.0 0.0 -1.0 -2.0 -2.0 
0.0 1.0 0.0 0.0 -1.0 0.0 1.0 0.0 1.0 1.0 
-1.0 -1.0 -1.0 -2.0 -1.0 -1.0 -2.0 -1.0 -2.0 -3.0 
-1.0 -1.0 0.0 1.0 0.0 1.0 2.0 1.0 2.0 1.0 
1.0 0.0 -1.0 -2.0 -1.0 -1.0 0.0 1.0 1.0 0.0 

As for efficiency, someone with more knowledge will have to comment on the best approach if you really are running this for a "large" number of walkers.

If I understand correctly, it seems like you are trying to append to a file after each iteration (if your data file already exists), if so use:

f = open("data_file.txt", "a+")

The "a+" indicates you wish to append with any subsequent calls to write. Docs here. If you are doing a bunch of iterations, be sure to open the file outside of any loops.

Of course you could also loop to write each element of the new row after each iteration and then just write a newline with something like this:

#open file
for item in newRow:
  #Your formatting will determine the specifics of this write
  f.write(item)
f.write("\n")

Something like this seems to do what you are getting at:

# Use with for file safety
with open('output.txt', 'w') as f:    
    for R in range(no_of_walkers):
        x = 0.0
        for t in range(t_max):
            x = x + np.random.randint(-1, 2)
            f.write(str(x) + ' ')
        f.write('\n')

output.txt

1.0 1.0 1.0 1.0 1.0 0.0 0.0 -1.0 -2.0 -2.0 
0.0 1.0 0.0 0.0 -1.0 0.0 1.0 0.0 1.0 1.0 
-1.0 -1.0 -1.0 -2.0 -1.0 -1.0 -2.0 -1.0 -2.0 -3.0 
-1.0 -1.0 0.0 1.0 0.0 1.0 2.0 1.0 2.0 1.0 
1.0 0.0 -1.0 -2.0 -1.0 -1.0 0.0 1.0 1.0 0.0 

As for efficiency, someone with more knowledge will have to comment on the best approach if you really are running this for a "large" number of walkers.

If I understand correctly, it seems like you are trying to append to a file after each iteration (if your data file already exists), if so use:

f = open("data_file.txt", "a+")

The "a+" indicates you wish to append with any subsequent calls to write. Docs here. If you are doing a bunch of iterations, be sure to open the file outside of any loops.

Of course you could also loop to write each element of the new row after each iteration and then just write a newline with something like this:

#open file
for item in newRow:
  #Your formatting will determine the specifics of this write
  f.write(item)
f.write("\n")

Something like this does what I think you are getting at:

# Use with for file safety
with open('output.txt', 'w') as f:    
    for R in range(no_of_walkers):
        x = 0.0
        for t in range(t_max):
            x = x + np.random.randint(-1, 2)
            f.write(str(x) + ' ')
        f.write('\n')

output.txt

1.0 1.0 1.0 1.0 1.0 0.0 0.0 -1.0 -2.0 -2.0 
0.0 1.0 0.0 0.0 -1.0 0.0 1.0 0.0 1.0 1.0 
-1.0 -1.0 -1.0 -2.0 -1.0 -1.0 -2.0 -1.0 -2.0 -3.0 
-1.0 -1.0 0.0 1.0 0.0 1.0 2.0 1.0 2.0 1.0 
1.0 0.0 -1.0 -2.0 -1.0 -1.0 0.0 1.0 1.0 0.0 

As for efficiency, someone with more knowledge will have to comment on the best approach if you really are running this for a "large" number of walkers.

added 270 characters in body
Source Link
David Owens
  • 685
  • 7
  • 29
Loading
added 250 characters in body
Source Link
David Owens
  • 685
  • 7
  • 29
Loading
added 250 characters in body
Source Link
David Owens
  • 685
  • 7
  • 29
Loading
added 214 characters in body
Source Link
David Owens
  • 685
  • 7
  • 29
Loading
added 2 characters in body
Source Link
David Owens
  • 685
  • 7
  • 29
Loading
Source Link
David Owens
  • 685
  • 7
  • 29
Loading