I hope the spelling isn't too far off, but what is list comprehension statement and why would one use it?
List comprehension is something Python borrowed from a language called Haskell. It returns a new list and is shorter and simply faster than the traditional loop/append alternative. I repeated the earlier code for the Python profile module here. Look at the two functions and you can get an idea how list comprehension works within the one-line [...].
# using the module profile to profile a function or a statement import profile #help('profile.run') def evenList1(): """returns a list of even numbers from 0 to 99998 using append in a loop the 50000 calls to append() consume much CPU time """ L1 =  for x in range(100000): if x % 2 == 0: L1.append(x) return L1 def evenList2(): """returns a list of even numbers from 0 to 99998 using list comprehension much faster in CPU time than the standard loop with append() """ L2 = [x for x in range(100000) if x % 2 == 0] return L2 # test first 30 elements print evenList1()[0:30] print evenList2()[0:30] print print "Profile of evenList1(), check the time consumed by the many append() calls:" print "(ignore time consumed by the profiler itself)\n" profile.run('evenList1()') print "Profile of evenList2():" print "(ignore time consumed by the profiler itself)\n" profile.run('evenList2()')
I put this also into the "Starting Python" thread.
I am developing a simple job card creator application with VB.net 2012, using Access 2010 as database.
The code I have generated to write data from a datagridview to the database table is successfully saving the data, but with an error "Parameter @name has no default value". The ...
write a C++ program which take inputs from the user/keyboard and calculate his ZAKAT.
get the input from user may include
-Amount invested in business
-Amount given to other business partners as loan
-Amount payable to others
I made a prime number finder, it finds the nearest prime that is above and below any integer (Does not work that well with giant numbers.)
Made this in cpp.sh
Why would I need the x=y+1-1;?
x=y; just doesnt seem to work
Why is this?
And I know my coding ...