简体   繁体   English

python 我需要在 while 循环中不重复单词

[英]python i need to not repeated words in a while loop

I need not to get the words again once its displayed using random.choice ,一旦使用random.choice显示,我就不需要再次获取这些词,

this is the problem it shouldnt repeated words because the words have been already used这是不应该重复单词的问题,因为这些单词已经被使用过

I tried to delete the word using list.remove but It doesn't work and i didn't get any results我尝试使用 list.remove 删除单词,但它不起作用,我没有得到任何结果

import random

a = ["hello", "carls", "theme", "threw", "folks", "dime", "blondie", "ocean"]
c = True
while c:
    c = input("")
    filtered_list = []
    for word in a:
      if c.upper() in word:
        filtered_list.append(word)

    if len(filtered_list) > 0:
        words = random.choice(filtered_list)
        print(words)
    else:
        print(f"there are any words with {c} or all the words have been used")

If i understand your problem correctly, your requirement is once the word displayed, it will not displayed again.如果我对你的问题理解正确的话,你的要求是这个词一旦显示,就不会再显示了。 So delete the word from the list a once you get the value from random.choice , so that you won't get the word again or maintain another list which holds the value already shown因此,一旦您从random.choice获得值,就从列表a中删除单词,这样您就不会再次获得该单词或维护另一个包含已显示值的列表

import random

a = ["hello", "carls", "theme", "threw", "folks", "dime", "blondie", "ocean"]
already_show = []
c = True
while c:
    c = input("Enter Letter to search:")
    filtered_list = []
    for word in a:
      if c in word:
        filtered_list.append(word)

    if len(filtered_list) > 0:
        words = random.choice(filtered_list)
        if words not in already_show:
            already_show.append(words)
            print(words)
        else:
           print("random.choice selected the already selected word")
        already_show = [] #Reset for another search
    else:
       print(f"there are any words with {c} or all the words have been used")

i'm not sure the original message of the example to solve;我不确定要解决的示例的原始消息; but i tried this但我试过了

#!/usr/bin/python3
import random

a = ["hello", "carls", "theme", "threw", "folks", "dime", "blondie", "ocean"]
c = True
while c:
    c = input("")
    print("a ", a)
    filtered_list = []
    for word in a:
        if c.lower() in word:
            a.remove(word)
            filtered_list.append(word)

    if len(filtered_list) > 0:
        words = random.choice(filtered_list)
        print(words)
    else:
        print(f"there are any words with {c} or all the words have been used")

Use another list for used words:使用另一个列表来used单词:

from random import choice

a = ["hello", "carls", "theme", "threw", "folks", "dime", "blondie", "ocean"]

used = []
while c := input('').lower():
    filtered_list = [word for word in a if c in word and word not in used]
    if filtered_list: used += [words := choice(filtered_list)]; print(words)
    else: print(f'there are any words with {c} or all the words have been used'); # used = []

Output: Output:

e
ocean
e
blondie
e
threw
e
dime
e
theme
e
hello
e
there are any words with e or all the words have been used

You might also need to reset used list to empty list after reach else statement for different search input of another character or letter.对于另一个字符或字母的不同搜索输入,您可能还需要在到达 else 语句后将used的列表重置为空列表。

You should start by creating a new normalised list because this code is destructive.您应该首先创建一个新的规范化列表,因为此代码具有破坏性。 In the question, the list contains all lowercase words but it's better to generalise在问题中,列表包含所有小写单词,但最好概括一下

Then:然后:

from random import choice

a = ["hello", "carls", "theme", "threw", "folks", "dime", "blondie", "ocean"]

acopy = [word.lower() for word in a]

while acopy:
    if (c := input('Enter a single character: ').lower()) == '?':
        print(acopy)
    elif len(c) == 1:
        if possibles := [i for i, word in enumerate(acopy) if c in word]:
            print(acopy.pop(choice(possibles)))
        else:
            print(f'There are no words containing {c}')

As written, the while loop will only terminate when the acopy list is empty.正如所写的那样,while 循环只会在acopy列表为空时终止。 You might want to do something else such as empty input.您可能想做其他事情,例如空输入。

Bonus: Enter?奖励:输入? to see what's remaining in the list查看列表中剩余的内容

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM