Tagged Questions
-2
votes
0answers
21 views
Python lambda vs Functional programming's lambda [on hold]
Could someone explain the basic difference between Python's lambda and functional programming's lambda?
2
votes
2answers
50 views
Explaination of this lambda function please?
I sorted a list of tuples using sorted(list_of_tuples, key = lambda tup: tup[1]) from code I found on the internet. I don't understand how the lambda function works in this command. Is the lambda ...
3
votes
2answers
38 views
passing a function as an argument in python
Suppose I want to calculate the following f(f(...f(x)..) .
Basically many times function of itself.
Currently I am doing the following to achieve this result (and to return all the intermediate ...
1
vote
3answers
92 views
Filter latest items in a list
I have this large data structure in Python - basically a list of dictionaries. Each of these dictionaries may contain a few recurring properties and a timestamp. I am trying to see if the values of ...
6
votes
2answers
96 views
Weird lambda behaviour in loops in python 2.7, anybody has a clue? [duplicate]
I stumbled upon a behaviour in python that I have an hard time understanding. This is the proof-of-concept code:
from functools import partial
if __name__ == '__main__':
sequence = ['foo', ...
6
votes
5answers
4k views
python max function using 'key' and lambda expression
I come from OOP background and trying to learn python.
I am using the max function which uses a lambda expression to return the instance of type Player having maximum totalScore among the list ...
4
votes
2answers
88 views
Address of lambda function in python
I have this:
>>> a = lambda : lambda x : x * x
This gives me a constant address every time:
>>> a
<function <lambda> at 0x7f22769e76e0>
>>> a
<function ...
0
votes
1answer
75 views
Get coordinates from a lambda function point
I am fitting data to certain curves.
I have a model lambda function and I would like to obtain the exact coordinates (x,y) of the maximum point of it.
I am able to know the maximum value, however, I ...
2
votes
1answer
58 views
what is the name of that in python
I know what are closures and what are lambda functions but I want to know what is the name of that :
>>> def foo(a, b):
>>> return a + b
>>>
>>> bar = foo
...
4
votes
1answer
830 views
lambda *args, **kwargs: None
consider:
blank_fn = lambda *args, **kwargs: None
def callback(x, y, z=''):
print x, y, z
def perform_task(callback=blank_fn):
print 'doing stuff'
callback('x', 'y', z='z' )
The ...
-8
votes
1answer
67 views
Performance gain using anonymous functions? [closed]
I'm processing 300k huge files and see a major performance issue.
The problem is with the number of checks that are being performed.
We have lots of functions and are planning to turn them into ...
1
vote
0answers
55 views
strange behavior of a function in for loop [duplicate]
I am trying to write a python function(al), which would generate a series, for example
f(x) = 1 + x + x^2 + ... + x^n
given number of terms in the series. (Remember, the above is only an example ...
1
vote
1answer
79 views
How to iteratively create new variables to store the ith value of a list
I want to write a function that takes L, a list of tuples (an example might be L = [(1,0,0), (1,1,0)]), and loops through L to create a new variable t{0}.format(i) for the ith entry in L. So ideally ...
0
votes
2answers
328 views
Python Lambda Map
I’m new to python and am trying to puzzle out the use of the lambda function. I have two lists of network user names in a text file which I need to match. The code I have so far works okay(it matches ...
4
votes
0answers
36 views
python lambda function mutability [duplicate]
I've tried the following code in the python interpreter:
>>> def say(n):
... print n
...
>>> say(12)
12
>>> test = []
>>> for each in range(30):
... ...
1
vote
1answer
94 views
Python and lambda functions [duplicate]
Might seems like a nooby question (myabe it is), but why does python behave like that:
>>>a = []
>>>for i in xrange(5):
... a.append(lambda: i + 1)
>>>a[0]()
5
...
7
votes
3answers
750 views
Python lambda function to calculate factorial of a number
I have just started learning python. I came across lambda functions. On one of the problems, the author asked to write a one liner lambda function for factorial of a number.
This is the solution ...
0
votes
5answers
133 views
using lambda to verify all elements in list
i have a list of foods called "my_foods".
i would like to use a lambda function to verify that all elements from the list "good_foods" appear in the list "my_foods", and also verify that none of the ...
0
votes
1answer
133 views
Passing parameters to a slot function using lambda
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__();
...
-3
votes
1answer
46 views
How to get arguments names in a list and create in lambda python?
argsneeded = ["dividend","divisor"]
lambda ?????: a/b
What should I put in the ????s?
I'm creating a record and replay tool 4 python, argsneeded is an arbitrary list gained by sys.traceit to get ...
1
vote
2answers
167 views
Using Python's filter function
Here are two seemingly equivalent versions of a function for filtering out the primes from a list of numbers.
Version 1
def prime (mylist):
for i in range(2, 8):
return ...
4
votes
1answer
859 views
How to remove negative values from a list using lambda functions by python
I have implemented a lambda function to sort a list. now i want to remove all the negative objects from the list by using lambda functions .
dto_list.sort(key=lambda x: x.count, reverse=True)
any ...
3
votes
2answers
152 views
Filter values in a Python object array generically with a lambda function as a string
I'm working on a generic framework, and at some point I'm trying to filter variables in a generic manner. Take for example the following class:
class X:
def __init__(self, val):
self.val ...
7
votes
2answers
1k views
Python lambda's binding to local values
The following code spits out 1 twice, I expect to see 0 and then 1
def pv(v) :
print v
def test() :
value = []
value.append(0)
value.append(1)
x=[]
for v in value :
x.append(lambda ...
0
votes
4answers
539 views
Python: to wrap functions via lambda
I have some code like
class EventHandler:
def handle(self, event):
pass
def wrap_handler(handler):
def log_event(proc, e):
print e
proc(e)
handler.handle = lambda ...
1
vote
4answers
399 views
Inline error/exception handling in python
So, I have 3 lists as follows:
list_1 = ['a','b','c']
list_2 = ['b','c']
list_3 = [5,4]
I am looking to generate a fourth list of values from list_2 & list_3 that will map to list_1. list_2 ...
0
votes
4answers
593 views
constant lambda function in python
How shall I define a constant lambda function in python?
I need it to evaluation expressions like
lam (array[1,2,3,4,5])
For now I used
lam = lambda t: 1 + t*0
It works but is it too ...
4
votes
7answers
275 views
Example where lambdas are very useful in Python
I met lambda expressions in Python. I have seen already many easy examples (including examples on SE) where lambda expressions produce functions like adders of whatever but I can't see the real ...
0
votes
6answers
4k views
map lambda x,y with a constant x
What would be an elegant way to map a 2 parameter Python lambda function to a list of values where the first parameter is constant and the second is taken from a list.
Example:
lambda x,y: x+y
x='a'
...
21
votes
14answers
9k views
How to make an anonymous function in Python without Christening it?
Is it possible to put a function in a data structure, without first giving it a name with def?
# This is the behaviour I want. Prints "hi".
def myprint(msg):
print msg
f_list = [ myprint ]
...
1
vote
1answer
70 views
Is there an equilivent to Linq.Expressions.Expression in python?
I would like to be able to get the expression out of a lambda function much like C# does and parse it into something else?
Example in C#:
void Foo<T>(Expression<Func<T, bool>> ...
1
vote
3answers
96 views
Why are my lambdas disappearing?
I have some Python code that's dependent upon passing around some lambdas, and they get copied to a few different places, but I'm finding that when I pop them out of a list, they just magically ...
1
vote
3answers
889 views
django lambda and django-activity-stream
I am not familiar with the lambda function itself, and don't really know how to debug this issue.
Django-1.1.2
I am using, django-activity-stream in order to render activity streams for my users.
In ...
4
votes
2answers
188 views
What is the Pythonic way of reordering a list consisting of dicts?
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 in an ...
2
votes
1answer
218 views
Few questions on generator expressions and speed efficient alternatives
Consider the following code, integral to my questions below:
import functools
N = 3
class Struct:
"""Create an instance with argument=value slots.
This is for making a lightweight object ...
0
votes
3answers
2k views
How to convert comma-separated key value pairs into a dictionary using lambda functions
I'm having a little problem figuring out lamba functions. Could someone show me how to split the following string into a dictionary using lambda functions?
...