python - Generating a count of shared IDs between a tuple of item numbers -


i have group object converted in dataframe :

item       id's  1           100,101,102 2           101,103,104 3           100,201,202 

now want generate 2-tuples/ordered pairs, gives me count of ids shared in each pair of items. desired output is:

item  item  id's  1    2      5  1    3      5  2    3      6 

the columns correspond every ordered pair of items such (1,2),(1,3),(2,3) , on , third column tells me how many ids both items have in original table.

if structure have like:

data = {     1: [100, 101, 102],     2: [101, 103, 104],     3: [100, 201, 202], } 

then help:

res = {} items = data.keys() n, in enumerate(items, start=1):     j in items[n:]:         res[(i, j)] = len(             set(data[i]).union(set(data[j]))         ) 

output:

res {(1, 2): 5, (1, 3): 5, (2, 3): 6} 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -