简体   繁体   中英

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.

I would use a 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).

One way to do this is to use reduce with a lambda function:

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

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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