Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Learning Pyhton - Functional Programming - How intersect/differencetwo dict with dict/values? fast!

Reply
Thread Tools

Learning Pyhton - Functional Programming - How intersect/differencetwo dict with dict/values? fast!

 
 
macm
Guest
Posts: n/a
 
      11-09-2010
Hi Folks,

dict1 = {'ab':[[1,2,3,'d3','d4',5],12],'ac':[[1,3,'78a','79b'],
54],'ad': [[56,57,58,59],34], 'ax': [[56,57,58,59],34]}
dict2 = {'ab':[[22,2,'a0','42s','c4','d3'],12],'ab':[[2,4,50,42,'c4'],
12],'ac':[[1,3,'79b',45,65,'er4'],54],'ae': [[56,57,58,59],34],'ax':
[[9],34]}
dict3 = {'ac':[[1,3,67,'gf'],12],'at':[[2,4,50,42,'c4'],12],'as':
[[1,3,'79b',45,65,'er4'],54],'ae': [[56,57,58,59],34]}
intersect = filter(dict1.has_key, dict2.keys())
intersect
result:
['ax', 'ac', 'ab']

expect result dict1 intersection with dict2
{'ac':[1,3,'79b'], 'ab':[2,'d3']} # look last key/value 'ax' (dict1,
dict2) even intersec a key but not values from list so not valid

and difference from dict3
dict3 = {'ac':[[1,3,67,'gf'],12],'at':[[2,4,50,42,'c4'],12],'as':
[[1,3,'79b',45,65,'er4'],54],'ae': [[56,57,58,59],34]}

result from (intersect - dict3)
{'ac':['79b'], 'ab':[2,'d3']}

Thanks in advance!

Before someone blame me.

Yes I am trying learn python Functional Programming! ; )

Best Regards

macm
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      11-09-2010
macm wrote:

> Hi Folks,
>
> dict1 = {'ab':[[1,2,3,'d3','d4',5],12],'ac':[[1,3,'78a','79b'],
> 54],'ad': [[56,57,58,59],34], 'ax': [[56,57,58,59],34]}
> dict2 = {'ab':[[22,2,'a0','42s','c4','d3'],12],'ab':[[2,4,50,42,'c4'],
> 12],'ac':[[1,3,'79b',45,65,'er4'],54],'ae': [[56,57,58,59],34],'ax':
> [[9],34]}
> dict3 = {'ac':[[1,3,67,'gf'],12],'at':[[2,4,50,42,'c4'],12],'as':
> [[1,3,'79b',45,65,'er4'],54],'ae': [[56,57,58,59],34]}
> intersect = filter(dict1.has_key, dict2.keys())
> intersect
> result:
> ['ax', 'ac', 'ab']
>
> expect result dict1 intersection with dict2
> {'ac':[1,3,'79b'], 'ab':[2,'d3']} # look last key/value 'ax' (dict1,
> dict2) even intersec a key but not values from list so not valid
>
> and difference from dict3
> dict3 = {'ac':[[1,3,67,'gf'],12],'at':[[2,4,50,42,'c4'],12],'as':
> [[1,3,'79b',45,65,'er4'],54],'ae': [[56,57,58,59],34]}
>
> result from (intersect - dict3)
> {'ac':['79b'], 'ab':[2,'d3']}
>
> Thanks in advance!
>
> Before someone blame me.
>
> Yes I am trying learn python Functional Programming! ; )


>>> dict((k, v) for k, v in ((k, [a for a in v if a in w]) for k, v, w in

((k, v[0], set(dict2[k][0])) for k, v in dict1.iteritems() if k in dict2))
if v)
{'ac': [1, 3, '79b'], 'ab': [2]}

Replacing the genexps with map/filter/lambda is left as an exercise.

> Before someone blame me.


I'm trying to set new standards in readability
 
Reply With Quote
 
 
 
 
John Nagle
Guest
Posts: n/a
 
      11-09-2010
On 11/9/2010 9:32 AM, macm wrote:
> Hi Folks,
>
> dict1 = {'ab':[[1,2,3,'d3','d4',5],12],'ac':[[1,3,'78a','79b'],
> 54],'ad': [[56,57,58,59],34], 'ax': [[56,57,58,59],34]}
> dict2 = {'ab':[[22,2,'a0','42s','c4','d3'],12],'ab':[[2,4,50,42,'c4'],
> 12],'ac':[[1,3,'79b',45,65,'er4'],54],'ae': [[56,57,58,59],34],'ax':
> [[9],34]}
> dict3 = {'ac':[[1,3,67,'gf'],12],'at':[[2,4,50,42,'c4'],12],'as':
> [[1,3,'79b',45,65,'er4'],54],'ae': [[56,57,58,59],34]}
> intersect = filter(dict1.has_key, dict2.keys())
> intersect
> result:
> ['ax', 'ac', 'ab']


When you've spent a year maintaining and fixing code written
by others, come back and we'll talk.

John Nagle
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      11-09-2010

> On 11/9/2010 9:32 AM, macm wrote:


>> dict1 = {'ab':[[1,2,3,'d3','d4',5],12],'ac':[[1,3,'78a','79b'],
>> 54],'ad': [[56,57,58,59],34], 'ax': [[56,57,58,59],34]}


To echo John Nagle's point, if you want non-masochist volunteers to read
your code, write something readable like:

dict1 = {'ab': [[1,2,3,'d3','d4',5], 12],
'ac': [[1,3,'78a','79b'], 54],
'ad': [[56,57,58,59], 34],
'ax': [[56,57,58,59], 34]}

--
Terry Jan Reedy

 
Reply With Quote
 
D'Arcy J.M. Cain
Guest
Posts: n/a
 
      11-09-2010
On Tue, 09 Nov 2010 15:55:16 -0500
Terry Reedy <(E-Mail Removed)> wrote:
> To echo John Nagle's point, if you want non-masochist volunteers to read
> your code, write something readable like:
>
> dict1 = {'ab': [[1,2,3,'d3','d4',5], 12],
> 'ac': [[1,3,'78a','79b'], 54],
> 'ad': [[56,57,58,59], 34],
> 'ax': [[56,57,58,59], 34]}


I have been learning to like this form:

dict1 = dict(
ab = [[1,2,3,'d3','d4',5], 12],
ac = [[1,3,'78a','79b'], 54],
ad = [[56,57,58,59], 34],
ax = [[56,57,58,59], 34],
)

Of course, it only works when all the keys are strings.

--
D'Arcy J.M. Cain <(E-Mail Removed)> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
 
Reply With Quote
 
macm
Guest
Posts: n/a
 
      11-09-2010
Sorry Mr. Nagle and Folks

I had a bad day today.

I need forward but with fever, a grasp and headache is hard.

You are absolute right, I was rude and I ask your pardon.

About my code, sorry I thought was the best way to copy and paste in
python console.

Best Regards

macm

On Nov 9, 7:03*pm, "D'Arcy J.M. Cain" <(E-Mail Removed)> wrote:
> On Tue, 09 Nov 2010 15:55:16 -0500
>
> Terry Reedy <(E-Mail Removed)> wrote:
> > To echo John Nagle's point, if you want non-masochist volunteers to read
> > your code, write something readable like:

>
> > dict1 = {'ab': [[1,2,3,'d3','d4',5], 12],
> > * * * * * 'ac': [[1,3,'78a','79b'], 54],
> > * * * * * 'ad': [[56,57,58,59], 34],
> > * * * * * 'ax': [[56,57,58,59], 34]}

>
> I have been learning to like this form:
>
> dict1 = dict(
> * ab = [[1,2,3,'d3','d4',5], 12],
> * ac = [[1,3,'78a','79b'], 54],
> * ad = [[56,57,58,59], 34],
> * ax = [[56,57,58,59], 34],
> )
>
> Of course, it only works when all the keys are strings.
>
> --
> D'Arcy J.M. Cain <(E-Mail Removed)> * * * * | *Democracy is three wolveshttp://www.druid.net/darcy/* * * * * * * *| *and a sheep voting on
> +1 416 425 1212 * * (DoD#0082) * *(eNTP) * | *what's for dinner.


 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      11-10-2010
In message <(E-Mail Removed)>, Terry Reedy
wrote:

> To echo John Nagle's point, if you want non-masochist volunteers to read
> your code, write something readable like:
>
> dict1 = {'ab': [[1,2,3,'d3','d4',5], 12],
> 'ac': [[1,3,'78a','79b'], 54],
> 'ad': [[56,57,58,59], 34],
> 'ax': [[56,57,58,59], 34]}


How come Python itself doesn’t display things that way?
 
Reply With Quote
 
Paul Rudin
Guest
Posts: n/a
 
      11-10-2010
Lawrence D'Oliveiro <(E-Mail Removed)_zealand> writes:

> In message <(E-Mail Removed)>, Terry Reedy
> wrote:
>
>> To echo John Nagle's point, if you want non-masochist volunteers to read
>> your code, write something readable like:
>>
>> dict1 = {'ab': [[1,2,3,'d3','d4',5], 12],
>> 'ac': [[1,3,'78a','79b'], 54],
>> 'ad': [[56,57,58,59], 34],
>> 'ax': [[56,57,58,59], 34]}

>
> How come Python itself doesn’t display things that way?


try: pprint.pprint(dict1)
 
Reply With Quote
 
macm
Guest
Posts: n/a
 
      11-10-2010
Hi Folks

I am studing yet (with fever, grasp and headache).

I know I can do better, but first I should learn more about
"dictionary comprehension syntax" in python 2.65


>>> dict1 = {'ab':[[1,2,3,'d3','d4',5],12],'ac':[[1,3,'78a','79b'],54],'ad': [[56,57,58,59],34], 'ax': [[56,57,58,59],34]}
>>> dict2 = {'ab':[[22,2,'a0','42s','c4','d3'],12],'ab':[[2,4,50,42,'c4'],12],'ac':[[1,3,'79b',45,65,'er4'],54],'ae': [[56,57,58,59],34],'ax':[[9],34]}
>>>
>>> # Arnaud Delobelle

.... def intersectList(iterables):
.... nexts = [iter(iterable).next for iterable in iterables]
.... v = [next() for next in nexts]
.... while True:
.... for i in xrange(1, len(v)):
.... while v[0] > v[i]:
.... v[i] = nexts[i]()
.... if v[0] < v[i]: break
.... else:
.... yield v[0]
.... v[0] = nexts[0]()
....
>>> def intersect(s1, s2):

.... d = {}
.... e = {}
.... r1 = filter(s1.has_key, s2.keys())
.... for x in r1:
.... d[x] = list(intersectList([s1[x][0],s2[x][0]]))
.... if len(d[x]) > 0:
.... e[x] = d[x]
.... return e
....
>>> intersect(dict1,dict2)

{'ac': [1, 3, '79b'], 'ab': [2]}
>>>



Best Regards

Mario



On 10 nov, 07:51, Paul Rudin <(E-Mail Removed)> wrote:
> Lawrence D'Oliveiro <(E-Mail Removed)_zealand> writes:
> > In message <(E-Mail Removed)>, Terry Reedy
> > wrote:

>
> >> To echo John Nagle's point, if you want non-masochist volunteers to read
> >> your code, write something readable like:

>
> >> dict1 = {'ab': [[1,2,3,'d3','d4',5], 12],
> >> * * * * * 'ac': [[1,3,'78a','79b'], 54],
> >> * * * * * 'ad': [[56,57,58,59], 34],
> >> * * * * * 'ax': [[56,57,58,59], 34]}

>
> > How come Python itself doesnt display things that way?

>
> try: pprint.pprint(dict1)


 
Reply With Quote
 
macm
Guest
Posts: n/a
 
      11-10-2010
.... and this works!

>>> def intersect(s1, s2):

.... d = {}
.... e = {}
.... r1 = filter(s1.has_key, s2.keys())
.... for x in r1:
.... d[x]= filter(lambda z:z in s1[x][0],s2[x][0])
.... if len(d[x]) > 0:
.... e[x] = d[x]
.... return e
....
>>> intersect(dict1,dict2)

{'ac': [1, 3, '79b'], 'ab': [2]}
>>>



but how I pass d[x] and make the filter?

>>> result = [filter(lambda z:z in dict1[x][0],dict2[x][0]) for x in filter(dict1.has_key, dict2.keys())]
>>> result

[[], [1, 3, '79b'], [2]]
>>>


but should be {'ac': [1, 3, '79b'], 'ab': [2]}

Best Regards

Mario



On 10 nov, 18:14, macm <(E-Mail Removed)> wrote:
> Hi Folks
>
> I am studing yet (with fever, grasp and headache).
>
> I know I can do better, but first I should learn more about
> "dictionary comprehension syntax" in python 2.65
>
> >>> dict1 = {'ab':[[1,2,3,'d3','d4',5],12],'ac':[[1,3,'78a','79b'],54],'ad': [[56,57,58,59],34], 'ax': [[56,57,58,59],34]}
> >>> dict2 = {'ab':[[22,2,'a0','42s','c4','d3'],12],'ab':[[2,4,50,42,'c4'],12],'ac':[[1,3,'79b',45,65,'er4'],54],'ae': [[56,57,58,59],34],'ax':[[9],34]}

>
> >>> # Arnaud Delobelle

>
> ... def intersectList(iterables):
> ... * * nexts = [iter(iterable).next for iterable in iterables]
> ... * * v = [next() for next in nexts]
> ... * * while True:
> ... * * * * for i in xrange(1, len(v)):
> ... * * * * * * while v[0] > v[i]:
> ... * * * * * * * * v[i] = nexts[i]()
> ... * * * * * * if v[0] < v[i]: break
> ... * * * * else:
> ... * * * * * * yield v[0]
> ... * * * * v[0] = nexts[0]()
> ...>>> defintersect(s1, s2):
>
> ... * * d = {}
> ... * * e = {}
> ... * * r1 = filter(s1.has_key, s2.keys())
> ... * * for x in r1:
> ... * * * * d[x] = list(intersectList([s1[x][0],s2[x][0]]))
> ... * * * * if len(d[x]) > 0:
> ... * * * * * * e[x] = d[x]
> ... * * return e
> ...>>>intersect(dict1,dict2)
>
> {'ac': [1, 3, '79b'], 'ab': [2]}
>
>
>
> Best Regards
>
> Mario
>
> On 10 nov, 07:51, Paul Rudin <(E-Mail Removed)> wrote:
>
> > Lawrence D'Oliveiro <(E-Mail Removed)_zealand> writes:
> > > In message <(E-Mail Removed)>, Terry Reedy
> > > wrote:

>
> > >> To echo John Nagle's point, if you want non-masochist volunteers to read
> > >> your code, write something readable like:

>
> > >> dict1 = {'ab': [[1,2,3,'d3','d4',5], 12],
> > >> * * * * * 'ac': [[1,3,'78a','79b'], 54],
> > >> * * * * * 'ad': [[56,57,58,59], 34],
> > >> * * * * * 'ax': [[56,57,58,59], 34]}

>
> > > How come Python itself doesnt display things that way?

>
> > try: pprint.pprint(dict1)

>
>


 
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: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
C (functional programming) VS C++ (object oriented programming) Joe Mayo C Programming 168 10-22-2007 01:00 AM
Can Your Programming Language Do This? Joel on functional programming and briefly on anonymous functions! Casey Hawthorne Python 4 08-04-2006 05:23 AM
dict!ident as equivalent of dict["ident"] Alexander Kozlovsky Python 5 05-22-2006 08:06 AM
Re: dict->XML->dict? Or, passing small hashes through text? Skip Montanaro Python 0 08-15-2003 03:46 PM



Advertisments