【问题标题】:Python3 merge tuples in a list and remove duplicatesPython3 合并列表中的元组并删除重复项
【发布时间】:2020-03-10 19:25:47
【问题描述】:

过去可能有与此类似的请求,但无法找到我正在寻找的请求,

Input = [('Icecream', 'Vanilla'), ('Icecream', 'Chocolate'), ('Icecream', 'Strawberry')]
Output = [('Icecream', ['Vanilla', 'Chocolate', 'Strawberry'])]

基本上,给定一个元组列表,需要合并元组以形成一个不重复的元组列表,其中每个元组中的第二个元素必须是一个列表。

输入列表可能包含更多项目,如下所示,

Input = [('Icecream', 'Vanilla'), ('Icecream', 'Chocolate'), ('Icecream', 'Strawberry'), ('Veggie', 'Carrot'), ('Milk', 'whole'), ('Milk', 'formula')]

Output = [('Icecream', ['Vanilla', 'Chocolate', 'Strawberry']), ('Veggie', ['Carrot']), ('Milk', ['whole', 'formula'])]

【问题讨论】:

  • 提供的示例对于每个元组都有相同的第一项 - 'Icecream'。会一直这样吗?
  • 是的,假设元组中的第一项相同
  • 如果第一个元素始终相同,是否需要输出为列表?似乎Output = ('Icecream', ['Vanilla', 'Chocolate', 'Strawberry']) 就足够了。

标签: python-3.x list tuples python-3.6


【解决方案1】:

我认为解决您的问题的最佳方法是将 Input 转换为 字典

Input = [('Icecream', 'Vanilla'), ('Icecream', 'Chocolate'), ('Icecream', 'Strawberry'), ('Veggie', 'Carrot'), ('Milk', 'whole'), ('Milk', 'formula')]

new_dict = {}
for item in Input:
    key, *values = item
    if key not in new_dict:
        new_dict[key] = []
    new_dict[key].append(*values)
print(new_dict)

输出:

{'Icecream': ['Vanilla', 'Chocolate', 'Strawberry'], 'Veggie': ['Carrot'], 'Milk': ['whole', 'formula']}

使用这种方法,您可以轻松获得所需的物品:

icecream_flavors = new_dict["Icecream"]
print(icecream_flavors)

输出:

['Vanilla', 'Chocolate', 'Strawberry']

但如果你真的想要一个 元组列表,只需将 dictionary 转换为 list

new_list = list(new_dict.items())
print(new_list)

输出:

[('Icecream', ['Vanilla', 'Chocolate', 'Strawberry']), ('Veggie', ['Carrot']), ('Milk', ['whole', 'formula'])]

【讨论】:

    【解决方案2】:

    假设元组中的第一项始终相同,这将满足您的需求。

    Input = [('Icecream', 'Vanilla'), ('Icecream', 'Chocolate'), ('Icecream', 'Strawberry')]
    
    flavors = []
    for i in Input:
        if i[1] not in flavors:
            flavors.append(i[1])
    
    Output = [(Input[0][0], flavors)]
    

    【讨论】:

      【解决方案3】:

      对此有 2 种不同的解决方案。元组的元素可以相互交换吗?像这样

      Input = [('Vanilla', 'Icecream'), ('Icecream', 'Chocolate'), ('Icecream', 'Strawberry')]
      

      如果不是,答案很简单。您可以使用计数器和 while 循环(使用 while 循环很重要,因为据我在 python 中的记忆,for 循环在列表的克隆内遍历,这可能是一个问题)在列表中遍历,对于每个元素,检查另一个元素。让我们编码吧。

      首先,将第二个元素更改为列表很重要。我们想要这样的输入

      Input = [('Icecream', ['Vanilla']), ('Icecream', ['Chocolate']), ('Icecream', ['Strawberry'])]
      

      要做到这一点,这应该可以工作

      for tuple in Input:
          tuple[1] = [tuple[1]]
      

      整理好我们的输入后,现在我们可以做你想做的事了

      counter = 0
      while counter<len(Input):
          for tuple in Input[counter+1:]:
              if Input[counter][0] == tuple[0]:
                  Input[counter][1].append(tuple[1][0])
                  Input.remove(tuple)
      counter = counter+1
      

      我没有尝试过(所以可能会出现错误),但我希望您能理解我尝试做的事情。我相信你可以用你自己的风格在你自己的代码中实现它。

      顺便说一句,使用字典数据类型而不是元组列表会容易得多。我建议你搜索字典。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-22
        • 2016-10-24
        • 2015-11-29
        • 1970-01-01
        • 1970-01-01
        • 2020-05-20
        • 2017-06-05
        • 2015-01-01
        相关资源
        最近更新 更多