Functional way to compare things inside a list

T

thorsopia

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?
 
I

Ivan@work

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]]
 
A

Alexander Blinne

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...
 
U

Ulrich Eckhardt

Am 21.09.2012 00:58, schrieb (e-mail address removed):
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
 
8

88888 Dihedral

Ulrich Eckhardtæ–¼ 2012å¹´9月21日星期五UTC+8下åˆ5時15分03秒寫é“:
Am 21.09.2012 00:58, schrieb (e-mail address removed):
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.
 
I

Ian Kelly

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.
 
8

88888 Dihedral

A

Ianæ–¼ 2012å¹´9月22日星期六UTC+8上åˆ4時50分49秒寫é“:
He wrote "lazy evaluation", not "lazy programming". Two entirely

different things.








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?
 
8

88888 Dihedral

A

Ianæ–¼ 2012å¹´9月22日星期六UTC+8上åˆ4時50分49秒寫é“:
He wrote "lazy evaluation", not "lazy programming". Two entirely

different things.








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?
 
S

Steven D'Aprano

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.
 
I

Ian Kelly

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.
 
8

88888 Dihedral

Ianæ–¼ 2012å¹´9月22日星期六UTC+8下åˆ2時23分43秒寫é“:
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?
 
8

88888 Dihedral

Ianæ–¼ 2012å¹´9月22日星期六UTC+8下åˆ2時23分43秒寫é“:
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?
 
J

Jamie Paul Griffin

[ Ian Kelly wrote on Sat 22.Sep'12 at 0:22:43 -0600 ]
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top