Hi All

How can I select some random names from a list, so that in next time, those previously selected names become omitted from the list?
for example, say we have 100 names in test.txt file, and we want to select 3 names randomly. then by recalling the selection function, we again select 3 names from remaining 97 names, and so on.

Please Note: I cant use pickle or cpickle, because I'm using python 2.2.2(pys60 1.45) on my phone and it doesnt support pickle.

here is my code, but every time I run it, it again selects from original 100 names.

f = open('e:\\test.txt', 'r')
names = f.readlines()
used = []
def shuffle(x):
    i = len(x)
    while i > 1:
        i = i - 1
        j = randrange(i)
        x[j], x[i] = x[i], x[j]
    return

def randSelect():
    shuffle(names)
    for i in xrange(3):
        x = names[i]
        if x not in used:
            names.pop(i)
            used.append(x)
        print x

randSelect()

any help or suggestions is highly appreciated.

:) well just so ya know there is a random module with things like shuffle.

import random
f=open('names.txt','r')
names=f.readlines()
f.close()
selected_names=set()
while len(selected_names)<5:
   random.shuffle(names)
   indx=random.randint(0,len(names)-1)
   selected_names.add(names[indx])

a set will get rid of any duplicates. but you could also do a "if key not in list" kind of thing:

selected_names=[]
while len(selected_names)<5:
   random.shuffle(names)
   indx=random.randint(0,len(names)-1)
   if names[indx] not in selected_names:
      selected_names.append(names[indx])
   else:
      pass

Thank you pyguy62.
but unfortunately "set()" is not supported by the Python version I must use.
Is there any way to define it manually(i.e. define the set function in this script)?

In versions of python >= 2.3, there is a function random.sample() which does exactly what you need.

>>> L = range(100)
>>> import random
>>> random.sample(L, 3)
[32, 93, 51]

So, I think it would be a good solution to reuse the source code to write your own sample() function. Here is the module random from python 2.6.

Thank you dear Gribouillis
I'll try what you suggested and will tell the results soon.

thank you all again.

If you have not set, you can set dictionary used with that key to value and use in dictionary test or dictionary.haskey. Even for such short stuff simple list implementation should be enough.

Here simple start:

from __future__ import generators

class Set(dict):
    def __init__(self, *args):
        dict.__init__(self)
        self.update((v, True) for v in args)

    def __str__(self):
        return '{'+', '.join(repr(item) for item in self.keys())+'}'

    def add(self, value):
        self[value] = True

    def remove(self, value):
        del self[value]


my_set = Set('a', 'b', 'c')
print my_set
my_set.add('d')
print my_set
my_set.remove('a')
print my_set

your_set= Set('I', 'you', 'he', 'she', 'I', 'you', 'we')
print your_set

Yesss! thats it:D
my problem solved with pyTony's Set class.
Thanks so much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.