kpp9c wrote: > I have a question... and ... whew ... i am gonna be honest, i haven't > the slightest clue how to even start ... i am not sure if i used up all > my good will here or can take a mulligan.. i love to try to at least > post some lame broken code of my own at first... but like i said, not > being a math person i am not even sure how to start or if it is even > possible. > > here's the deal... i have a dictionary that defines some collections.. > like so: > > sets = { ('one') : [0, 4, 7, 9 ], > ('two') : [0, 3, 7, 9 ], > ('three') : [0, 4, 7, 11], > ('four') : [0, 3, 7, 10 ], > ('five') : [0, 4, 7, 10 ], > ('six') : [0, 4, 8, 10 ], > ('seven') : [0, 3, 6, 10], > ('eight') : [0, 3, 6, 9 ], > ('nine') : [0, 3, 7, 11 ], > ('ten') : [0, 5, 7, 10 ] } > > I every time i call this function i would like like it to return a > collection at random, any collection, so long as it has all but one > element that is the same. So if i grab [0, 4, 7, 9 ] as my first set > my next set could be: [0, 3, 7, 9 ], or [0, 4, 8, 9 ], or [0, 4, 7, > 10], or [1, 4, 7, 9 ], since all these sets contain 3 elements in > common with the first, and only one that is new or different... but if > my first run give me: [0, 4, 7, 9 ] i would not get [0, 5, 7, 10 ], > since this is set has 2 elements that are unique. The goal it to move > from set to set to set to set always with a maximum of overlap & common > elements. >
probably not very efficient but I think it roughly does what you want. (maybe add a boolean 'sort' parameter, to select sorted output or not): # k is the length of the required output lists, 0<k<n # n is the number of lists to output import random alphabet = [ 0, 3, 4, 5, 6, 7, 8, 9, 10, 11 ] def randomiser( a_list, n, k ): d = len( a_list ) choice = range(d) vector = [] for i in range(k): vector.append( choice.pop(random.randint(0,d-i-1)) ) yield [ a_list[s] for s in vector ] #yield sorted( a_list[s] for s in vector ) for _ in range(n): rand_vector_idx = random.randint(0,k-1) rand_choice_idx = random.randint(0,d-k-1) rand_vector_val = vector[rand_vector_idx] #remember old value vector[rand_vector_idx] = choice.pop( rand_choice_idx ) choice.append( rand_vector_val ) #add old value back to choice yield [ a_list[t] for t in vector ] #yield sorted( a_list[t] for t in vector ) print for item in randomiser(alphabet, 10, 4): print item [3, 6, 9, 8] [3, 6, 9, 0] [3, 11, 9, 0] [3, 11, 10, 0] [9, 11, 10, 0] [9, 11, 7, 0] [9, 4, 7, 0] [9, 3, 7, 0] [9, 11, 7, 0] [9, 11, 7, 8] [9, 11, 10, 8] Gerard -- http://mail.python.org/mailman/listinfo/python-list