I need help, I have this dict
{
2: {"B", "AA"},
3: {"C", "BA", "AB"},
4: {"D", "BB", "CA", "AC"},
5: {"AD", "E", "CB", "DA", "BC"},
6: {"CC", "F", "EA", "DB", "AE", "BD"},
7: {"G", "CD", "DC", "AF", "FA", "BE", "EB"},
8: {"CE", "AG", "DD", "BF", "GA", "FB", "H", "EC"},
9: {"GB", "ED", "CF", "FC", "I", "HA", "BG", "DE", "AH"},
10: {"DF", "AI", "FD", "J", "GC", "CG", "IA", "BH", "HB", "EE"},
20: {"JJ", "K"},
30: {"JK", "KJ", "L"},
40: {"M", "JL", "KK", "LJ"},
50: {"JM", "KL", "MJ", "LK", "N"},
60: {"JN", "NJ", "MK", "KM", "LL", "O"},
70: {"OJ", "JO", "LM", "P", "NK", "KN", "ML"},
80: {"PJ", "LN", "OK", "KO", "Q", "NL", "MM", "JP"},
90: {"OL", "QJ", "KP", "LO", "JQ", "MN", "R", "PK", "NM"},
100: {"PL", "RJ", "NN", "JR", "QK", "S", "LP", "KQ", "MO", "OM"},
200: {"SS", "T"},
300: {"TS", "ST", "U"},
400: {"TT", "SU", "US", "V"},
500: {"W", "UT", "VS", "TU", "SV"},
600: {"SW", "VT", "X", "WS", "TV", "UU"},
700: {"XS", "Y", "SX", "TW", "WT", "VU", "UV"},
800: {"TX", "Z", "SY", "XT", "UW", "YS", "WU", "VV"},
1: {"A"},
}
and I'm looking for a way to organize it so that duplicates are deleted, how to use this part of the script on the dict
mylist = [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2), (3, 3)]
mylist = [sorted(item) for item in mylist]
mylist = list(set(map(tuple, mylist)))
print(mylist)
[(1, 2), (1, 3), (2, 3), (3, 3)]
to make z input
3: {'C', 'BA', 'AB'}
became the output for example
3: {'C', 'AB'}
I want all duplicates (in reverse order) to be removed from intertol
CodePudding user response:
As your values don't seem to have an order, you could use:
{k: set(''.join(sorted(s)) for s in v) for k,v in d.items()}
or, to ensure a single {'BA'}
would remain as {'BA'}
and not {'AB'}
:
{k: set({''.join(sorted(x)): x for x in v}.values()) for k,v in d.items()}
NB. you cannot however control which item is selected as sets are unordered
output:
{2: {'AA', 'B'},
3: {'AB', 'C'},
4: {'AC', 'BB', 'D'},
5: {'AD', 'BC', 'E'},
6: {'AE', 'BD', 'CC', 'F'},
7: {'AF', 'BE', 'CD', 'G'},
8: {'AG', 'BF', 'CE', 'DD', 'H'},
9: {'AH', 'BG', 'CF', 'DE', 'I'},
10: {'AI', 'BH', 'CG', 'DF', 'EE', 'J'},
...
800: {'SY', 'TX', 'UW', 'VV', 'Z'},
1: {'A'}}
used input:
d = {2: {'B', 'AA'}, 3: {'C', 'BA', 'AB'}, 4: {'D', 'BB', 'CA', 'AC'}, 5: {'AD', 'E', 'CB', 'DA', 'BC'}, 6: {'CC', 'F', 'EA', 'DB', 'AE', 'BD'}, 7: {'G', 'CD', 'DC', 'AF', 'FA', 'BE', 'EB'}, 8: {'CE', 'AG', 'DD', 'BF', 'GA', 'FB', 'H', 'EC'}, 9: {'GB', 'ED', 'CF', 'FC', 'I', 'HA', 'BG', 'DE', 'AH'}, 10: {'DF', 'AI', 'FD', 'J', 'GC', 'CG', 'IA', 'BH', 'HB', 'EE'}, 20: {'JJ', 'K'}, 30: {'JK', 'KJ', 'L'}, 40: {'M', 'JL', 'KK', 'LJ'}, 50: {'JM', 'KL', 'MJ', 'LK', 'N'}, 60: {'JN', 'NJ', 'MK', 'KM', 'LL', 'O'}, 70: {'OJ', 'JO', 'LM', 'P', 'NK', 'KN', 'ML'}, 80: {'PJ', 'LN', 'OK', 'KO', 'Q', 'NL', 'MM', 'JP'}, 90: {'OL', 'QJ', 'KP', 'LO', 'JQ', 'MN', 'R', 'PK', 'NM'}, 100: {'PL', 'RJ', 'NN', 'JR', 'QK', 'S', 'LP', 'KQ', 'MO', 'OM'}, 200: {'SS', 'T'}, 300: {'TS', 'ST', 'U'}, 400: {'TT', 'SU', 'US', 'V'}, 500: {'W', 'UT', 'VS', 'TU', 'SV'}, 600: {'SW', 'VT', 'X', 'WS', 'TV', 'UU'}, 700: {'XS', 'Y', 'SX', 'TW', 'WT', 'VU', 'UV'}, 800: {'TX', 'Z', 'SY', 'XT', 'UW', 'YS', 'WU', 'VV'}, 1: {'A'}}