如何调整我的变量以使程序将符合条件的任何单词附加到新列表中?

huangapple go评论82阅读模式
英文:

How should I adjust my variables in order to make it so if any word fits the criteria in them, the program appends it to a new list?

问题

抱歉,我只会翻译文本内容,不会执行代码。以下是您提供的文本的翻译:

"我如果表达问题有点奇怪,我是Python新手,这是我第一次在Stack Overflow提问。长话短说,我有一个函数,它创建一个用户制定的单词列表(例如'树'、'狗'、'阿拉斯加'等)。find_row函数的目标是找出word_list中哪些单词可以只使用键盘上的一行键来打出。我的问题是,无论键入什么单词(不包括'qwertyuiop'、'asdfghjkl'和'zxcvbnm'),for循环总是打印'单词无法使用一行键打出'。

以下是我的代码:

def find_row(word_list):
    first_row = 'qwertyuiop'
    second_row = 'asdfghjkl'
    third_row = 'zxcvbnm'
    new_list = []
    for word in word_list:
        if word in first_row:
            new_list.append(word)
        elif word in second_row:
            new_list.append(word)
        elif word in third_row:
            new_list.append(word)
        else:
            print("单词无法使用一行键打出")
    return new_list

我已尝试将变量first_row、second_row和third_row更改为列表,以查看是否有所帮助,但似乎没有改变任何内容。我还尝试将if语句更改为"if word in first_row and word not in second_row and word not in third_row",但也没有帮助(我对其他两个elif语句也这样做了)。程序能够按照预期工作的唯一情况是当我输入'qwertyuiop'、'asdfghjkl'或'zxcvbnm'时。"

希望这可以帮助您解决问题。如果您有其他问题,可以随时提出。

英文:

I apologize if my question is worded weirdly, I'm new to Python and this is the first time in Stack overflow. Long story short, I have a function that creates a list (called word_list) of words a user makes (ex. 'tree', 'dog', 'Alaska' etc.) The function find_row is supposed to find what words in word_list can be created only using one row of keys on a keyboard. My problem is that no matter what words (excluding quertyuiop, asdfghjkl, and zxcvbnm) are typed in, the for loop will always print "Word can't be made with one row"

Here is my code:

def find_row(word_list):
first_row = 'qwertyuiop'
second_row = 'asdfghjkl'
third_row = 'zxcvbnm'
new_list = []
for word in word_list:
    if word in first_row:
        new_list.append(word)
    elif word in second_row:
        new_list.append(word)
    elif word in third_row:
        new_list.append(word)
    else:
        print("Word can't be made with one row")
return new_list

I've tried make the variables first_row, second_row, and third_row lists in order to see if that would help, however that didn't seem to change anything. I also tried to make the if statements say "if word in first_row and word not in second_row and word not in third_row" but that didn't help. (I also did that for the other two elif statements) The only time the program works as intended is if I type in qwertyiop, asdfghjkl, or zxcvbnm

答案1

得分: 0

如果我理解你的意思正确,你正在寻找一种集合操作。对于你的示例,你可以使用 set.issuperset 来检查单词是否可以由键盘行字符构成:

def find_row(word_list):
    first_row = set('qwertyuiop')   # <--- 将你的键盘行转换为集合
    second_row = set('asdfghjkl')
    third_row = set('zxcvbnm')

    new_list = []
    for word in word_list:
        if first_row.issuperset(word):
            new_list.append(word)
        elif second_row.issuperset(word):
            new_list.append(word)
        elif third_row.issuperset(word):
            new_list.append(word)
        else:
            print(f"Word {word} 无法由一行构成")

    return new_list

print(find_row(['tree', 'lag', 'cx', 'qaz']))

输出:

Word qaz 无法由一行构成
['tree', 'lag', 'cx']
英文:

If I understand you correctly you're looking for a set operation. For your example you can use set.issuperset to check if the word can be constructed from the row characters:

def find_row(word_list):
    first_row = set(&#39;qwertyuiop&#39;)   # &lt;--- convert your rows to sets
    second_row = set(&#39;asdfghjkl&#39;)
    third_row = set(&#39;zxcvbnm&#39;)

    new_list = []
    for word in word_list:
        if first_row.issuperset(word):
            new_list.append(word)
        elif second_row.issuperset(word):
            new_list.append(word)
        elif third_row.issuperset(word):
            new_list.append(word)
        else:
            print(f&quot;Word {word} can&#39;t be made with one row&quot;)

    return new_list

print(find_row([&#39;tree&#39;, &#39;lag&#39;, &#39;cx&#39;, &#39;qaz&#39;]))

Prints:

Word qaz can&#39;t be made with one row
[&#39;tree&#39;, &#39;lag&#39;, &#39;cx&#39;]

huangapple
  • 本文由 发表于 2023年3月7日 03:45:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定