简体   繁体   English

检查后将字符串追加到列表

[英]Append a string to a list after checking

I was writing a small game which requires just a thing in which when user enters a string it should check if the word is made from the alphabets given and then it should append to a new list. 我正在写一个小型游戏,它只需要一个东西,当用户输入字符串时,它应该检查单词是否由给定的字母组成,然后应附加到新列表中。

user_input = raw_input("Please enter a word: ")
ls = ["a", "f" , "x" , "u"]
user_list = []

for i in user_input:
    if i in ls:
        user_list.append(user_input)

But the problem is that if any of the string is matched the whole word gets appended to the list. 但是问题是,如果匹配任何字符串,整个单词都将附加到列表中。 Like fun , aim would get appended to the list. 就像fun一样, aim也会添加到列表中。

I would use a set . 我会用set Your allowed alphabet could be a set, and then make a set out of your user's word. 您允许的字母可以是一个集合,然后根据用户的单词来进行集合。 If the user specified set is a subset of the alphabet set, then you'll know that the word is composed exclusively of characters from the desired alphabet. 如果用户指定的集合是字母集的子集,那么您将知道该单词仅由所需字母中的字符组成。

user_input = raw_input("Please enter a word: ")
ls = set('afxu')
user_list = []

if set(user_input).issubset(ls):
    user_list.append(user_input)

You should use a boolean and only add if all letters from the input are in your dictionary: 您应该使用布尔值,并且仅当输入中的所有字母都在字典中时才添加:

user_input = raw_input("Please enter a word: ")
ls = ["a", "f" , "x" , "u"]
user_list = []
inDict = True

for i in user_input:
    if not(i in ls):
        inDict = False
        break
if(inDict):    
    user_list.append(user_input)

Another answer for condition using sets is 使用集的条件的另一个答案是

user_input = raw_input("Please enter a word: ")
ls = ["a", "f" , "x" , "u"]
user_list = []


if set(list(user_input)).issubset(set(ls)):
    user_list.append(user_input)

You need to check if each letter in user_input is in you list of allowable letters (which you should probably rename to letter_list or something else as ls is a reserved word in IPython). 您需要检查user_input每个字母是否在允许的字母列表中(您可能应该将其重命名为letter_list或其他名称,因为ls是IPython中的保留字)。

One way to do this is to use reduce with a lambda function: 一种实现方法是对lambda函数使用reduce

if reduce(lambda a, b: a and b, [letter in ls for letter in user_input]):
    user_list.append(user_input)

You can check if all the characters in user_input are in ls 您可以检查user_input中的所有字符是否都在ls

if all(char in ls for char in user_input) 

i in ls will evaluate to True or False, if we hit a char not in ls the loop will short circuit and return False, if all the chars in user_input are in ls it will return True and the word will be appended. i in ls将为True或False,如果我们在ls中未命中一个字符,则循环将短路并返回False,如果user_input中的所有字符都在ls中,则它将返回True并附加该单词​​。

It does not make much sense to create a list for single word and append but if you really want it you can use a list comp: 为单个单词创建一个列表并追加并没有多大意义,但是如果您确实需要,可以使用列表组合:

user_list  = [user_input for i in user_input  if all(char in ls for char in user_input)]

If you are actually wanting to take multiple words, then you can split and check each word: 如果您实际上想要多个单词,则可以拆分并检查每个单词:

user_list  = [word for word in user_input.split() if all(char in ls for char in word)]

Using your own code you would have to check every character first and only append after every char has been checked but using all is a much better approach. 使用您自己的代码,您必须首先检查每个字符,并且仅在检查完每个字符之后才追加,但使用all是一种更好的方法。

For large input making ls a set ls = {"a", "f" , "x" , "u"} would be a more efficient approach as set lookups are 0(1) but for small input sizes it would not make any real difference 对于输入大的ls ,设置ls = {"a", "f" , "x" , "u"}会是一种更有效的方法,因为设置查找为0(1),但对于小尺寸的输入则不会做任何事情真正的差异

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

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