I want to create a list of lambda objects from a list of constants in Python; for instance:
listOfNumbers = [1,2,3,4,5]
square = lambda x: x * x
listOfLambdas = [lambda: square(i) for i ...
|
How to run an operation on a collection in Python and collect the results?
So if I have a list of 100 numbers, and I want to run a function like this ... |
I need to store a big list of integers in Bigtable(db). For efficiency I am storing them as diff between 2 consecutive items.
for eg:
original_list = [1005, 1004, 1003, 1004, 1006] ... |
I recently posted a question using a lambda function and in a reply someone had mentioned lambda is going out of favor, to use list comprehensions instead. I am relatively new ... |
A simplified version of my problem:
I have a list comprehension that i use to set bitflags on a two dimensional list so:
s = FLAG1 | FLAG2 | FLAG3
[[c.set_state(s) for c in ...
|
I happened to find myself having a basic filtering need: I have a list and I have to filter it by an attribute of the items.
My code looked like this:
my_list = ...
|
All:
def a(p):
return p+1
def gen(func, k=100):
l= []
for x in range(k):
...
|
|
def a(p): return p + 1
def b(p): return p + 2
def c(p): return p + 3
l= [a,b,c]
import itertools
ll = itertools.combinations(l, 2)
[x for x in ll]
[(<function a at 0x00CBD770>, <function b at ...
|
I have the following pyhton code that generates a list of anonymous function:
basis = [ (lambda x: n*x) for n in [0, 1, 2] ]
print basis[0](1)
I ... |
I have the the following list:
list = [{'nr' : 2, 'name': 'streamname'}, {'nr' : 3,'name': 'streamname'}, {'nr' : 1, 'name': 'streamname'}]
So how would I reorder it to become like this ... |
I have a list of lambda functions I want to evaluate in order. I'm not sure why, but only the last function gets evaluated. Example below:
>>> def f(x,z):
...
|
Why is the output of the following two list comprehensions different, even though f and the lambda function are the same ?
f = lambda x: x*x
[f(x) for x in range(10)]
and
[lambda x: ...
|
When comparing these, which do you think is more intuitive / easier to read?
>>> [ord(i) for i in 'some string']
[115, 111, 109, 101, 32, 115, 116, 114, 105, 110, 103]
>>> map(ord,'some ...
|
Is there any version of split that works on generic list types? For example, in Haskell
Prelude> import Data.List.Split
Prelude Data.List.Split> splitWhen (==2) [1, 2, 3]
[[1],[3]]
|
I have been trying to generate a list of lambda functions in python using list comprehension. but it didn't work,
for example
fl=[lambda x: x**i for i in range(5)]
i have check ... |
Imagine I have a list like this:
a = [1,2,3,4,5,6]
Using a lambda function, I would like to return two elements at a time, so the result would be:
res = [[1,2], [2,3], [3,4], ...
|
In python 2.6:
[x() for x in [lambda: m for m in [1,2,3]]]
results in:
[3, 3, 3]
I would expect the output to be [1, 2, 3]. I get the exact same problem even ... |
def myFunction(name):
index = 0
list = self.getList()
index = (x => x.name == name)
return index
i want to ... |
>>> params = [1, 2] >>> z = [None, None] >>> for i in range(len(z)): ... z[i] = lambda x, p=i: x**params[p] ... >>> x = range(5) >>> for i in x: ... print z[0](i), z[1](i) ... 0 0 1 1 2 4 3 9 4 16 >>> |
|