输出与字符串匹配的列表的最近值?

2024-05-23 23:36:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我在下象棋。我想得到一个建议

best_move_dataset = Collection of played Moves in the History
move_done = is the move i have played if the app started

Solution-Output (best_move_dataset == move_done): nearest equality
f6
f5

这是我的代码,我真的不知道如何使这个解决方案

best_move_dataset = ['d4 Nc6 e4 e5 f4 f6','d4 Nc6 e4 e5 f4 f5','d4 Nc6 e3 e5 f4']

moves_done = 'd4 Nc6 e4 e5 f4'
moves_done_list = moves_done.split(' ')
len_moves_done = len(moves_done_list)

len_best_move = len(best_move_dataset)

for move_zaehler in range(len_moves_done):
        print(best_move_dataset[0])

我希望你们能理解,如果你们有什么建议,我可以改变的问题,它更清楚,然后请提醒我

希望我能得到一些帮助

致以最诚挚的问候

托比亚斯


Tags: theinmovelendataset建议bestdone
1条回答
网友
1楼 · 发布于 2024-05-23 23:36:48
best_move_dataset = ['d4 Nc6 e4 e5 f4 f6','d4 Nc6 e4 e5 f4 f5','d4 Nc6 e3 e5 f4']
moves_done = 'd4 Nc6 e4 e5 f4'

best_sequences = [str_.split() for str_ in best_move_dataset]
moves_sequence = moves_done.split()
count = len(moves_sequence)
suggestions = []

for best_sequence in best_sequences:
    if len(best_sequence) > count:
        if moves_sequence == best_sequence[:count]:
            suggestions.append(best_sequence[count])

for suggestion in suggestions:
    print(suggestion)

输出:

f6
f5

解释:

将移动序列的字符串拆分为列表后,我们将完成的移动序列与历史中的每个移动序列进行比较(在for循环中)

如果该序列比完成动作的序列长,并且其缩短部分与完成动作的序列相同,我们将该序列的下一个动作添加到准备好的建议列表中

然后在for循环中打印它们


注意:

您可以写下以下内容,而不是使用的列表理解:

best_sequences = []
for str_ in best_move_dataset:
    best_sequences.append(str_.split())

相关问题 更多 >