0

Suppose I have this following list

 [('2015-2016-regular', '2016-playoff'), ('2016-2017-regular', '2017-playoff'), ('2017-2018-regular',)]

which represents the two previous complete NHL years and the current one.

I would like to convert it so that It will give me

[('Regular Season 2015-2016 ', 'Playoff 2016'), ('Regular Season 2016-2017', 'Playoff 2017'), ('Regular Season 2017-2018 ',)]

My English is bad and those writing will be used as titles. Are there any errors in the last list?

How could I construct a function which will do such conversions in respecting the 80 characters long norm?

2 Answers2

0

This is a little hacky, but it's an odd question and use case so oh well. Since you have a really limited set of replacements, you can just use a dict to define them and then use a list comprehension with string formatting:

repl_dict = {
    '-regular': 'Regular Season ',
    '-playoff': 'Playoff '
}
new_list = [
    tuple(
        '{}{}'.format(repl_dict[name[name.rfind('-'):]], name[:name.rfind('-')]) 
        for name in tup
    ) 
    for tup in url_list
]
jack6e
  • 1,512
  • 10
  • 12
0

I tried this. So, I unpacked the tuple. I know where I have to split and which parts to join and did the needful. capitalize() function is for making the first letter uppercase. Also I need to be careful whether the tuple has one or two elements.

l = [('2015-2016-regular', '2016-playoff'), ('2016-2017-regular', '2017-playoff'), ('2017-2018-regular',)]
ans = []
for i in l:
    if len(i)==2:
        fir=i[0].split('-')
        sec = i[1].split('-')
        ans.append((fir[2].capitalize()+" "+fir[0]+'-'+fir[1],sec[1].capitalize()+" "+sec[0]))
    else:
        fir=i[0].split('-')
        ans.append((fir[2].capitalize()+" "+fir[0]+'-'+fir[1],))
print ans

Output:

[('Regular 2015-2016', 'Playoff 2016'), ('Regular 2016-2017', 'Playoff 2017'), ('Regular 2017-2018',)]
Miraj50
  • 4,257
  • 1
  • 21
  • 34