0

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.

PiccolMan
  • 4,854
  • 12
  • 35
  • 53
  • 1
    What do you mean - *if "is the color of" is a ordering of words in an index of mylist* ? Do you mean if "is the color of" is a substring of an element in mylist? – Anand S Kumar Aug 17 '15 at 19:13
  • 2
    So would a sentence containing `"is not the real color of"` match? It has those specific words in that order in there. Also, you are mixing UK and US spelling, your text uses *color* (US spelling), but your *sample text* has `colour` (UK spelling). – Martijn Pieters Aug 17 '15 at 19:13
  • a sentence containing "is not the real color of" would not match. I want the words to be exactly in that order. – PiccolMan Aug 17 '15 at 19:15
  • @AnandSKumar yes that's what I mean. – PiccolMan Aug 17 '15 at 19:16
  • Next question: is `Crisis the colour offer` a match? – Martijn Pieters Aug 17 '15 at 19:26
  • More questions: Do you want to remove only the *first* such match, or all matches? Should the list be altered *in place* or should a new list be produced? – Martijn Pieters Aug 17 '15 at 19:29
  • Please stop altering your requirements with each edit; now you suddenly want *two* lists filtered. You haven't really answered my questions either. – Martijn Pieters Aug 17 '15 at 19:33
  • Sorry about that, but I realized my question was too vague, and the solutions were not helping. – PiccolMan Aug 17 '15 at 19:36

4 Answers4

2

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']
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
2

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 (i.e. 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)
Vishal Rao
  • 872
  • 1
  • 7
  • 18
1
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.

f.rodrigues
  • 3,499
  • 6
  • 26
  • 62
0
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]
vks
  • 67,027
  • 10
  • 91
  • 124