如何通过一次选择两个并要求某人在它们之间进行选择来缩小 python 中的选项列表?

How to narrow down a list of options in python by picking two at a time and asking someone to pick between them?

提问人:JoseGonzales 提问时间:10/26/2023 更新时间:10/26/2023 访问量:45

问:

我想创建一个程序,允许您选择自己喜欢的项目。为此,我想一次向用户提供两个选项。我将从项目列表开始,我需要程序选择前两个,然后将它们呈现给用户。用户将选择一个项目,然后将此项目添加到结果列表中。程序将遍历第一个列表,直到它显示所有选项,然后使用结果列表作为输入。最后,最终列表中应该只有一个项目。

我已经编写了一些代码来向用户显示选项,但我想不出一种方法将结果列表用作同一程序的输入。

list1 = ['1','2','3','4']
dict1 = dict(enumerate(list1,0))
def narrow (dict, listing):
    for k in list(dict.keys()):
        print('dictionary has ' + str(len(list(dict.keys()))) + '  keys')
        if len(list(dict.keys())) == 0:
            break
        elif len(list(dict.keys())) != 0:
            if len(list(dict.keys())) != 3:
                choice = input (dict[k] + ' or ' + dict[k+1] + ' ')
                if choice == 'a':
                    listing.append (dict[k])
                    print(listing)
                    dict.pop(k)
                    dict.pop(k+1)
                    print(dict)
                elif choice == 'b':
                    listing.append (dict[k+1])
                    print(listing)
                    dict.pop(k)
                    dict.pop(k+1)
                    print(dict)
                narrow(dict, listing)
                return(listing)
            elif len(list(dict.keys())) == 3:
                choice = input (dict[k] + ' or ' + dict[k+1] + ' or ' + dict[k+2] + ' ')
                if choice == 'a':
                    listing.append (dict[k])
                    print(listing)
                    dict.pop(k)
                    dict.pop(k+1)
                    dict.pop(k+2)
                    print(dict)
                elif choice == 'b':
                    listing.append (dict[k+1])
                    print(listing)
                    dict.pop(k)
                    dict.pop(k+1)
                    dict.pop(k+2)
                    print(dict)
                elif choice == 'c':
                    listing.append (dict[k+2])
                    print(listing)
                    dict.pop(k)
                    dict.pop(k+1)
                    dict.pop(k+2)
                    print(dict)
                narrow(dict, listing)
                return(listing)
list2 = []
narrow(dict1,list2)
Python 函数 循环 递归

评论

0赞 JonSG 10/26/2023
您是要在内部更新结果还是要返回结果?和 的目的是什么?您对替代方法的想法感兴趣吗?narrow()narrow()list1dict1
0赞 JonSG 10/26/2023
看起来您想接受一个列表并返回最好的项目,在我看来这是正确的方法。但是,我不清楚您所说的“一种将结果列表用作同一程序的输入的方法”是什么意思

答:

0赞 Mathias R. Jessen 10/26/2023 #1

一个相当简单的方法:

  • 提取列表中的前 2 个项目
  • 让用户在 2 之间进行选择
  • 将所选值追加到剩余项的列表中
  • 重复上述步骤,直到列表只有一个项目
item_list = [1,2,3,4]

while len(item_list) > 1:
    # unpack current list, grab first two items
    a, b, *item_list = item_list

    # prompt user to pick
    response = input(f"Would you like option (a) '{a}' or option (b) '{b}'?\n")
    if response == 'a':
        item_list.append(a)
    else:
        item_list.append(b)

评论

0赞 JoseGonzales 10/26/2023
谢谢Mathias!我非常喜欢你的代码!
1赞 JonSG 10/26/2023 #2

与其修改你传入的列表,我建议你拿一份副本并使用它。如果用户想要更新原始版本,他们可以决定自己这样做。

请注意,这在概念上类似于 @mathias-r-jessen 的答案

import random

##-----------------
## Given a list of values, return the favored one.
##-----------------
def narrow(values):
    if not values:
        return None

    values = list(set(values))  ## take a copy and remove duplicates

    while len(values) > 1:
        choices = random.sample(values, 2)
        choice = input(f"Do you prefer \"{choices[0]}\" over \"{choices[1]}\" (y|n)? ")
        item_to_remove = choices[1] if choice == "y" else choices[0]
        values.remove(item_to_remove)

    return values[0]
##-----------------

all_values = ["1","2","3","4"]
favored_value = narrow(all_values)
print(f"Out of all the values in {all_values}, your favorite was '{favored_value}'")

评论

1赞 JoseGonzales 10/26/2023
谢谢JonSG,这是一个绝妙的解决方案