Quote:
|
Originally Posted by DaWei
Obviously, some Python brevity is of the same type as the C/C++ ternary operator, which is to say, not really more efficient, but more efficient to write. How much do you use this in your Python? Here's an example:
for sets in re.finditer (pattern, doc, re.I):
medList.append ([found for found in sets.group (1, 2, 3) if found != None])
tagList.append (medList) The second line is an example, and all three lines could be combined. How much of this do you do? Just curious.
|
In general I'd make that five lines (and if the listcomp were complicated I'd use more):
for sets in re.finditer(pattern, doc, re.I):
matches = [found for found in sets.group (1, 2, 3)
if found is not None] # "is not" is safer practice in general
medList.append (matches)
tagList.append (medList) It's rare that a function in Python will go over a page for me. If it does, I try to split things up. For me the extra fraction of a second overhead in the funccall is neither here nor there and readability should trump brevity. The entire structure isn't there, but assuming that you're gathering tags all at the same time, I'd probably use a generator instead:
def gather(pattern, doc):
yield [[found for found in sets.group (1, 2, 3)
if found is not None]
for sets in re.finditer(pattern, doc, re.I)]
def run():
taglist = [gather(pattern, doc)
for pattern, doc in fetchlist()]
use(taglist) -T. (Gnaaah - code appears as non-monospaced for me - hope it looks ok)