Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sorting tuples by randomly generated weight

The problem:
I have a list of tuples containing a string and an int. I have managed sorting the tuples by their second value using the built in sorted function and lambda. The problem with that is that I also need to group up the tuples in case they have the same int. After they are sorted in groups I need to generate a random number between 1 and 6 and based on the highest put the corresponding tuple inside a final list which is meant to represent the truly sorted tuples.

Context: The algorithm is meant to be an Initiative roller for a roleplay game and comparing one value to the next does not suffice, all tuples that have the same int value need to be compared at the same time and not one after another.

Current code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

iniList = [('Enemy 3', 15), ('Aldare', 14), ('Enemy 2', 14), ('Enemy 5', 14), ('Enemy 1', 13), ('Enemy 4', 13)]
finalIniList = [] #the list meant to contain the tuples when they are sorted
iniGroups = []
    currentIni = iniList[0][1]
    currentIniGroup = []
    finalIniList = []
    for x in range(len(iniList)):
        if(currentIni == iniList[x][1]):
            currentIniGroup.append(iniList[x])
            if(x == len(iniList) - 1): iniGroups.append(currentIniGroup)
        else:
            iniGroups.append(currentIniGroup)
            currentIniGroup = []
            currentIniGroup.append(iniList[x])
            currentIni = iniList[x][1]
            if(x == len(iniList) - 1): iniGroups.append(currentIniGroup)
for item in iniGroups:
        print(item)

Output:

[('Enemy 3', 15)]
[('Aldare', 14), ('Enemy 2', 14), ('Enemy 5', 14)]
[('Enemy 1', 13), ('Enemy 4', 13)]

>Solution :

Given

iniList = [('Enemy 3', 15), ('Aldare', 14), ('Enemy 2', 14), ('Enemy 5', 14), ('Enemy 1', 13), ('Enemy 4', 13)]

use itertools.groupby and random.sample:

from random import sample
from itertools import groupby

finalIniList = [(group[0],
                 sample(list_:=[tup[0] for tup in group[1]],k=len(list_)),
                )
                for group in groupby(iniList,key=lambda tup: tup[1])
               ]

to get something like

>>> finalIniList
[
 (highest_initiative, ['shuffled', 'list', 'of', 'entities']),
 (lower_initiative,   ['entity']),
 (lowest_initiative,  ['some', 'more', 'entities', 'randomly', 'ordered']),
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading