英文:
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('qwertyuiop') # <--- convert your rows to sets
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} can't be made with one row")
return new_list
print(find_row(['tree', 'lag', 'cx', 'qaz']))
Prints:
Word qaz can't be made with one row
['tree', 'lag', 'cx']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论