Remove all instances from a list - python

I'm trying to remove all instances of words from a list. I searched and pretty much all the answers are similar to my code below but I can't get it to work. The list simply returns the same list without removing any words. For the print function at the very end if I enter a word like "and" manually instead of the parameter it works so I assume it has something to do with the "remove_words" list.
I'm using vscode and imported a text file with a few paragraphs in them.
myFile = open("mytext.txt")
remove_words = ["and", "a", "to", "the", "if", "is", "it", "of"]
mylist = myFile.read().lower()
newlist = mylist.split(" ")
def remove_items(thelist, item):
final_list = [i for i in thelist if i != item]
return final_list
print(remove_items(newlist, remove_words))

item is a list so just replace the comprehension code as below.
def remove_items(thelist, item):
final_list = [i for i in thelist if not i in item]
return final_list

You are passing remove_words as the item parameter, meaning that you are passing a list and not a word

Your issue is that remove_words is a list of strings, while remove_items expects one string. If you want to not have to change remove_items, you would have to pass each item in remove_words to it separately:
for word in remove_words:
newlist = remove_items(newlist, word)
print(newlist)
As others pointed out and given examples of, though, if you changed remove_items, you could have it accept a list of words to remove, thus simplifying your code.

instead of using "!=" try "not in". Hope that works.

As other answers mention, you pass the list of items rather than a single item to be removed. Try this instead:
def remove_items(thelist, items):
final_list = [i for i in thelist if i not in items]
return final_list

It is best to use with open when handling files.
Use filter to filter out the unwanted words:
remove_words = ["and", "a", "to", "the", "if", "is", "it", "of"]
with open("mytext.txt") as myFile:
mylist = myFile.read().lower()
newlist = mylist.split(" ")
def remove_items(thelist, item):
return list(filter(lambda a: a not in item, thelist))
print(remove_items(newlist, remove_words))

Related

How can I write a function that doesn't print out uppercase words from the list?

I've a list ["ABC", "JAVA", "Python", "user", "CODE"] and I want the functions print out ["Python", "user"].
Here is my progress so far:
def no_upper(my_list : list):
new_list = my_list
for word in my_list:
if word.isupper():
my_list.remove(word)
return new_list
if __name__ == "__main__":
my_list = ["ABC", "JAVA", "Python", "user", "CODE"]
new_list = no_upper(my_list)
print(new_list)
If you objective is to "remove" words written entirely in capital letters:
lst = ["ABC", "JAVA", "Python", "user", "CODE"]
[x for x in lst if x != x.upper()]
OUTPUT
['Python', 'user']
Enclose your strings in quotes.
Your function tries to modify my_list rather than actually building your new_list. Just build the new list and return it without trying to remove the items from the input -- modifying a list you're iterating over will usually have unexpected/buggy results, and even if it worked to do that, there's no good reason to mess around with the caller's data that way.
>>> def no_upper(my_list: list) -> list:
... return [word for word in my_list if not word.isupper()]
...
>>> no_upper(["ABC", "JAVA", "Python", "user", "CODE"])
['Python', 'user']
Try with a list like this:
my_list = ["ABC", "Java", "Python"]
Unless Python will raise an UnboundLocalError, since ABC without quotes ('') or double quotes ("") is parsed as a variable, but there's no ABC variable in your program.
You can also use inline syntax to solve your problem, like this:
def noUpper(l: list):
return [x for x in list if not x.isupper()]

Cut of first and last char of a string in python

I have a list with multiple strings
e.g.:
list = ["This", "is", "a", "test"]
Now I want to create a function that "cuts out" the first and the last char of each string in the given list.
So for this certain example the output of the function should be:
"Tsisaatt"
I have already experimented with list[0][1], but this code:
newList = []
newList = newList.append(list[0][0])
newList = newList.append(satz[0][1])
newList = newList.append(satz[0][2])
...
...
did't do what I want. Did I miss anything or is my idea completely wrong?
Thanks in advance for your feedback!
test.py
list = ["This", "is", "a", "test"]
print("".join([l[0] + l[-1] for l in list]))
Step 1: Just fetch first item with l[0], plus it with last item with l[-1]. It will be ['Ts', 'is', 'aa', 'tt'].
Step 2: Change the new list to string with "".join().
Execution result:
$ python test.py
Tsisaatt
you can do:
newlist.append(mylist[0][0])
newlist.append(mylist[0][-1])
you can write these two lines inside for loop and at last you can join the result
Use list comprehensions
"".join(i[0]+i[-1] for i in list)
You can do it using list comprehension -
output_str = "".join(["".join([x[0], x[-1]]) for x in input_list])
the inner ''.join will join a local list of x[0] and x[-1] and the outer ''.join will join all such list of strings.
You can acces first element of a string using [0] and last element using [-1]
output = ""
for text in text_list:
if text:
output += text[0]+text[-1]
print(output)
You can use this function
def cut_first_and_last(text_list):
output = ""
for text in text_list:
if text:
output += text[0]+text[-1]
return output
or for one-liner
def cut_first_and_last(text_list):
return "".join([text[0]+text[-1] for text in text_list])
If you aren't familiar with list comprehension you can use a simple for loop like this:
mylist = ["this", "is", "a", "list"]
output_string = ""
for item in word_list:
output_string.join(item[0], item[1])
print(output_string)
If you are familiar with list comprehensions just use this one liner:
mylist = ["this", "is", "a", "list"]
print("".join(["".join(item[0],item[1]) for item in mylist])

How to code no duplicates and sorted in python?

I am trying to code a function which can index no duplicates and sorted, and return to a list, here is what I wrote:
def get_common_words_used(sentence, common_words):
new_list = ""
for item in sentence:
if item == common_words:
new_list = new_list.lower()
new_list = [item] + new_list
new_list.sort()
return new_list
def test_get_common_words_used():
print(get_common_words_used("Easy come, easy go go go", ["a", "go", "i", "on", "the"]))
This prints Nothing
But my expected output is ['go']
Please help.
This should help.
def get_common_words_used(sentence, common_words):
new_list = []
for item in set(sentence.split()): #Split by space and use set to remove duplicates.
if item in common_words:
new_list.append(item)
return sorted(new_list) #sort
def test_get_common_words_used():
print(get_common_words_used("Easy come, easy go go go", ["a", "go", "i", "on", "the"]))
test_get_common_words_used()
Output:
['go']
First you need to split the given string into words then you need to check whether that item present in given list or not
def get_common_words_used(sentence, common_words):
new_list = []
for item in sentence.split(" "):# You need to split the string into words by space
if item in common_words:
new_list.append(item)
new_list = list(set(new_list))# List will have the duplicates
new_list.sort()
return new_list
def test_get_common_words_used():
print(get_common_words_used("Easy come, easy go go go", ["a", "go", "i", "on", "the"]))
Output:
['go']

Python list finding with index

I was wondering how would you find the index of a element in a list if you only had part of it. For example
list = ["this", "is", "an", "example", "for", "python"]
how would you find it if you only had "pyt" and needed the index for python???
Straightforward for loop:
def find_starts(val, my_list):
for i, v in my_list:
if v.startswith(val):
return i
(If there is no match then this returns None.) This could be made neater with a list comprehension: see this related question for more details.
As the question is not 100% clear i assume you want to find all items that include the specified string.
alist = ["this", "is", "an", "example", "for", "python"]
val = 'is'
def find_in_list(alist, val):
res = []
for e, v in enumerate(alist):
if val in v:
res.append(e)
return res
find_in_list(alist, val)
You have to work with a solution that work when some element are similar (for example ["foo", "asd", "foobar"] and you have "foo" what would be detected as a correct element?)
The given function will return a list of indexes, which were identified as a correct element.
def find(foo, list):
#Find similar elements
similarElements = [e for e in list if foo in e]
#Find their indexes
indexes = [similarElements.index(i) for i in similarElements]
return indexes
This solution isn't the fastest way, but an easy and understandable.
>>> list = ["this", "is", "an", "example", "for", "python"]
>>> word = "python"
>>> y = [i for i,j in enumerate(list) if j == word]
>>> y
[5]
However, this is for the whole word, try this aswell.
>>> word = "py"
>>> for w in list:
... if word in w:
... sus.append(w)
... print(sus)
...
['python']

Removing character in list of strings

If I have a list of strings such as:
[("aaaa8"),("bb8"),("ccc8"),("dddddd8")...]
What should I do in order to get rid of all the 8s in each string? I tried using strip or replace in a for loop but it doesn't work like it would in a normal string (that not in a list). Does anyone have a suggestion?
Try this:
lst = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
print([s.strip('8') for s in lst]) # remove the 8 from the string borders
print([s.replace('8', '') for s in lst]) # remove all the 8s
Beside using loop and for comprehension, you could also use map
lst = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
mylst = map(lambda each:each.strip("8"), lst)
print mylst
A faster way is to join the list, replace 8 and split the new string:
mylist = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
mylist = ' '.join(mylist).replace('8','').split()
print mylist
mylist = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")]
print mylist
j=0
for i in mylist:
mylist[j]=i.rstrip("8")
j+=1
print mylist
Here's a short one-liner using regular expressions:
print [re.compile(r"8").sub("", m) for m in mylist]
If we separate the regex operations and improve the namings:
pattern = re.compile(r"8") # Create the regular expression to match
res = [pattern.sub("", match) for match in mylist] # Remove match on each element
print res
lst = [("aaaa8"),("bb8"),("ccc8"),("dddddd8")...]
msg = filter(lambda x : x != "8", lst)
print msg
EDIT:
For anyone who came across this post, just for understanding the above removes any elements from the list which are equal to 8.
Supposing we use the above example the first element ("aaaaa8") would not be equal to 8 and so it would be dropped.
To make this (kinda work?) with how the intent of the question was we could perform something similar to this
msg = filter(lambda x: x != "8", map(lambda y: list(y), lst))
I am not in an interpreter at the moment so of course mileage may vary, we may have to index so we do list(y[0]) would be the only modification to the above for this explanation purposes.
What this does is split each element of list up into an array of characters so ("aaaa8") would become ["a", "a", "a", "a", "8"].
This would result in a data type that looks like this
msg = [["a", "a", "a", "a"], ["b", "b"]...]
So finally to wrap that up we would have to map it to bring them all back into the same type roughly
msg = list(map(lambda q: ''.join(q), filter(lambda x: x != "8", map(lambda y: list(y[0]), lst))))
I would absolutely not recommend it, but if you were really wanting to play with map and filter, that would be how I think you could do it with a single line.

Categories

Resources