python - Expanding elements in a list -
I'm looking for a "good" way to process a list where some elements need to be expanded in more elements (Only once, no detail on the results)
The standard way to do it is:
i = 0 while i nl = [] at x in l: if needed _exending (x): Nl + = expand (x) else: nl.append (x)
But they both look too long or I can just pass 2 and later level the list I:
level (expand (x) if necessary x for xx xx A.) # or Diif try_expanding (x) ..
But it does not "feel right" either.
Is there any other obvious way to do this? If you do not need random access to the list that you are creating, then you can also write. A generator.
def iter_new_list (old_list): for x in old_list: if need_expanding (x): for extension y for (x): yield y and: produce x new_list = list (iter_new_list ( Old_list))
This is functionally equivalent to your second example, but it can be more readable in your real-world situation.
In addition, Python coding standards should use lowercase-L as a variable name, since it is not nearly different from the number.
Comments
Post a Comment