【问题标题】:How can lists in a list be overwritten by new lists in a new list in Python?列表中的列表如何被 Python 中的新列表中的新列表覆盖?
【发布时间】:2015-01-15 23:55:37
【问题描述】:

如果有人知道如何表达问题以使其更清晰,请随时编辑!

我有一个这样的列表:

a = [["Adolf", "10"], ["Hermann", "20"], ["Heinrich", "30"]]

我有一个这样的“更新”列表:

b = [["Rudolf", "40"], ["Adolf", "50"]]

我希望能够添加新条目并使用列表 b 覆盖列表 a 中的相同条目:

c = magic(a, b)

结果应该如下:

>>> print(c)
[["Adolf", "50"], ["Hermann", "20"], ["Heinrich", "30"], ["Rudolf", "40"]]

是否存在类似魔法的功能?如果不是,怎么写?


编辑:为了清楚起见,我知道字典方法。出于我的目的,我需要有重复的条目,所以字典不合适——这就是我询问列表的原因。在提及之前,将对这些特殊的重复条目进行保护。例如,假设“Heinrich”是这些可以有重复项的特殊类型之一:

a = [['Adolf', '10'], ['Hermann', '20'], ['Heinrich', '30'], ['Heinrich', '15']]

现在,假设我有如下更新列表:

b = [['Rudolf', '40'], ['Adolf', '50']]

使用列表b 更新列表a 应生成以下列表:

>>> print(c)
[['Adolf', '50'], ['Hermann', '20'], ['Heinrich', '30'], ['Heinrich', '15'], ['Rudolf', '40']]

如您所见,有预期的重复条目。这就是为什么不能直接使用字典的原因。

【问题讨论】:

  • 您使用了错误的数据结构。这应该在字典里。
  • 不,这没有什么神奇的功能。但是您可以为此使用collections.Counter。顺便说一句,顺序重要吗?
  • 你有没有你尝试过来弄清楚它是怎么写的?有什么想法吗?不应该是['Adolf', '60']吗?它们真的应该是字符串而不是数字吗?
  • @g.d.d.c 我知道字典的方法。我需要使用 duplicate 条目,因此字典不适用于我的目的。 (任何被更新的条目不得重复。)@Ashwini Chaudhary保持顺序是可取的,是的。
  • 你是什么意思“我需要有重复的条目”?在那种情况下,["Adolf", "10"] 去哪儿了?什么情况下允许重复?合并的逻辑是什么?

标签: python list overwrite


【解决方案1】:

您可能应该为此使用字典。无论如何,您实际上是在模仿它的功能。如果你有字典 a 和字典 b:

a = { "Adolf": "10", "Hermann": "20", "Heinrich": "30" }
b = { "Rudolf": "40", "Adolf": "50" }

那么你可以这样做:

a.update(b)

一行!易如反掌。我认为这与您将要使用内置的magic() 一样接近。但是,如果您只需要使用列表来执行此操作,而 ever 使用字典(因为我无法想象为什么可以使用字典作为中间步骤,但不存储您的数据一),你可以这样做:

def magic(original, update):
    for i in range(len(update)):
        found_match = False

        for j in range(len(original)):
            # if there's a matching 'key' in an index of the original list
            if update[i][0] == original[j][0]:
                found_match = True
                original[j][1] = update[i][1]

        # if there's no entry to override, place a new entry in
        if not found_match:
            original.append(update[i])

同样,除非您有一些非常奇怪的用例,否则我无法想象为什么这比字典更可取的集合或性能。

【讨论】:

  • 非常感谢您的帮助。字典方法非常干净,但我需要有重复的条目,所以我将采用列表方法。再次感谢。
【解决方案2】:
>>> a = [["Adolf", "10"], ["Hermann", "20"], ["Heinrich", "30"]]
>>> b = [["Rudolf", "40"], ["Adolf", "50"]
>>> c = [list(i) for i in (dict(a+b).items())] # if b is update list then use (a+b), if you write (b+a) it will treat a as update list.
>>> c
[['Hermann', '20'], ['Rudolf', '40'], ['Adolf', '50'], ['Heinrich', '30']]

【讨论】:

  • 感谢您的帮助以及您在内部使用字典的非常清晰和有效的方法。碰巧,我需要重复的条目,所以我要使用列表方法。
【解决方案3】:

我建议你使用他们提到的字典。

我假设你已经给出了列表 ab

def magic(a, b):
    d = dict(a)   # convert a to a dictionary
    d.update(b)   # value updates
    result = [ list(i) for i in d.iteritems() ]
    return result

结果是你想要的形式

【讨论】:

  • 感谢您的帮助以及有关如何使用字典方法制作函数的详细信息。碰巧,我需要重复的条目,所以我要使用列表方法。
【解决方案4】:
  1. a 转换为字典
  2. a 上调用更新方法,并使用一个包含b 的字典作为参数
  3. 获取a更新版本中的项目列表

这是一个例子:

adict = dict(a)
adict.update(dict(b))
[list(item) for item in adict.iteritems()]

【讨论】:

  • 感谢您的帮助。你的解释很清楚。碰巧,我需要重复的条目,所以我要使用列表方法。
猜你喜欢
  • 2014-10-14
  • 2016-02-25
  • 2021-03-22
  • 2020-08-28
  • 2019-11-14
  • 2023-02-24
  • 1970-01-01
  • 1970-01-01
  • 2021-04-01
相关资源
最近更新 更多