如何删除列表中具有特定单词排序的项目

时间:2015-08-17 19:12:33

标签: python string list

说我有清单

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

我想检查"是否是"的颜色。是mylist索引中的单词排序。如果它是我想要从mylist和其他列表中删除该索引。

更具体地说,我希望最终结果是:

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

我在想这样的事情:

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

但是,此代码不起作用。

4 个答案:

答案 0 :(得分:2)

如果您想要完全匹配,请使用带有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)])

输出:

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

mylist[:]将意味着你改变原始列表,使用单词边界意味着is the colours等等。不会匹配,这可能是也可能不是所期望的行为。

如果要获取包含子字符串的字符串的索引,请使用enumerate保留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)])

输出:

[2]

如果您只想要第一场比赛,可能会有多个:

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

如果要同时从两个列表中删除zip,请检查子字符串匹配,如果匹配则删除:

 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)

输出:

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

答案 1 :(得分:2)

我猜你正试图查看字符串“是否是列表中存在的任何字符串的一部分的颜色”,如果是这样,你想删除该列表元素。唯一的方法是遍历列表(列表中的项目),然后使用 关键字检查您要搜索的子字符串用于列表元素内部。一种简单的方法是创建一个新列表,如果满足条件(即列表元素不包含您要搜索的子字符串),请将列表元素复制到新列表中。如果满足条件,您可以输入继续来跳过它。

编辑:看看你想如何修改2个列表,具体取决于你可以执行以下操作的条件匹配

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)

答案 2 :(得分:1)

CREATE VIEW dbo.VIEW_NAME AS
SELECT a.[no]
    ,a.NAME
    ,a.Sex
    ,a.DBO
    ,a.CaseNo
    ,a.SeqNum
    ,MIN(a.StartDate) StartDate
    ,MAX(b.Admin_1) Admin_1
    ,MAX(b.Admin_2) Admin_2
    ,MAX(b.Admin_3) Admin_3
    ,MAX(c.DisDate) DisDate
    ,MAX(c.DisRea) DisRea
FROM dbo.mem_information AS a
INNER JOIN dbo.auth_information AS b ON b.caseno = a.caseno
    AND b.seqnum = a.seqnum
    AND b.StartDate = a.StartDate
LEFT JOIN dbo.discharge_information AS c ON c.caseno = b.caseno
    AND c.seqnum = b.seqnum
GROUP BY  a.[no]
    ,a.NAME
    ,a.Sex
    ,a.DBO
    ,a.CaseNo
    ,a.SeqNum;

这是查找和删除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 的第一个实例的类似方法。

答案 3 :(得分:0)

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

您可以在此使用re

对于包含字符串

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

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

otherlist = ["1", "2", "3"]

中删除

你可以做到

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