【问题标题】:Print sorbet flavors打印冰糕口味
【发布时间】:2020-11-10 10:45:43
【问题描述】:

您在一家餐厅工作,他们要求您生成冰糕菜单。

提供一个脚本,打印给定口味列表中所有可能的冰糕二重奏。

不要打印两次相同口味的食谱(没有“巧克力巧克力”),也不要打印两次相同的食谱(如果您打印“香草巧克力”,请不要打印“巧克力香草”,反之亦然- 反之)。

口味是:

FLAVORS = [
    "Banana",
    "Chocolate",
    "Lemon",
    "Pistachio",
    "Raspberry",
    "Strawberry",
    "Vanilla",
]

>我的代码在这里:

FLAVORS = [
    "Banana",
    "Chocolate",
    "Lemon",
    "Pistachio",
    "Raspberry",
    "Strawberry",
    "Vanilla",
]

for i in FLAVORS:
    if len(FLAVORS) >= 2:
        for g in FLAVORS:
            if g==i:
                continue
            else:
                print(f"{i}, {g}")
        FLAVORS.remove(g)

我得到的结果:

Banana, Chocolate
Banana, Lemon
Banana, Pistachio
Banana, Raspberry
Banana, Strawberry
Banana, Vanilla
Chocolate, Banana
Chocolate, Lemon
Chocolate, Pistachio
Chocolate, Raspberry
Chocolate, Strawberry
Lemon, Banana
Lemon, Chocolate
Lemon, Pistachio
Lemon, Raspberry
Pistachio, Banana
Pistachio, Chocolate
Pistachio, Lemon

问题是: 为什么不推荐开心果、覆盆子(或覆盆子、开心果)?

【问题讨论】:

  • 因为您在遍历列表时删除了条目。在PythonTutor 上尝试以了解问题所在。

标签: python python-3.x permutation


【解决方案1】:

您可以执行以下操作:

import itertools
for f in itertools.permutations(FLAVORS, 2):
    if f[0] <= f[-1]:
        print(f)

它会输出:

('Banana', 'Chocolate')
('Banana', 'Lemon')
('Banana', 'Pistachio')
('Banana', 'Raspberry')
('Banana', 'Strawberry')
('Banana', 'Vanilla')
('Chocolate', 'Lemon')
('Chocolate', 'Pistachio')
('Chocolate', 'Raspberry')
('Chocolate', 'Strawberry')
('Chocolate', 'Vanilla')
('Lemon', 'Pistachio')
('Lemon', 'Raspberry')
('Lemon', 'Strawberry')
('Lemon', 'Vanilla')
('Pistachio', 'Raspberry')
('Pistachio', 'Strawberry')
('Pistachio', 'Vanilla')
('Raspberry', 'Strawberry')
('Raspberry', 'Vanilla')
('Strawberry', 'Vanilla')

条件f[0] &lt;= f[-1] 有效,因为颠倒了排列 总是会翻转第一个和最后一个元素之间的关系!

【讨论】:

    【解决方案2】:

    问题是您正在删除循环中的元素,同时在同一个列表上进行迭代。

    为了说明问题的原因,让我们检查一下这段代码:

    FLAVORS = [
        "Banana",
        "Chocolate",
        "Lemon",
        "Pistachio",
        "Raspberry",
        "Strawberry",
        "Vanilla",
    ]
            
            
    for i in FLAVORS:
        print(i)
        FLAVORS.remove(i)   
    

    您希望打印列表中的所有风味,但您却得到:

    Banana
    Lemon
    Raspberry
    Vanilla
    

    试试这个:

        FLAVORS = [
        "Banana",
        "Chocolate",
        "Lemon",
        "Pistachio",
        "Raspberry",
        "Strawberry",
        "Vanilla",
    ]
    
    already_tried = []
    
    for i in FLAVORS:
        if len(FLAVORS) >= 1:
            for g in FLAVORS:
                if g==i or g in already_tried:
                    continue
                else:
                    print(f"{i}, {g}")
            already_tried.append(i)
    
    

    【讨论】:

      【解决方案3】:

      我根据其他答案找到了解决方案(它们对我来说没有按预期工作)。他们唯一缺少的是将 i 变量也附加到 already_tried 列表中。

        already_tried = []
      
      for i in FLAVORS:
          if len(FLAVORS) >= 1:
              for g in FLAVORS:
                  if g == i or g in already_tried:
                      continue
                  else:
                      print(f'{g}, {i}')
                  already_tried.append(g and i)
      

      【讨论】:

        【解决方案4】:

        查看结果,我发现了一种模式,这种组合是由自身和除自身和之前的风味之外的所有事物组成的。 我认为这段代码可能是最简单的。

        FLAVORS = [
            "Banana",
            "Chocolate",
            "Lemon",
            "Pistachio",
            "Raspberry",
            "Strawberry",
            "Vanilla",
        ]
        
        for i in FLAVORS:
            for j in range(FLAVORS.index(i)+1,len(FLAVORS)):
                print(i + ", " + FLAVORS[j])
        

        【讨论】:

          猜你喜欢
          • 2019-11-02
          • 1970-01-01
          • 2019-11-10
          • 1970-01-01
          • 1970-01-01
          • 2022-07-27
          • 2021-08-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多