Python: how prevent duplication of randomly chosen key/value pair

Viewed 67
def fill2_line1():
    x2 = random.choice(list(twoSyllValues.items()))
    line1.append(x2)

magicList = ([x[1] for x in line1])
            
if 1 in magicList:
    fill2_line1()
    fill2_line1()
    complete_line = line1[0][0] + " " + line1[1][0] + " " + 
    line1[2][0]
    print(complete_line)

This is the part in question - the whole program is over 150 lines. It works perfectly as is, but sometimes I'm getting duplicated words. To refine the code, I want to make sure that the key in the first random tuple selection is not duplicated in the next two selected tuples IN that list, line1. The only way I can think of is having another dictionary created after the first word selection and then exclude that key,value pair from new dictionary and then make the next two random.choice calls. I feel like there must be a simpler way, but I just don't have the experience yet.

1 Answers

I can think of three ways to solve the problem you are facing. I will write a sample code that you can refer to since I do not know your whole code.

  1. Delete the key before the second random.choice calls and adding them back in if you need them. This is somewhat similar to creating another dictionary but more efficient.
def fill2_line1():
    x2 = random.choice(list(twoSyllValues.items()))
    line1.append(x2)
    return x2

magicList = ([x[1] for x in line1])
            
if 1 in magicList:
    deleted_list = []
    # run first random.choice
    deleted_item = fill2_line1()
    deleted_list.append(deleted_item)
    del twoSyllValues[deleted_item[0]]
    # run second random.choice
    deleted_item = fill2_line1()
    deleted_list.append(deleted_item)
    del twoSyllValues[deleted_item[0]]
    complete_line = line1[0][0] + " " + line1[1][0] + " " 
                    + line1[2][0]
    print(complete_line)
    # add the deleted items back in after work is done
    for k, v in deleted_list:
        twoSyllValues[k] = v
  1. Run random.choice until there is no duplicate item.
def fill2_line1(deleted_set):
    x2 = random.choice(list(twoSyllValues.items()))
    while x2 not in deleted_set:
        x2 = random.choice(list(twoSyllValues.items()))
    line1.append(x2)
    deleted_set.add(x2)

magicList = ([x[1] for x in line1])
            
if 1 in magicList:
    deleted_set = set([])
    fill2_line1(deleted_set)
    fill2_line1(deleted_set)
    complete_line = line1[0][0] + " " + line1[1][0] + " " 
                    + line1[2][0]
    print(complete_line)
  1. Keep another list just for the random.choice
def fill2_line1(items_list):
    x2 = random.choice(items_list)
    line1.append(x2)
    items_list.remove(x2)

magicList = ([x[1] for x in line1])
            
if 1 in magicList:
    items_list = list(twoSyllValues.items())
    fill2_line1(items_list)
    fill2_line1(items_list)
    complete_line = line1[0][0] + " " + line1[1][0] + " " 
                    + line1[2][0]
    print(complete_line)

Hope my answer gave you some ideas!

Related