discord py如何从一个列表中检查它是否是相同的字符串格式但变量不同?

7d7tgy0s  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(209)

所以我对python还很陌生,如果这个问题不好的话,我很抱歉,但是我真的不知道如何看待或描述这个问题。
我一直在尝试让bot对某个有格式的短语做出响应,但我希望它也能对我在格式中使用时放入列表的其他单词做出响应。
以下是我尝试过的:

keywords = {
     "youre": ['ure', 'your'],
     "nice": ["good", "cool"]
}

if self.client.user.mentioned_in(message):
  mention = self.client.user.mention[:2] + '!' + self.client.user.mention[2:]
  replied = False

  for x in range(len(keywords["youre"]) + len(keywords["nice"])):
    if replied: return

    y = max(min(x, len(keywords["youre"]) - 1), 0)
    z = max(min(x, len(keywords["nice"]) - 1), 0)

    youre = keywords["youre"][y]
    nice = keywords["nice"][z]

    if (compare(msg, '{} {}'.format(youre, nice))):
      await message.reply(f'{random.choice(replies).format(name=getName(message))}')
      replied = True

      break

它工作得很好,但是如果我增加列表的大小,只有一些特定的组合可以工作。我该怎么办?

ac1kyiln

ac1kyiln1#

如果您只想列出一个别名列表,以下示例将执行以下操作:


# If you need to keep it in different lists:

keywords = {
     "youre": ['ure', 'your', 'yar'],
     "nice": ["good", "cool", "pleasant"]
}

for x in range(len(keywords["youre"])):
    print(f'{(keywords["youre"])[x]} {(keywords["nice"])[x]}')

# Just to separate the outputs:

print("---------------")

# And if you don't care about the amount of lists:

aliases = ["ure good", "your cool", "yar pleasant"]
for x in aliases:
    print(x)

# Just to separate the outputs:

print("---------------")

# And if you need all the possible combinations:

keywords = {
     "youre": ['ure', 'your', 'yar', 'yer', 'u are'], #The lists have different lenghts, and one of the items has a space in it, yet it still works
     "nice": ["good", "cool", "pleasant"]
}

for x in keywords["youre"]:
    for y in keywords["nice"]:
        print(f"{x} {y}")

输出:

ure good
your cool
yar pleasant
---------------
ure good
your cool
yar pleasant
---------------
ure good
ure cool
ure pleasant
your good
your cool
your pleasant
yar good
yar cool
yar pleasant
yer good
yer cool
yer pleasant
u are good
u are cool
u are pleasant

相关问题