bing 聊天控制台和 pycharm 的结果不同?

问题描述 投票:0回答:1

当我在 pycharm 中运行此代码时,得到的结果与在 bing chat 中运行时得到的结果不同。

在 pycharm 中我得到

[['abc'], ['a', 'bc'], ['a', 'b', 'c']]

但是在 bing 聊天中我得到了

[['a', 'b', 'c'], ['a', 'bc'], ['ab', 'c'], ['abc']]

My pycharm interpreter(3.11) and decoding(utf-8) are the same as the bing chat.

代码是

def get_all_sublists(char_list):
    if not char_list:
        return [[]]
    rest = get_all_sublists(char_list[1:])
    result = rest + [[char_list[0]] + x for x in rest]
    return result

char_list = ["a","b","c","ab","bc","abc"]
all_sublists = get_all_sublists(char_list)
matching_sublists = [x for x in all_sublists if ''.join(x) == char_list[-1]]
print(matching_sublists)
python pycharm openai-api bing
1个回答
0
投票

结果的差异是由于剩余变量中子列表的顺序造成的。 get_all_sublists 函数使用递归将列表分为两部分:第一个元素和列表的其余部分。然后,它在列表的其余部分上调用自身,并将第一个元素添加到结果中的每个子列表中。结果中子列表的顺序取决于递归的实现方式。

在 PyCharm 中,递归是使用堆栈实现的,这意味着最后添加到结果中的子列表是第一个返回的。这会导致剩余变量中子列表的顺序相反。例如,当 char_list 为 ["a","b","c"] 时,剩余变量为 [[], ['c'], ['b'], ['b', 'c'], ['a']、['a'、'c']、['a'、'b']、['a'、'b'、'c']]。这解释了为什么你得到 [['abc'], ['a', 'bc'], ['a', 'b', 'c']] 作为输出。

在Bing聊天中,递归是使用队列实现的,这意味着添加到结果中的第一个子列表是第一个返回的。这会导致剩余变量中子列表的顺序被保留。例如,当 char_list 为 ["a","b","c"] 时,剩余变量为 [[], ['a'], ['b'], ['a', 'b'], ['c']、['a'、'c']、['b'、'c']、['a'、'b'、'c']]。这解释了为什么您得到 [['a', 'b', 'c'], ['a', 'bc'], ['ab', 'c'], ['abc']] 作为输出。

© www.soinside.com 2019 - 2024. All rights reserved.