Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Functional way to compare things inside a list

Reply
Thread Tools

Functional way to compare things inside a list

 
 
thorsopia@lavabit.com
Guest
Posts: n/a
 
      09-20-2012
Hi,

list = [{'1': []}, {'2': []}, {'3': ['4', '5']}]

I want to check for a value (e.g. '4'), and get the key of the dictionary
that contains that value.
(Yep, this is bizarre.)

some_magic(list, '4')
=> '3'

What's the functional way to do it?
Is it possible to do it with a one-liner?






 
Reply With Quote
 
 
 
 
Ivan@work
Guest
Posts: n/a
 
      09-21-2012
On 21.09.2012 00:58, wrote:
> Hi,
>
> list = [{'1': []}, {'2': []}, {'3': ['4', '5']}]
>
> I want to check for a value (e.g. '4'), and get the key of the dictionary
> that contains that value.
> (Yep, this is bizarre.)
>
> some_magic(list, '4')
> => '3'
>
> What's the functional way to do it?
> Is it possible to do it with a one-liner?
>
>


Yes:

[key for d in list for key in d if '4' in d[key]]

 
Reply With Quote
 
 
 
 
Alexander Blinne
Guest
Posts: n/a
 
      09-21-2012
On 21.09.2012 00:58, wrote:
> Hi,
>
> list = [{'1': []}, {'2': []}, {'3': ['4', '5']}]
>
> I want to check for a value (e.g. '4'), and get the key of the dictionary
> that contains that value.
> (Yep, this is bizarre.)
>
> some_magic(list, '4')
> => '3'
>
> What's the functional way to do it?
> Is it possible to do it with a one-liner?


simple, but possibly slow solution:

import itertools

def some_magic(list, search):
return (key for key, val in itertools.chain(*(d.iteritems() for d in
list)) if search in val).next()

one-liner, yeah...
 
Reply With Quote
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      09-21-2012
Am 21.09.2012 00:58, schrieb :
> list = [{'1': []}, {'2': []}, {'3': ['4', '5']}]
>
> I want to check for a value (e.g. '4'), and get the key of the dictionary
> that contains that value.


Note:
1. list is a built-in type, who's name is rebound above
2. The list above contains dictionaries that all only contain a single key?
3. You have strings containing decimal representations of numbers?

> (Yep, this is bizarre.)


The data are really stored in a strange way and you might be able to
make things clearer by reorganizing them a bit.


> some_magic(list, '4')
> => '3'
>
> What's the functional way to do it?


Functional as in functional programming and an emphasis on lazy
evaluation? In that case I'd write a generator that emits the keys where
the values contain the requested string.


> Is it possible to do it with a one-liner?


Yep, filter(), lambda and the 'in' operator. Question remains if this is
readable. Note that you can use a local function, too, if you just want
to reduce the scope/visibility.


Good luck!


Uli

 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      09-21-2012
Ulrich Eckhardt於 2012年9月21日星期五UTC+8下午5時15分03秒 寫道:
> Am 21.09.2012 00:58, schrieb :
>
> > list = [{'1': []}, {'2': []}, {'3': ['4', '5']}]

>
> >

>
> > I want to check for a value (e.g. '4'), and get the key of the dictionary

>
> > that contains that value.

>
>
>
> Note:
>
> 1. list is a built-in type, who's name is rebound above
>
> 2. The list above contains dictionaries that all only contain a single key?
>
> 3. You have strings containing decimal representations of numbers?
>
>
>
> > (Yep, this is bizarre.)

>
>
>
> The data are really stored in a strange way and you might be able to
>
> make things clearer by reorganizing them a bit.
>
>
>
>
>
> > some_magic(list, '4')

>
> > => '3'

>
> >

>
> > What's the functional way to do it?

>
>
>
> Functional as in functional programming and an emphasis on lazy
>
> evaluation? In that case I'd write a generator that emits the keys where
>
> the values contain the requested string.
>
>
>
>
>
> > Is it possible to do it with a one-liner?

>
>
>
> Yep, filter(), lambda and the 'in' operator. Question remains if this is
>
> readable. Note that you can use a local function, too, if you just want
>
> to reduce the scope/visibility.
>
>
>
>
>
> Good luck!
>
>
>
>
>
> Uli


I don't think functional aspects are only marked as lazy
programming.

It just means when one is experimenting something
the efficient execution in speed is not on focus
yet.


 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      09-21-2012
On Fri, Sep 21, 2012 at 1:54 PM, 88888 Dihedral
<> wrote:
> I don't think functional aspects are only marked as lazy
> programming.


He wrote "lazy evaluation", not "lazy programming". Two entirely
different things.

> It just means when one is experimenting something
> the efficient execution in speed is not on focus
> yet.


No, what you're describing is a "prototype". It has nothing to do
with functional programming at all.
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      09-21-2012
A

Ian於 2012年9月22日星期*UTC+8上午4時50分49秒 寫道:
> On Fri, Sep 21, 2012 at 1:54 PM, 88888 Dihedral
>
> <> wrote:
>
> > I don't think functional aspects are only marked as lazy

>
> > programming.

>
>
>
> He wrote "lazy evaluation", not "lazy programming". Two entirely
>
> different things.
>
>
>
> > It just means when one is experimenting something

>
> > the efficient execution in speed is not on focus

>
> > yet.

>
>
>
> No, what you're describing is a "prototype". It has nothing to do
>
> with functional programming at all.


A function with varaible arguments can be stored as a variable
to functions called decorators in python to return enhanced functions.

A function mapps a decorator to another decorator can be called
a decorator map or a decorator maker in python.

The closure level is guaranteed for decorators to be mapped by
multi-levels of decorator mappers trivially in python.

What do you want else for functional prgramming in python?
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      09-21-2012
A

Ian於 2012年9月22日星期*UTC+8上午4時50分49秒 寫道:
> On Fri, Sep 21, 2012 at 1:54 PM, 88888 Dihedral
>
> <> wrote:
>
> > I don't think functional aspects are only marked as lazy

>
> > programming.

>
>
>
> He wrote "lazy evaluation", not "lazy programming". Two entirely
>
> different things.
>
>
>
> > It just means when one is experimenting something

>
> > the efficient execution in speed is not on focus

>
> > yet.

>
>
>
> No, what you're describing is a "prototype". It has nothing to do
>
> with functional programming at all.


A function with varaible arguments can be stored as a variable
to functions called decorators in python to return enhanced functions.

A function mapps a decorator to another decorator can be called
a decorator map or a decorator maker in python.

The closure level is guaranteed for decorators to be mapped by
multi-levels of decorator mappers trivially in python.

What do you want else for functional prgramming in python?
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-22-2012
On Fri, 21 Sep 2012 14:49:55 -0600, Ian Kelly wrote:

> On Fri, Sep 21, 2012 at 1:54 PM, 88888 Dihedral
> <> wrote:
>> I don't think functional aspects are only marked as lazy programming.

>
> He wrote "lazy evaluation", not "lazy programming". Two entirely
> different things.



For the record, the consensus here is that 88888 Dihedral is probably a
bot. It appears to be a pretty good bot, I haven't spotted it making any
egregious or obvious grammatical mistakes, but the semantics of its posts
don't seem quite human.

88888 Dihedral, if you're not a bot, you can go a long way towards
proving that by telling us what colour a purple elephant is.


--
Steven
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      09-22-2012
On Fri, Sep 21, 2012 at 7:25 PM, Steven D'Aprano
<steve+> wrote:
> On Fri, 21 Sep 2012 14:49:55 -0600, Ian Kelly wrote:
>
>> On Fri, Sep 21, 2012 at 1:54 PM, 88888 Dihedral
>> <> wrote:
>>> I don't think functional aspects are only marked as lazy programming.

>>
>> He wrote "lazy evaluation", not "lazy programming". Two entirely
>> different things.

>
>
> For the record, the consensus here is that 88888 Dihedral is probably a
> bot. It appears to be a pretty good bot, I haven't spotted it making any
> egregious or obvious grammatical mistakes, but the semantics of its posts
> don't seem quite human.


I'm aware of that, although sometimes the posts seem coherent enough
that I think maybe it's not. Especially the ones where it posts
almost-working code snippets, complete with obvious typos.

Then it posts a complete non sequitur like the reply to my reply in
this thread, and the illusion is shattered.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Functional way to compare things inside a list Chris Rebert Python 0 09-21-2012 08:33 AM
Re: Functional way to compare things inside a list Chris Angelico Python 0 09-21-2012 08:31 AM
Re: Functional way to compare things inside a list Chris Rebert Python 0 09-21-2012 08:28 AM
Re: Functional way to compare things inside a list Chris Angelico Python 0 09-21-2012 08:23 AM
Making things more functional in Python gf gf Python 6 03-05-2005 07:28 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57