3 ms·
Not in this case. 'row' is an item in the 'table.rows' iterable.
by progval 4y ago
Not in this case. 'row' is an item in the 'table.rows' iterable.
- BiteCode_dev 4y agoRight, I misread the code, my bad. For this case, one should use a nested comprehension: space = ((r, c, cell) for r, row in enumerate(table.rows) for c, cell in enumerate(row)) for (r, c, cell) in space: if search_query.matches(cell.value): logger.info("Found cell: (%s,%s)", r, c) return (r, c) Or see masklinn comment if you don't need the logger. In all languages, you separate code for preparing your input and code for processing it. In python, it's also a good idea to do that at the loop level: - prepare your stream of data so that it's normalized using itertools, generators, a function, a numpy array, etc - do a single loop pass for your processing This way you will you obtain a single iterable, and will be able to get the most out of the language tooling: - you can pass it to map/filter, itertools or another comprehension - you can convert to a list, a tuble, a Counter, etc. And slice it - you can use "else" in the loop, which is an underused construct, but very useful when you need break - you can use advanced unpacking - you can call iter() and/or next(). This is what masklinn does in his example. The point being, nested loops are rare enough, and we have tool to deal with them in an idiomatic way in Python, and not adding new syntax for a rare case that usually has a good solution makes sense to me.
- masklinn 4y agoAt that point you might as well make the entire thing into a gencomp inside a `next()` call, the explicit for loop is redundant.
- BiteCode_dev 4y agoYes, if you don't need the logger, it is indeed cleaner.
- masklinn 4y agoIn that case a flatmap does the job fine[0]. In fact you don't even need a flatmap: found = next(( (r, c) for r, row in enumerate(table.rows): for c, cell in enumerate(row): if search_query.matches(cell.value) ), None) Or just return at the location where you found a match. That way you can also provide a better error message in the failure case. [0] well map + itertools.concat, IIRC the stdlib doesn't have a flatmap per-se, though more_itertools probably does.