Girish Sahani wrote: > Gerard Flanagan wrote: >> Girish Sahani wrote: >> > I wrote the following code to concatenate every 2 keys of a dictionary >> and >> > their corresponding values. >> > e.g if i have tiDict1 = tiDict1 = {'a':[1,2],'b':[3,4,5]} i should get >> > tiDict2={'ab':[1,2][3,4,5]} and similarly for dicts with larger no. of >> > features. >> > Now i want to check each pair to see if they are connected...element >> of >> > this pair will be one from the first list and one from the >> second....e.g >> > for 'ab' i want to check if 1 and 3 are connected,then 1 and 4,then 1 >> and >> > 5,then 2 and 3,then 2 and 4,then 2 and 5. >> > The information of this connected thing is in a text file as follows: >> > 1,'a',2,'b' >> > 3,'a',5,'a' >> > 3,'a',6,'a' >> > 3,'a',7,'b' >> > 8,'a',7,'b' >> > . >> > . >> > This means 1(type 'a') and 2(type 'b') are connected,3 and 5 are >> connected >> > and so on. >> > I am not able to figure out how to do this.Any pointers would be >> helpful >> >> >> Girish >> >> It seems you want the Cartesian product of every pair of lists in the >> dictionary, including the product of lists with themselves (but you >> don't say why ;-)). >> >> I'm not sure the following is exactly what you want or if it is very >> efficient, but maybe it will start you off. It uses a function >> 'xcombine' taken from a recipe in the ASPN cookbook by David >> Klaffenbach (2004). >> > > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302478 > > -- > http://mail.python.org/mailman/listinfo/python-list >
>Thanks a lot Gerard and Roberto.but i think i should explain the exact >thing with an example. >Roberto what i have right now is concatenating the keys and the >corresponding values: >e.g {'a':[1,2],'b':[3,4,5],'c':[6,7]} should give me >{'ab':[1,2][3,4,5] 'ac':[1,2][6,7] 'bc':[3,4,5][6,7]} >The order doesnt matter here.It could be 'ac' followed by 'bc' and 'ac'. >Also order doesnt matter in a string:the pair 'ab':[1,2][3,4,5] is same as >'ba':[3,4,5][1,2]. >This representation means 'a' corresponds to the list [1,2] and 'b' >corresponds to the list [3,4,5]. >Now, for each key-value pair,e.g for 'ab' i must check each feature in the >list of 'a' i.e. [1,2] with each feature in list of 'b' i.e. [3,4,5].So I >want to take cartesian product of ONLY the 2 lists [1,2] and [3,4,5]. >Finally i want to check each pair if it is present in the file,whose >format i had specified. >The code Gerard has specified takes cartesian products of every 2 lists. Hi Garish, it's better to reply to the Group. >Now, for each key-value pair,e.g for 'ab' i must check each feature in the >list of 'a' i.e. [1,2] with each feature in list of 'b' i.e. [3,4,5].So I >want to take cartesian product of ONLY the 2 lists [1,2] and [3,4,5]. I'm confused. You say *for each* key-value pair, and you wrote above that the keys were the 'concatenation' of "every 2 keys of a dictionary". Sorry, too early for me. Maybe if you list every case you want, given the example data. All the best. Gerard -- http://mail.python.org/mailman/listinfo/python-list