我有2个彼此关联的列表。例如,在这里,“约翰”与“ 1”相关联,“鲍勃”与4相关联,依此类推:

l1 = ['John', 'Bob', 'Stew', 'John']
l2 = [1, 4, 7, 3]


我的问题是约翰重复。我不想添加重复的John,而是要取与John关联的值的平均值,即1和3,即(3 +1)/ 2 =2。因此,我希望列表实际上是:

l1 = ['John', 'Bob', 'Stew']
l2 = [2, 4, 7]


我已经尝试了一些解决方案,包括for循环和“ contains”函数,但似乎无法将其组合在一起。我对Python不太熟悉,但是听起来链表似乎可以用于此目的。

谢谢

最佳答案

我相信您应该使用字典。 :)

def mean_duplicate(l1, l2):
    ret = {}
    #   Iterating through both lists...
    for name, value in zip(l1, l2):
        if not name in ret:
            #   If the key doesn't exist, create it.
            ret[name] = value
        else:
            #   If it already does exist, update it.
            ret[name] += value

    #   Then for the average you're looking for...
    for key, value in ret.iteritems():
        ret[key] = value / l1.count(key)

    return ret

def median_between_listsElements(l1, l2):
    ret = {}

    for name, value in zip(l1, l2):
        #   Creating key + list if doesn't exist.
        if not name in ret:
            ret[name] = []
        ret[name].append(value)

    for key, value in ret.iteritems():
        ret[key] = np.median(value)

    return ret

l1 = ['John', 'Bob', 'Stew', 'John']
l2 = [1, 4, 7, 3]

print mean_duplicate(l1, l2)
print median_between_listsElements(l1, l2)
# {'Bob': 4, 'John': 2, 'Stew': 7}
# {'Bob': 4.0, 'John': 2.0, 'Stew': 7.0}

10-07 16:31
查看更多