[Tutor] lambdas, generators, and the like
Dave Angel
davea at davea.name
Sun Jan 12 15:33:36 CET 2014
Keith Winston <keithwins at gmail.com> Wrote in message:
> I've got this line:
>
> for k in range(len(tcombo)):
> tcombo_ep.append(list(combinations(tcombo, k+1)))
>
> generating every possible length combination of tcombo. I then test
> them, and throw most of them away. I need to do this differently, it
> gets way too big (crashes my computer).
>
You should learn how to write and use a generator. Anytime you
find yourself creating a huge list, and only navigating it once,
consider writing a generator instead. A generator is any function
that has a yield in it. You can turn the loop above into a
one-level generator by
def gen (tcombo):
for k in range (len (tcombo))
yield list (combinations (tcombo, k+1))
And depending how you use the nested list, remove the call to list
() for some real serious space savings.
(untested)
> any help will be appreciated. I don't really understand lambda
> functions yet, but I can sort of imagine they might work here
> somehow... or not.
>
A lambda is seldom necessary or useful in simple programs.
>
--
DaveA nr
----Android NewsGroup Reader----
http://www.piaohong.tk/newsgroup
More information about the Tutor
mailing list