Removing An Item From A List
Solution 1:
How about you check if the word is already in the list before appending it, like so:
def endwords(sent):
wordList = []
words = sent.split()
for word in words:
if"." in word and word not in wordList:
wordList.append(word)
return wordList
You're trying to check if word == list
, but that's seeing if the word is equal to the entire list. To check if an element is in a container in python, you can use the in
keyword. Alternatively, to check if something is not in a container, you can use not in
.
Another option is to use a set:
def endwords(sent):
wordSet = set()
words = sent.split()
for word in words:
if"."in word:
wordSet.add(word)
return wordSet
And to make things a little cleaner, here is a version using set comprehension:
defendwords(sent):
return {word for word in sent.split() if'.'in word}
If you want to get a list out of this function, you can do so like this:
defendwords(sent):
returnlist({word for word in sent.split() if'.'in word})
Since you said in your question you want to check if the word ends with a '.', you probably also want to use the endswith() function like so:
def endwords(sent):
return list({word for word in sent.split() if word.endswith('.')})
Solution 2:
After statement
list = []
you can't use built-in list
class and to understand that you can spend about an hour or so, that's why we avoid names of built-ins for our objects.
More at this answer.
function checks for words that end with a ''.''
Statement
"."in word
checks if word
contains dot symbol (e.g. "." in "sample.text"
will work ok while it simply doesn't end with dot), if you need to check that it ends with dot – use str.endswith
method.
I would like to make sure that duplicate words don't go in the list.
just make sure before storing one that it hasn't been stored already.
Finally we can write
defendwords(sent, end='.'):
unique_words = []
words = sent.split()
for word inwords:if word.endswith(end) and word notinunique_words:
unique_words.append(word)
return unique_words
Test
>>>sent = ' '.join(['some.', 'oth.er'] * 10)>>>unique_words = endwords(sent)>>>unique_words
['some.']
P. S.
If order doesn't matter – use set
, it will take care of duplicates (works only with hashable types, str
is hashable):
defendwords(sent, end='.'):
unique_words = set()
words = sent.split()
for word inwords:if word.endswith(end) and word notinunique_words:
unique_words.add(word)
return unique_words
or with set comprehension
defendwords(sent, end='.'):
words = sent.split()
return {word for word in words if word.endswith(end)}
Solution 3:
You can add a sample judge for the question.
defendwords(sent):
list = []
words = sent.split()
for word in words:
if"."in word:
if word notinlist:
list.append(word)
# bottom if statment does not work for some reason. thats the one i am trying to fix returnlist
Solution 4:
Why not use a set?
def endwords(sent):
my_list = set()
words = sent.split()
for word in words:
if"."in word:
my_list.add(word)
return my_list
Solution 5:
The less verbose way to do it would be using list comprehension, that is
my_list = [word for word in words if '.' in word]
And to ensure the elements aren't duplicated, just use set
.
my_list = set(my_list) # No more duplicated values
Post a Comment for "Removing An Item From A List"