0

我在解析一个两列文件后生成了两个元素列表(list1 和 list2)。List1 包含重复不同时间的项目(即 a、a、a、b、b、c、c、c、c、d、d),而 list2 包含它们对应的值,这些值要么重复,如在 list1 中,要么是独特的。

我想要做的是,对于list1中的常用项,获取最大对应数。我正在考虑在 python 中执行此操作,方法是启动一个字典,并使用一个条件,使用 list1 中的唯一项和 list2 中的相应最大值作为键来填充它。

我将不胜感激任何帮助。

谢谢

4

1 回答 1

0

您可以使用以下方法将这两个列表组合成一个对列表zip

# You probably want the values in list2 to be ints
list2 = map(int, list2)
# Combines each item in list1 with the corresponding one in list2
pairs = zip(list1, list2)

然后要创建一个最大值的字典,你可以通过这些对:

max_values = {}
for key, value in pairs:
    current_value = max_values.get(key) # None if the key isn't there.
    if current_value is None or current_value < value:
        max_values[key] = value
于 2013-02-13T10:20:54.347 回答