Skip to content Skip to sidebar Skip to footer

Explanation Of List Comprehensions

I'm attempting to learn about dynamic programming and recursion for Python 3.x. The code below is a simple example from my textbook: def recMC(coinValueList, change): minCoins

Solution 1:

for i in[cforcin coinValueList ifc<= change]:

is the same as:

coins =[]forcin coinValueList:ifc<= change:
        coins.append(c)for i in coins:#do your stuff

the list comprehension is much easier to code and better to read. list comprehension is the "python-way".

Solution 2:

let me explain

[cforcin coinValueList ifc<= change]

create new list with values from coinValueList which are less than change inside After this You iterate over new list.

N.B. If You insist on using comprehension there Consider generator statement It will work same way but without memory consumption.

so result for loop should be not

for i inc:forcin coinValueList:ifc<= change:

but

for i in coinValueList:ifi<=change:

Post a Comment for "Explanation Of List Comprehensions"