简体   繁体   English

如何按找到它们的顺序列出它找到的特定单词

[英]How do I list specific words it finds in the order it finds them in

I'm creating a discord bot and was wondering how I could list out the words in order it finds them in?我正在创建一个 discord 机器人,我想知道如何列出单词以便找到它们?

item_dict = {
    "okay": ("Mythical", "50000"),
    "hello": ("Mythical", "17500"),
    "hi": ("Legendary", "14500"),
    "good": ("Legendary", "11600"),
    "very bad": ("Common", "1"),
    }

msg = input("Enter ")
new_list = []

for x in item_dict:
    if x in msg:
        new_list.append(x)
        
print(new_list)

If for example I enter 4 hi 3 very bad good , I would want it to check the string and check if it matches any of the items in the dictionary and make it print [hi, very bad, good] instead of how its ordered in the dictionary which would print out [hi, good, very bad] .例如,如果我输入4 hi 3 very bad good ,我希望它检查字符串并检查它是否与字典中的任何项目匹配并使其打印[hi, very bad, good]而不是它的排序方式会打印出[hi, good, very bad]的字典。

Sort new_list with a lambda , with the key being the position the word is found in msg .使用new_listlambda进行排序, key是 position 该词在msg中找到。

Code:代码:

item_dict = {
    "okay": ("Mythical", "50000"),
    "hello": ("Mythical", "17500"),
    "hi": ("Legendary", "14500"),
    "good": ("Legendary", "11600"),
    "very bad": ("Common", "1"),
    }

msg = input("Enter ")
new_list = []

for x in item_dict:
    if x in msg:
        new_list.append(x)
        
new_list.sort(key=lambda x: msg.find(x))
        
        
print(new_list)

Output: Output:

Enter进入

4 hi 3 very bad good
['hi', 'very bad', 'good']

Instead of looping through the dictionary loop through the message and then you can look for word matches when you hit a letter that matches the start of a key, like so:而不是遍历字典循环遍历消息,然后您可以在点击与键开头匹配的字母时查找单词匹配,如下所示:

item_dict = {
  "okay": ("Mythical", "50000"),
  "hello": ("Mythical", "17500"),
  "hi": ("Legendary", "14500"),
  "good": ("Legendary", "11600"),
  "very bad": ("Common", "1"),
}

msg = input("Enter ")
new_list = []

for index, letter in enumerate(msg):
  for key in item_dict.keys():
    if letter == key[0]:
      if msg[index:index+len(key)] == key:
        new_list.append(key)
        break



print(new_list)

This solution can also handle spaces in the dictionary keys, albeit at a higher computation cost.该解决方案还可以处理字典键中的空格,尽管计算成本更高。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写一个在Postgres列中查找单词的Django查询? - How do I write a Django query that finds words in a Postgres column? 如何遍历特定列中的行,找到一个值,如果找到该值,则将 count = 1 添加到另一列? - How do I iterate through rows in a specific column, find a value, and add a count = 1 to another column if it finds that value? 在 python 中编写一个过程,在列表中查找素数,并将它们存储在一个空列表中 - Writing a procedure in python that finds primes in a list, and stores them in an empty list 如何创建一个在列表中查找单词索引的函数? - How to create a function that finds the index of a word in a list? 如何遍历列表直到找到匹配项? - How to loop over a list until it finds a match? 如何修改这个正则表达式模式,以便一旦找到匹配它返回整个句子,而不仅仅是它匹配的单词? - How can I modify this regex pattern so that once it finds a match it returns the whole sentence, not just the words it matched? 如何创建一个找到第一个内部对词的正则表达式? - How can I make a regular expression which finds the first inner pair words? 我正在创建一个程序,该程序在 2 个列表中找到 6 个数字(无论顺序如何),该列表是随机生成的,常见但我被卡住了 - I am creating a program that finds the 6 numbers (regardless of the order) in 2 list which is generated randomly that is found in common but I'm stuck Python — 无法解析列表数据,如果它在列表数据中找到特定字母,则删除变量 - Python — Having trouble parsing list data that I have where if it finds a specific letter within that list data, deletes the variable 如何纠正我的代码,以便找到异常并相应退出,如果有效,则尝试字符串突变? - How do I correct my code so that it finds the exceptions and quits accordingly and if its valid it tries the string mutation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM