简体   繁体   中英

How to remove an item in a list that has a specific ordering of words

Say I have the list

mylist = ["hello there", "Watermelons are delicious", "What is the color of my shirt"]
otherlist = ["1", "2", "3"]

and I want to check if "is the color of" is an ordering of words in an index of mylist. If it is than I want to remove that index from mylist and otherlist.

To be more specific, I want the end result to be:

otherlist = ["1", "2"]
mylist = ["hello there", "Watermelons are delicious"]

I was thinking something like:

while "is the color of" in mylist:
    del otherlist[mylist.index("is the color of")]
    del mylist[mylist.index("is the color of")]

However, this code does not work.

If you want an exact match , use word boundaries with re.search:

import re

mylist = ["hello there", "Watermelons are delicious", "What is the colour of my shirt"]

mylist[:] = [s for s in mylist if not re.search(r"\bis the colour\b",s)])

Output:

['hello there', 'Watermelons are delicious']

mylist[:] will mean you mutate the original list, using word boundaries means is the colours etc.. won't be matched which may or may not be the desired behaviour.

If you want to get the indexe(s) of the string(s) that contains the substring use enumerate keeping the ind if re.search(r"\\bis the colour\\b",s) :

print([ind for ind, s  in enumerate(mylist) if re.search(r"\bis the colour\b",s)])

Output:

[2]

If you only want the first match if there may be more than one:

ind = next((s for s in mylist f re.search(r"\bis the colour\b",s)),None)
if ind:
    print(ind)

If you want to remove from both lists together, zip, check for the substring match and remove if there is a match:

 mylist = ["red is the color of my shirt", "hello there", "foo", "Watermelons are delicious",
          "What is the color of my shirt", "blue is the color of my shirt", "foobar"]
otherlist = ["0", "1", "2", "3", "4", "5", "6"]

for s1, s2 in zip(mylist, otherlist):
    if re.search(r"\bis the color\b", s1):
        mylist.remove(s1)
        otherlist.remove(s2)

print(mylist)

print(otherlist)

Output:

['hello there', 'foo', 'Watermelons are delicious', 'foobar']
['1', '2', '3', '6']

I'm guessing you're trying to see if the string "is the color of" part of any of the strings present in the list and if so you want to remove that list element. The only way to do this would be to loop through the list ( for item in list ) and then using the in keyword to check for the sub-string you're searching for inside the list element. An easy way to do so would be to create a new list and if the conditions are met (ie the list element doesn't contain the sub-string you're searching for) copy the list element into the new list. If the condition is met however you can put in a continue to skip it.

Edit: Seeing as how you want to modify 2 lists depending upon the condition matching for one you could do the following

mylist = ["hello there", "Watermelons are delicious", "What is the colour of my shirt"]
newlist = []
otherlist = ["1", "2", "3"]
for item in mylist:
  if "is the color of" in item:
    otherlist.pop(mylist.index(item));
    continue;
else:
 newlist.append(item)
def find_and_remove(phrases, string_to_find):
    for index, phrase in enumerate(phrases):
        if string_to_find in phrase:
            phrases.pop(index)
            break

mylist = ["hello there", "Watermelons are delicious", "What is the colour of my shirt"]
find_and_remove(mylist, "is the colour of")
print mylist

Here's a similar way to find and remove the first instance of string_to_find .

import re
print [i for i in mylist if not re.findall(r"is the colour of",i)]

You can use re over here.

For index of element containing the string

print [mylist.index(i) for i in mylist if re.findall(r"is the colour of",i)]

or

k= [i for i,j in enumerate(mylist) if re.findall(r"is the colour of",j)]

for removing from otherlist = ["1", "2", "3"]

you can do

print [i for i in otherlist if i not in k]

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