dimanche 26 avril 2020

How to best diplay a random string, with some strings weighted heavier than others

I am trying to display a random string but would like some strings to occur more often than others. My current strategy is with nested dictionaries for ease of updating and the 'choices' function.

msg_list = {

    'msg_1': {
        'msg': 'Hi',
        'weight': 40,
        'size': 15,
        },

    'msg_2': {
        'msg': 'hello',
        'weight': 50,
        'size': 12,
        },

    'msg_3': {
        'msg': "What's up",
        'weight': 10,
        'size': 50,
        },
    }

message = choices(msg_list['msg'], msg_list['weight'])
string = message['msg']

This obviously doesn't work, and I imagine I could build the lists with a loop, but I am curious if there is a faster way of doing this. Thanks!




Aucun commentaire:

Enregistrer un commentaire