... 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 <moura.ma...@gmail.com> 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 <paul.nos...@rudin.co.uk> wrote: > > > Lawrence D'Oliveiro <l...@geek-central.gen.new_zealand> writes: > > > In message <mailman.787.1289336127.2218.python-l...@python.org>, 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) > > -- http://mail.python.org/mailman/listinfo/python-list