Say I have the following lists:
List1=['Name1','Name3','Color1','Size2','Color3','Color2','Name2','Size1', 'ID']
List2=['ID','Color1','Color2','Size1','Size2','Name1','Name2']
Each list will have element named "ID" variable and then 3 other categories (Name, Color, and Size) of which there is an unpredetermined number of elements in each category.
I want to sort these variables without knowing how many there will be in each category with the following 'sort list':
SortList=['ID','Name','Size','Color']
I can get the desired output (see below) although I imagine there is a better / more pythonic way of doing so.
>>> def SortMyList(MyList,SortList):
... SortedList=[]
... for SortItem in SortList:
... SortItemList=[]
... for Item in MyList:
... ItemWithoutNum="".join([char for char in Item if char.isalpha()])
... if SortItem==ItemWithoutNum:
... SortItemList.append(Item)
... if len(SortItemList)>1:
... SortItemList=[SortItem+str(I) for I in range(1,len(SortItemList)+1)]
... for SortedItem in SortItemList:
... SortedList.append(SortedItem)
... return SortedList
...
>>>
>>> SortMyList(List1, SortList)
['ID', 'Name1', 'Name2', 'Name3', 'Size1', 'Size2', 'Color1', 'Color2', 'Color3']
>>> SortMyList(List2, SortList)
['ID', 'Name1', 'Name2', 'Size1', 'Size2', 'Color1', 'Color2']
>>>
Any suggestions as to how my methodology or my code can be improved?
Name11which you would want afterName10and beforeName12?pep8andpyflakesare two linters that will help point out at least the style problems in your code. Though you may want to take their advice with a grain of salt