简体   繁体   中英

Python algorithm - picking objects from a list based on object attributes

I have a custom Python object like:

    ID - string    
    Name - string
    Unit - string
    Weight - float

I have all possible objects in a single list. The Unit property can be a string value from AF. I need to break this list out into all possible combinations of objects that have 1 object with Unit=A, 1 object with Unit=B, all the way to F but in the case of F, I need to have 3 objects where Unit=F. All of the new lists need to contain unique items (ie the Unit=F objects need to be unique) and all the resulting lists also need to be unique. I am completely lost on how to create an algorithm like this. Can someone please provide some ideas and/or psuedo-code?

You may find itertools to be quite helpful in this case, in particular the functions itertools.product and itertools.combinations .

Below is an example created based on your description:

from itertools import product, combinations

class Blob:

    def __init__(self, ID, Name, Unit, Weight):
        self.ID = ID  
        self.Name = Name
        self.Unit = Unit
        self.Weight = Weight

    def __repr__(self):
        return 'Object: ID={ID}, Name={Name}, Unit={Unit}, Weight={Weight}'.format(Name=self.Name, ID=self.ID, Unit=self.Unit, Weight=self.Weight)

blobs = [Blob(1, 'AA', 'A', 1.0),
         Blob(2, 'AA', 'A', 1.0),
         Blob(3, 'AA', 'B', 1.0),
         Blob(4, 'AA', 'B', 1.0),
         Blob(5, 'AA', 'C', 1.0),
         Blob(6, 'AA', 'C', 1.0),
         Blob(7, 'AA', 'D', 1.0),
         Blob(8, 'AA', 'D', 1.0),
         Blob(9, 'AA', 'E', 1.0),
         Blob(10,'AA', 'E', 1.0),
         Blob(11,'AA', 'F', 1.0),
         Blob(12,'AA', 'F', 1.0),
         Blob(13,'AA', 'F', 1.0),
         Blob(14,'AA', 'F', 1.0)]

# Break down into selectable sub-groups by unit name
groups = {k: [b for b in blobs if b.Unit==k] for k in ['A','B','C','D','E','F']}
# Special treatment for unit F: expand to combination chunks of length 3
groups['F'] = combinations(groups['F'], 3)
# Create the list of all combinations
selected = list(product(*groups.values()))

Some example output:

>>> selected[0]
(Object: ID=1, Name=AA, Unit=A, Weight=1.0,
 Object: ID=5, Name=AA, Unit=C, Weight=1.0,
 Object: ID=3, Name=AA, Unit=B, Weight=1.0,
 Object: ID=9, Name=AA, Unit=E, Weight=1.0,
 Object: ID=7, Name=AA, Unit=D, Weight=1.0,
 (Object: ID=11, Name=AA, Unit=F, Weight=1.0,
  Object: ID=12, Name=AA, Unit=F, Weight=1.0,
  Object: ID=13, Name=AA, Unit=F, Weight=1.0))

>>> selected[1]
(Object: ID=1, Name=AA, Unit=A, Weight=1.0,
 Object: ID=5, Name=AA, Unit=C, Weight=1.0,
 Object: ID=3, Name=AA, Unit=B, Weight=1.0,
 Object: ID=9, Name=AA, Unit=E, Weight=1.0,
 Object: ID=7, Name=AA, Unit=D, Weight=1.0,
 (Object: ID=11, Name=AA, Unit=F, Weight=1.0,
  Object: ID=12, Name=AA, Unit=F, Weight=1.0,
  Object: ID=14, Name=AA, Unit=F, Weight=1.0))

You may wish to flatten the elements with F as the unit in the final list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM