如何将字典中的键更改为大写,并在结果字典中添加相同键的值?

时间:2021-06-02 19:46:51

I have a dictionary that looks like this:

我有一个字典,看起来像这样:

d = {'A':110, 'a':100, 'T':50, 't':5}

I want to change the keys to upper case and combine A+a and T+t and add their values, so that the resulting dictionary looks like this:

我想将键更改为大写并组合A + a和T + t并添加它们的值,以便生成的字典如下所示:

d = {'A': 210, T: 55}

This is what I tried:

这是我试过的:

for k, v in d.items():
    k.upper(), v

and the result is:

结果是:

('A', 110)
('A', 100)
('T', 50)
('t', 5)

I looks like tuples but I want to change it in the dictionary, so I tried to write a function:

我看起来像元组,但我想在字典中更改它,所以我尝试编写一个函数:

def Upper(d):
    for k, v in d.items:
        k.upper(), v
    return d

but it returns the dictionary unchanged.

但它返回字典不变。

After I have changed the keys to upper case I had found this solution to how to add values of keys in a dictionary:

在我将键更改为大写后,我找到了如何在字典中添加键值的解决方案:

dict([(x, a[x] + b[x]) if (x in a and x in b) else (x, a[x]) if (x in a) else (x, b[x])

but first I need to get the keys to upper case!

但首先我需要把钥匙拿到大写!

6 个解决方案

#1


11  

Counter does this quite nicely

计数器做得非常好

>>> d = {'A':110, 'a':100, 'T':50, 't':5}
>>> from collections import Counter
>>> c = Counter()
>>> for k,v in d.items():
...     c.update({k.upper(): v})
... 
>>> c
Counter({'A': 210, 'T': 55})

#2


4  

upper() method doesn't change anything. You can use the following code:

upper()方法不会改变任何东西。您可以使用以下代码:

def capitalize_keys(d):
    result = {}
    for key, value in d.items():
        upper_key = key.upper()
        result[upper_key] = result.get(upper_key, 0) + value
    return result

#3


2  

defaultdict is helpful:

defaultdict很有帮助:

>>> from collections import defaultdict
>>> d = {'A':110, 'a':100, 'T':50, 't':5}
>>> new_d = defaultdict(int)
>>> for key, val in d.iteritems():
...     new_d[key.upper()] += val
...
>>> dict(new_d)
{'A': 210, 'T': 55}

#4


1  

I think you have to rebuild your dictionary. Example:

我想你必须重建你的字典。例:

from collections import defaultdict

d={'A':110, 'a':100, 'T':50, 't':5}

def upper(d):
        nd=defaultdict(int)
        for k, v in d.iteritems():
                nd[k.upper()]+=v
        return dict(nd)

print d
print upper(d)

Output:

{'A': 110, 'a': 100, 'T': 50, 't': 5}
{'A': 210, 'T': 55}

Or use the solution from @citxx with result.get(upper_key, 0) + value and avoid the defaultdict altogether.

或者使用@citxx中带有result.get(upper_key,0)+值的解决方案,并完全避免使用defaultdict。

#5


1  

With this function (corrected):

有了这个功能(更正):

>>> def upper_kdict(d):
...     r = {}
...     for k, v in d.items():
...             K = k.upper()
...             r[K] = v if not r.__contains__(K) else v + r[K]
...     return r
... 
>>> 
>>> d = {'a': 100, 'A': 110, 'b': 20, 'B': 1000, 'C': 150, 'c': 100, 'd': 180}
>>> upper_kdict(d)
{'A': 210, 'C': 250, 'B': 1020, 'D': 180}

#6


0  

Use a very long dictionary comprehension:

使用很长的字典理解:

d = {'A':110, 'a':100, 'T':50, 't':5}
d = dict((k, v + d.get(k.lower(), 0)) if (k == k.upper()) 
    else (k.upper(), v) for (k, v) in d.items() 
    if ((k == k.upper()) or (k == k.lower() and not (k.upper() in d))))
print d

Output:

{'A': 210, 'T': 55}

#1


11  

Counter does this quite nicely

计数器做得非常好

>>> d = {'A':110, 'a':100, 'T':50, 't':5}
>>> from collections import Counter
>>> c = Counter()
>>> for k,v in d.items():
...     c.update({k.upper(): v})
... 
>>> c
Counter({'A': 210, 'T': 55})

#2


4  

upper() method doesn't change anything. You can use the following code:

upper()方法不会改变任何东西。您可以使用以下代码:

def capitalize_keys(d):
    result = {}
    for key, value in d.items():
        upper_key = key.upper()
        result[upper_key] = result.get(upper_key, 0) + value
    return result

#3


2  

defaultdict is helpful:

defaultdict很有帮助:

>>> from collections import defaultdict
>>> d = {'A':110, 'a':100, 'T':50, 't':5}
>>> new_d = defaultdict(int)
>>> for key, val in d.iteritems():
...     new_d[key.upper()] += val
...
>>> dict(new_d)
{'A': 210, 'T': 55}

#4


1  

I think you have to rebuild your dictionary. Example:

我想你必须重建你的字典。例:

from collections import defaultdict

d={'A':110, 'a':100, 'T':50, 't':5}

def upper(d):
        nd=defaultdict(int)
        for k, v in d.iteritems():
                nd[k.upper()]+=v
        return dict(nd)

print d
print upper(d)

Output:

{'A': 110, 'a': 100, 'T': 50, 't': 5}
{'A': 210, 'T': 55}

Or use the solution from @citxx with result.get(upper_key, 0) + value and avoid the defaultdict altogether.

或者使用@citxx中带有result.get(upper_key,0)+值的解决方案,并完全避免使用defaultdict。

#5


1  

With this function (corrected):

有了这个功能(更正):

>>> def upper_kdict(d):
...     r = {}
...     for k, v in d.items():
...             K = k.upper()
...             r[K] = v if not r.__contains__(K) else v + r[K]
...     return r
... 
>>> 
>>> d = {'a': 100, 'A': 110, 'b': 20, 'B': 1000, 'C': 150, 'c': 100, 'd': 180}
>>> upper_kdict(d)
{'A': 210, 'C': 250, 'B': 1020, 'D': 180}

#6


0  

Use a very long dictionary comprehension:

使用很长的字典理解:

d = {'A':110, 'a':100, 'T':50, 't':5}
d = dict((k, v + d.get(k.lower(), 0)) if (k == k.upper()) 
    else (k.upper(), v) for (k, v) in d.items() 
    if ((k == k.upper()) or (k == k.lower() and not (k.upper() in d))))
print d

Output:

{'A': 210, 'T': 55}