Home > Back-end >  how do I add keys of a sorted dictionary sequentially having no intersection with other value lists
how do I add keys of a sorted dictionary sequentially having no intersection with other value lists

Time:07-09

I have a dictionary which contains key pair values as key interprets "nodes " and value is a list of communities it belongs to. arranging them according to non-increasing order based on length of list value ,i need to create a list of keys starting from the top rank key and iterate over all keys to find keys with no intersection in their list values with previously added key

this is a dictionary

" {'2179': [15, 197, 363, 594, 766, 865, 1150, 1417, 1575, 1615, 1617, 1618, 1621, 1623, 1624, 1625, 1627], '2188': [15, 363, 766, 1150, 1417, 1616, 1617, 1618, 1619, 1620, 1622, 1624, 1625, 1626, 1629], '2180': [197, 594, 1150, 1575, 1616, 1617, 1618, 1619, 1620, 1622, 1624, 1625, 1626, 1629, 2201], '2195': [1615, 1616, 1617, 1618, 1619, 1620, 1621, 1622, 1623, 1624, 1625, 1626, 1627, 1628, 1629], '2452': [1757, 1758, 1759, 1760, 1761, 1762, 1763, 1765, 1766, 1767, 1768, 1769, 1770, 1771, 1772], '238': [57, 65, 76, 213, 251, 1080, 1126, 1448, 1896, 1897, 1898, 1899, 1900], '6974': [14, 122, 137, 491, 641, 660, 675, 1046, 1800, 2054, 2371], '124': [19, 66, 70, 113, 123, 159, 276, 297, 826, 2122], '3224': [18, 36, 44, 215, 230, 419, 1139, 1259, 2153], '100': [19, 66, 113, 297, 635, 826, 1356, 2122], '553': [40, 50, 133, 135, 192, 526, 1677, 1829]}" . I need to add keys iteratively to the list which have no intersection with list values of previously added key and next key to be added. this is a code i tried "this is a code i tried.

k=len(new_dict)
seed=list(new_dict.keys())[0]
 print(seed)

 CummunitySet=[]
 CommunitySet=set(new_dict.get(seed))
 print(CommunitySet)

 seedSet=set(seed)
 Index=1
 while ((seedCount < k) & (Index < count)):
    seed=list(new_dict.keys())[Index]
    if(set(new_dict.get(seed)).difference(CommunitySet)!=set()):
       CommunitySet = CommunitySet.union(new_dict.get(seed))
       print(CommunitySet)
       seedSet = seedSet.union(set(seed)) 
       Index=Index 1
       seedCount=seedCount 1
    else:
         Index=Index 1

Index=Index 1 print(seedSet)

thankyou.

CodePudding user response:

Maybe you could utilize set.intersection:

def main() -> None:
    data = {
        '2179': [15, 197, 363, 594, 766, 865, 1150, 1417, 1575, 1615, 1617, 1618, 1621, 1623, 1624, 1625, 1627],
        '2188': [15, 363, 766, 1150, 1417, 1616, 1617, 1618, 1619, 1620, 1622, 1624, 1625, 1626, 1629],
        '2180': [197, 594, 1150, 1575, 1616, 1617, 1618, 1619, 1620, 1622, 1624, 1625, 1626, 1629, 2201],
        '2195': [1615, 1616, 1617, 1618, 1619, 1620, 1621, 1622, 1623, 1624, 1625, 1626, 1627, 1628, 1629],
        '2452': [1757, 1758, 1759, 1760, 1761, 1762, 1763, 1765, 1766, 1767, 1768, 1769, 1770, 1771, 1772],
        '238': [57, 65, 76, 213, 251, 1080, 1126, 1448, 1896, 1897, 1898, 1899, 1900],
        '6974': [14, 122, 137, 491, 641, 660, 675, 1046, 1800, 2054, 2371],
        '124': [19, 66, 70, 113, 123, 159, 276, 297, 826, 2122],
        '3224': [18, 36, 44, 215, 230, 419, 1139, 1259, 2153],
        '100': [19, 66, 113, 297, 635, 826, 1356, 2122],
        '553': [40, 50, 133, 135, 192, 526, 1677, 1829]
    }
    new_data = {}
    used_values = set()
    for key, values in data.items():
        values_set = set(values)
        if values_set.intersection(used_values): # Equivalant to `values_set & used_values`.
            continue
        used_values |= values_set
        new_data[key] = values
    print(new_data)

if __name__ == '__main__':
    main()

Output:

{
    '2179': [15, 197, 363, 594, 766, 865, 1150, 1417, 1575, 1615, 1617, 1618, 1621, 1623, 1624, 1625, 1627],
    '2452': [1757, 1758, 1759, 1760, 1761, 1762, 1763, 1765, 1766, 1767, 1768, 1769, 1770, 1771, 1772],
    '238': [57, 65, 76, 213, 251, 1080, 1126, 1448, 1896, 1897, 1898, 1899, 1900],
    '6974': [14, 122, 137, 491, 641, 660, 675, 1046, 1800, 2054, 2371],
    '124': [19, 66, 70, 113, 123, 159, 276, 297, 826, 2122],
    '3224': [18, 36, 44, 215, 230, 419, 1139, 1259, 2153],
    '553': [40, 50, 133, 135, 192, 526, 1677, 1829]
}

Note: Indentation in output has been added manually for readability.

  • Related