在字典列表python中添加相同的字母数字值

时间:2017-11-23 16:53:34

标签: python dictionary

我有以下字典:

items = {1: ['7a', '3c', '6b'], 2: ['7a', '3c', '6b']}

我想为每个列表添加所有“相似”值的整数分量 (类似意味着字符串以相同的字符结尾。)

我希望最终输出为:

${PARAM}

7 个答案:

答案 0 :(得分:3)

这样可行:

from collections import defaultdict

items = {1: ['1a', '3c', '2a', '1b', '2a', '1b', '1a', '2b', '1a', '2b'],
         2: ['1a', '3c', '2a', '1b', '2a', '1b', '1a', '2b', '1a', '2b']}

for key, value in items.items():
    counting = defaultdict(int)
    for elem in value:
        counting[elem[1]] += int(elem[0])
    items[key] = [str(v) + k for k,v in counting.items()]

print(items)

答案 1 :(得分:1)

以下作品,假设每个字符串的末尾只有一个字母。如果您的字母数字字符串更复杂,请相应地调整索引。

items = {1: ['1a', '3c', '2a', '1b', '2a', '1b', '1a', '2b', '1a', '2b'], 2: ['1a', '3c', '2a', '1b', '2a', '1b', '1a', '2b', '1a', '2b']}

def group_alphanum(alnums):
    import itertools
    alnums = sorted(alnums, key=lambda s: s[-1])
    for grp, items in itertools.groupby(alnums, key=lambda s: s[-1]):
        al = grp[-1]
        num = sum(int(item[:-1]) for item in items)
        yield "{}{}".format(num, al)

grouped_items = {k: list(group_alphanum(vals)) for k, vals in items.items()}
# {1: ['7a', '6b', '3c'], 2: ['7a', '6b', '3c']}

答案 2 :(得分:1)

使用itertools.groupby功能:

import itertools

items = {1: ['1a', '3c', '2a', '1b', '2a', '1b', '1a', '2b', '1a', '2b'], 2: ['1a', '3c', '2a', '1b', '2a', '1b', '1a', '2b', '1a', '2b']}
d = {}               # new dict
f = lambda x: x[-1]  # function to get the last char from each item

for key,l in items.items():
    d[key] = [ str(sum(int(n[:-1]) for n in g)) + k
               for k,g in itertools.groupby(sorted(l, key=f), key=f) ]

print(d)

输出:

{1: ['7a', '6b', '3c'], 2: ['7a', '6b', '3c']}

答案 3 :(得分:1)

这是一种方法:( d是你原来的字典)

from itertools import groupby      
# iterate through the k,value pairs of the original dictionary 
for i, v in d.items():
     # sort the values of a list by the letters : a, b .. 
     v = sorted(v, key=lambda x: x[-1])
     summed_groups = []
     # groupby the elements of a list by the letters: a, b .. 
     for j, k in groupby(v, key=lambda x:x[-1]):
         # sum the int part of each group 
         sum_ints = sum([int(x.replace(j,'')) for x in k])
         # prepare the results 
         summed_groups.append("{}{}".format(sum_ints,j))
     d[i] = summed_groups

结果:

Out[13]: {1: ['7a', '6b', '3c'], 2: ['7a', '6b', '3c']}

答案 4 :(得分:1)

您可以使用itertools.groupby

import itertools
items = {1: ['1a', '3c', '2a', '1b', '2a', '1b', '1a', '2b', '1a', '2b'], 2: ['1a', '3c', '2a', '1b', '2a', '1b', '1a', '2b', '1a', '2b']}
new_items = {a:[(c, list(d)) for c, d in itertools.groupby(sorted(b, key=lambda x:x[-1]), key=lambda x:x[-1])] for a, b in items.items()}
final_items = {a:[str(sum(int(i[0]) for i in d))+c for c, d in b] for a, b in new_items.items()}

输出:

{1: ['7a', '6b', '3c'], 2: ['7a', '6b', '3c']}

答案 5 :(得分:1)

这是一个使用Counter预处理原始items字典的解决方案,然后结果是以Counter为值的字典构建的。< / p>

>>> from collections import Counter
>>> items = {1: ['1a', '3c', '2a', '1b', '2a', '1b', '1a', '2b', '1a', '2b'], 2: ['1a', '3c', '2a', '1b', '2a', '1b', '1a', '2b', '1a', '2b']}
>>> tmp = {k: Counter(''.join(int(x[:-1])*x[-1] for x in v)) for k,v in items.items()}
>>> {k:[str(vv)+kk for kk,vv in v.items()] for k,v in tmp.items()}
{1: ['7a', '3c', '6b'], 2: ['7a', '3c', '6b']}

答案 6 :(得分:1)

import pandas as pd
items = {1: ['1a', '3c', '2a', '1b', '2a', '1b', '1a', '2b', '1a', '2b'], 2: ['1a', '3c', '2a', '1b', '2a', '1b', '1a', '2b', '1a', '2b']}

for key in items:
    s = pd.Series(items[key])
    items[key] = [str(v.str[0].astype(int).sum())+k for k,v in s.groupby(s.str[1])]
print(items)