954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

coverting function to a list comprehension

Hi Guys, Can anyone help me redo this, so it'll be a list comprehension instead?


def parseAnnotation(annotation):
ExonStartAndStop = []
d = annotation.split(',')
for i in d:
removeparenteses = i[1:-1]
numbers = removeparenteses.split('..')
ExonStartAndStop.append((int(numbers[0]),int(numbers[1])))
return ExonStartAndStop
example usage:
print parseAnnotation('(1834..2736)') ---> [(1834, 2736))

Petee.bill
Newbie Poster
2 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

Here is a way

def parse(annotation):
    return list((int(x), int(y)) for (x,y) in (item.strip(" ()").split('..') for item in annotation.split(',')))

print parse('(1834..2736), (348..7734)')

""" my output -->
[(1834, 2736), (348, 7734)]
"""


Edit: try and use the[code] button in the editor window at the bottom of this page!
Edit: it seems that pyTony gave you another nice solution last month http://www.daniweb.com/software-development/python/threads/400630/1715515#post1715515

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You