python - Give equal items in list a unique number if items > 1 -


i made functionality, not happy quantity of code. end result good, believe can made easier, don't know how.

the functionality: if equal items > 1 in list equal items getting unique set number. below made unit test end result. i'm not happy class creatset. can advise me how can implemented better.

import unittest   class curtain(object):   def __init__(self, type, fabric, number):       self.type = type       self.fabric = fabric       self.number = number       self.set_number = none    def __str__(self):       return '%s %s %s %s' % (self.number, self.type, self.fabric, self.set_name)    def __eq__(self, other):       return self.type == other.type , self.fabric == other.fabric  class creatset(object):   def make_unique(self, original_list):       checked = []       e in original_list:           # if curtain: type , fabric equal           if e not in checked:               checked.append(e)       return checked    def create_set(self, curtains):       # uniuqe items in list       unique_list = self.make_unique(curtains)       result = []       x in unique_list:           # create set list           set_range = []           y in curtains:               if y == x:                   set_range.append(y)           # add set range list           result.append(set_range)        # create set number       set_result = []       set_number = 0       x in result:           if len(x) == 1:               set_result.append(x[0])           else:               set_number += 1               y in x:                   y.set_number = set_number                   set_result.append(y)       # return list ordered number       return sorted(set_result, key=lambda curtain: curtain.number)  class testcreateset(unittest.testcase):     def setup(self):       self.curtains = []       self.curtains.append(curtain('pleatcurtain', 'pattern', 0))       self.curtains.append(curtain('pleatcurtain', 'plain', 1))       self.curtains.append(curtain('pleatcurtain', 'pattern', 2))       self.curtains.append(curtain('foldcurtain', 'pattern', 3))       self.curtains.append(curtain('pleatcurtain', 'plain', 4))       self.curtains.append(curtain('foldcurtain', 'plain', 5))       self.curtains.append(curtain('pleatcurtain', 'pattern', 6))       self.curtains.append(curtain('foldcurtain', 'pattern', 7))     def test_auto_set(self):       creat_set = creatset()       result = creat_set.create_set(self.curtains)       # creating set       self.assertequal(result[0].set_number, 1)  # pleatcurtain, pattern       self.assertequal(result[1].set_number, 2)  # pleatcurtain, plain       self.assertequal(result[2].set_number, 1)  # pleatcurtain, pattern       self.assertequal(result[3].set_number, 3)  # foldcurtain, pattern       self.assertequal(result[4].set_number, 2)  # pleatcurtain, plain       self.assertequal(result[5].set_number, none)  # foldcurtain, plain       self.assertequal(result[6].set_number, 1)  # pleatcurtain, pattern       self.assertequal(result[7].set_number, 3)  # foldcurtain, pattern   if __name__ == '__main__':     unittest.main() 


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -