Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. '''
  2. input:
  3. category: a string which represents a category
  4. howto: choices of how you want the new category be extracted,for example, as of 'A,B,C,D,E'
  5. 'first': the first tier category, 'A'
  6. 'last': the last tier category, 'E'
  7. 'first_half': the first half of the categories, 'A,B', if the just 1 tier, first and second halves will be the same.
  8. 'second_half':the second half of the categories, 'C,D,E'
  9. 'fill': fill the missing categories upto 5 tier with the fill_string provided.
  10. For example, for 'A,B,C', it will be'A,B,C,fill_str,fill_str'
  11. ''
  12. output:return a categoty string with respect to the 'howto' methods mentioned above
  13. '''
  14. def new_category(category, howto='last', fill_str='AbCxYZ'):
  15. temp = category.split(',')
  16. if howto=='first':
  17. return temp[0]
  18. elif howto=='last':
  19. return temp[-1]
  20. elif howto=='first_half':
  21. if len(temp)==1:
  22. return temp[0]
  23. else:
  24. return ','.join(i for i in temp[:len(temp)//2])
  25. elif howto=='second_half':
  26. return ','.join(i for i in temp[len(temp)//2:])
  27. elif howto=='filled':
  28. while(len(temp)<5):
  29. temp.append(fill_str)
  30. return ','.join(i for i in temp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement