I need help on how to remove duplicate items in a tuple of a dictionary.
dict of {tuple of (str, str, str, str): int}) -> tuple of (str, None)
Here is the dictionary:
{('ALPHA', 'BETA', 'GAMMA', 'DELTA'): 5
('BETA', 'GAMMA', 'ALPHA', 'DELTA'): 3
('DELTA', 'BETA', 'GAMMA', 'ALPHA'): 1
('GAMMA', 'DELTA', 'ALPHA', 'BETA'): 3
('BETA', 'ALPHA', 'DELTA', 'GAMMA'): 4}
and the integer is the value of the first index of the tuple such that I got to group them by:
def rad_type(particle):
my_dict = {}
for (k, v) in particle.items():
if (k[0] in my_dict):
my_dict[k[0]] += v
else:
my_dict[k[0]] = v
return my_dict
This returns:
{'ALPHA': 5, 'BETA': 7, 'GAMMA': 3, 'DELTA': 1}
Since 'DELTA' has the least value which in this case is 1, but I want to remove the element like this:
{('ALPHA', 'BETA', 'GAMMA'): 5
('BETA', 'GAMMA', 'ALPHA'): 7
('BETA', 'GAMMA', 'ALPHA'): 1
('GAMMA', 'ALPHA', 'BETA'): 3}
This gives ALPHA = 5, BETA = 8, GAMMA = 3; this is what I really need in terms of a dictionary.
I tried to remove the least element and it’s not working?
for (p, v) in my_dict.items():
if (max(my_dict.values()) / sum(my_dict.values()):
if (v == min(my_dict.values())):
del my_dict[p]
return my_dict
But this gives ALPHA = 5, BETA = 7, GAMMA = 3
Since this return a dictionary, how do I remove the duplicates with a tuple and return it back as a dictionary without importing anything?
The problem is similar to this.
here is a suggestion ..
if we use your function:
and if we define another function that uses your function :
if we apply this function on the dictionary particle :
the output is :