1

Let's say that I have a separate text file that contains a series of numbers:

1
2
3

And so on. Is it possible for a Python program to randomly choose one of the numbers in that text file, and then remove that number from the text file? I know it is possible to do the first, but the I am struggling with the second part.

If it helps, the list is about 180000 numbers long. I am very new at this. The idea is to randomly assign a player a number, and then remove that number from the list so another player can't get it.

MisterPuggles
  • 129
  • 1
  • 3
  • 18
  • It's possible for *any* programming language to 1) read a file into a list, 2) perform some update(s) on the list, 3) write the updated list back out to a new version of the file. – paulsm4 Dec 03 '13 at 05:53
  • 1
    I think your question is already answered here, but... do you really need a text file? If you have to do this task many times with large files, rewriting your txt-s will get very slow. If you are not bound to txt-s, try something like pickle or numpy's binary (npy) format, in which you can store your data much more space and time efficiently. – leeladam Dec 03 '13 at 05:58

4 Answers4

2

Do you actually have 180,000 players? If not, what about solving the problem the other way round:

  • Create a file listing the IDs already used
  • For each new user:
    1. Create a fairly large random ID (like the ones in your current file)
    2. Run through the 'used' IDs in your file and check your new ID doesn't collide with an existing one - if it does, generate new ones until there is no collision
    3. Append the new ID to your file

This will be much faster than reading, checking and writing a large file each time. If your IDs are large, you won't get many collisions.

You could also optimise the process, for example using a two-part ID consisting of today's date and a random number. You would then keep a file for each day, and only need to check for collisions with the IDs issued today.

Aaron Lockey
  • 777
  • 1
  • 6
  • 15
1

The suggestion I would say is that, you read the entire text file, make whatever changes you want to do to it, and then rewrite over the original contents of the file, which is the best way as far as i know

Aswin Murugesh
  • 9,508
  • 10
  • 34
  • 65
1

If the file is small, read the whole thing into a list, delete a value from the list, then write the new list to a temp file. Finally, rename the temp file to the original filename.

If the file is large, read the file one line at a time, writing the values (except one) to a temp file. Then rename the temp file to the original filename.

dstromberg
  • 6,356
  • 20
  • 22
0

Like dstromberg said, if the file is small, check out the documentation on file IO and this answer's strategy for writing lists to a file. Note that writelines() "does not add line separators."

Community
  • 1
  • 1
yurrriq
  • 633
  • 7
  • 11
  • [Aaron's answer](http://stackoverflow.com/a/20344316/1793234) is great. You can still use the information from the links above to write to a plain text file, but I agree with [leeladam](http://stackoverflow.com/questions/20343836/choosing-random-number-from-list-and-then-removing-it/20344006#comment30366541_20343836) that [`pickle`](http://docs.python.org/2/library/pickle.html) or [NumPy](http://www.numpy.org) would probably be better. – yurrriq Dec 03 '13 at 06:34