Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #29604 > unrolled thread

Functional way to compare things inside a list

Started bythorsopia@lavabit.com
First post2012-09-20 18:58 -0400
Last post2012-09-22 21:09 -0700
Articles 16 — 10 participants

Back to article view | Back to comp.lang.python


Contents

  Functional way to compare things inside a list thorsopia@lavabit.com - 2012-09-20 18:58 -0400
    Re: Functional way to compare things inside a list "Ivan@work" <ivan.cvetkovic@pakel.hr> - 2012-09-21 10:24 +0200
    Re: Functional way to compare things inside a list Alexander Blinne <news@blinne.net> - 2012-09-21 10:31 +0200
    Re: Functional way to compare things inside a list Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-09-21 10:52 +0200
      Re: Functional way to compare things inside a list 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-21 12:54 -0700
        Re: Functional way to compare things inside a list Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-21 14:49 -0600
          Re: Functional way to compare things inside a list 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-21 14:45 -0700
          Re: Functional way to compare things inside a list 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-21 14:45 -0700
          Re: Functional way to compare things inside a list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-22 01:25 +0000
            Re: Functional way to compare things inside a list Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-22 00:22 -0600
              Re: Functional way to compare things inside a list 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-21 23:50 -0700
              Re: Functional way to compare things inside a list 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-21 23:50 -0700
            Re: Functional way to compare things inside a list Jamie Paul Griffin <jamie@kode5.net> - 2012-09-22 08:08 +0100
            Re: Functional way to compare things inside a list Andrew Berg <bahamutzero8825@gmail.com> - 2012-09-22 02:19 -0500
              Re: Functional way to compare things inside a list Ramchandra Apte <maniandram01@gmail.com> - 2012-09-22 21:09 -0700
              Re: Functional way to compare things inside a list Ramchandra Apte <maniandram01@gmail.com> - 2012-09-22 21:09 -0700

#29604 — Functional way to compare things inside a list

Fromthorsopia@lavabit.com
Date2012-09-20 18:58 -0400
SubjectFunctional way to compare things inside a list
Message-ID<mailman.990.1348213606.27098.python-list@python.org>
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?





[toc] | [next] | [standalone]


#29608

From"Ivan@work" <ivan.cvetkovic@pakel.hr>
Date2012-09-21 10:24 +0200
Message-ID<k3h888$rl1$1@ls237.t-com.hr>
In reply to#29604
On 21.09.2012 00:58, thorsopia@lavabit.com 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]]

[toc] | [prev] | [next] | [standalone]


#29610

FromAlexander Blinne <news@blinne.net>
Date2012-09-21 10:31 +0200
Message-ID<505c25ed$0$6581$9b4e6d93@newsspool3.arcor-online.net>
In reply to#29604
On 21.09.2012 00:58, thorsopia@lavabit.com 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...

[toc] | [prev] | [next] | [standalone]


#29616

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2012-09-21 10:52 +0200
Message-ID<ffmti9-elv.ln1@satorlaser.homedns.org>
In reply to#29604
Am 21.09.2012 00:58, schrieb thorsopia@lavabit.com:
> 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

[toc] | [prev] | [next] | [standalone]


#29665

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-09-21 12:54 -0700
Message-ID<48191878-3a0e-4733-a426-55b6981c471d@googlegroups.com>
In reply to#29616
Ulrich Eckhardt於 2012年9月21日星期五UTC+8下午5時15分03秒寫道:
> Am 21.09.2012 00:58, schrieb thorsopia@lavabit.com:
> 
> > 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. 

 

[toc] | [prev] | [next] | [standalone]


#29681

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-09-21 14:49 -0600
Message-ID<mailman.1040.1348260628.27098.python-list@python.org>
In reply to#29665
On Fri, Sep 21, 2012 at 1:54 PM, 88888 Dihedral
<dihedral88888@googlemail.com> 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.

[toc] | [prev] | [next] | [standalone]


#29688

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-09-21 14:45 -0700
Message-ID<709c4ffe-27ad-47d8-8536-1ab394092da5@googlegroups.com>
In reply to#29681
A

Ian於 2012年9月22日星期六UTC+8上午4時50分49秒寫道:
> On Fri, Sep 21, 2012 at 1:54 PM, 88888 Dihedral
> 
> <dihedral88888@googlemail.com> 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?

[toc] | [prev] | [next] | [standalone]


#29689

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-09-21 14:45 -0700
Message-ID<mailman.1044.1348264463.27098.python-list@python.org>
In reply to#29681
A

Ian於 2012年9月22日星期六UTC+8上午4時50分49秒寫道:
> On Fri, Sep 21, 2012 at 1:54 PM, 88888 Dihedral
> 
> <dihedral88888@googlemail.com> 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?

[toc] | [prev] | [next] | [standalone]


#29703

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-09-22 01:25 +0000
Message-ID<505d138e$0$29981$c3e8da3$5496439d@news.astraweb.com>
In reply to#29681
On Fri, 21 Sep 2012 14:49:55 -0600, Ian Kelly wrote:

> On Fri, Sep 21, 2012 at 1:54 PM, 88888 Dihedral
> <dihedral88888@googlemail.com> 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

[toc] | [prev] | [next] | [standalone]


#29721

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-09-22 00:22 -0600
Message-ID<mailman.1063.1348294996.27098.python-list@python.org>
In reply to#29703
On Fri, Sep 21, 2012 at 7:25 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Fri, 21 Sep 2012 14:49:55 -0600, Ian Kelly wrote:
>
>> On Fri, Sep 21, 2012 at 1:54 PM, 88888 Dihedral
>> <dihedral88888@googlemail.com> 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.

[toc] | [prev] | [next] | [standalone]


#29722

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-09-21 23:50 -0700
Message-ID<d099bec5-0d31-47ca-9354-993716b0ac6f@googlegroups.com>
In reply to#29721
Ian於 2012年9月22日星期六UTC+8下午2時23分43秒寫道:
> On Fri, Sep 21, 2012 at 7:25 PM, Steven D'Aprano
> 
> <steve+comp.lang.python@pearwood.info> wrote:
> 
> > On Fri, 21 Sep 2012 14:49:55 -0600, Ian Kelly wrote:
> 
> >
> 
> >> On Fri, Sep 21, 2012 at 1:54 PM, 88888 Dihedral
> 
> >> <dihedral88888@googlemail.com> 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.

Do you want to use the lisp way for implementing functional programming?

 

[toc] | [prev] | [next] | [standalone]


#29723

From88888 Dihedral <dihedral88888@googlemail.com>
Date2012-09-21 23:50 -0700
Message-ID<mailman.1064.1348296636.27098.python-list@python.org>
In reply to#29721
Ian於 2012年9月22日星期六UTC+8下午2時23分43秒寫道:
> On Fri, Sep 21, 2012 at 7:25 PM, Steven D'Aprano
> 
> <steve+comp.lang.python@pearwood.info> wrote:
> 
> > On Fri, 21 Sep 2012 14:49:55 -0600, Ian Kelly wrote:
> 
> >
> 
> >> On Fri, Sep 21, 2012 at 1:54 PM, 88888 Dihedral
> 
> >> <dihedral88888@googlemail.com> 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.

Do you want to use the lisp way for implementing functional programming?

 

[toc] | [prev] | [next] | [standalone]


#29724

FromJamie Paul Griffin <jamie@kode5.net>
Date2012-09-22 08:08 +0100
Message-ID<mailman.1065.1348297709.27098.python-list@python.org>
In reply to#29703
[ Ian Kelly wrote on Sat 22.Sep'12 at  0:22:43 -0600 ]

> On Fri, Sep 21, 2012 at 7:25 PM, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> wrote:
> > On Fri, 21 Sep 2012 14:49:55 -0600, Ian Kelly wrote:
> >
> >> On Fri, Sep 21, 2012 at 1:54 PM, 88888 Dihedral
> >> <dihedral88888@googlemail.com> 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.

I find this intriguing, I had no idea bots existed to post to mailing
lists in this way. What's the point of them?

[toc] | [prev] | [next] | [standalone]


#29726

FromAndrew Berg <bahamutzero8825@gmail.com>
Date2012-09-22 02:19 -0500
Message-ID<mailman.1066.1348298396.27098.python-list@python.org>
In reply to#29703
On 2012.09.22 02:08, Jamie Paul Griffin wrote:
> I find this intriguing, I had no idea bots existed to post to mailing
> lists in this way. What's the point of them?

To amuse their owners is my guess.
-- 
CPython 3.3.0rc2 | Windows NT 6.1.7601.17835

[toc] | [prev] | [next] | [standalone]


#29784

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-09-22 21:09 -0700
Message-ID<d262d39c-7716-4ae1-a802-68047af2e8f6@googlegroups.com>
In reply to#29726
On Saturday, 22 September 2012 12:50:08 UTC+5:30, Andrew Berg  wrote:
> On 2012.09.22 02:08, Jamie Paul Griffin wrote:
> 
> > I find this intriguing, I had no idea bots existed to post to mailing
> 
> > lists in this way. What's the point of them?
> 
> 
> 
> To amuse their owners is my guess.
> 
> -- 
> 
> CPython 3.3.0rc2 | Windows NT 6.1.7601.17835

The bot could be used for automatic replies for duplicate posts (common questions are quite often repeated)

[toc] | [prev] | [next] | [standalone]


#29785

FromRamchandra Apte <maniandram01@gmail.com>
Date2012-09-22 21:09 -0700
Message-ID<mailman.1099.1348373407.27098.python-list@python.org>
In reply to#29726
On Saturday, 22 September 2012 12:50:08 UTC+5:30, Andrew Berg  wrote:
> On 2012.09.22 02:08, Jamie Paul Griffin wrote:
> 
> > I find this intriguing, I had no idea bots existed to post to mailing
> 
> > lists in this way. What's the point of them?
> 
> 
> 
> To amuse their owners is my guess.
> 
> -- 
> 
> CPython 3.3.0rc2 | Windows NT 6.1.7601.17835

The bot could be used for automatic replies for duplicate posts (common questions are quite often repeated)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web