Python 从多个列表中删除字符

Python 从多个列表中删除字符,python,Python,我有两份清单: PB = ['1','2','3', '10'] LB = ['12', '5', '21'] 我想从列表中删除所有带有“1”的内容。我可以通过以下方式一次删除一个项目: for notAWinner in [PB, LB]: notAWinner.remove('1') 删除不适用于多个参数。如何使用一个命令删除1、10、12和21?使用以下命令: no_ones = [i for i in PB + LB if '1' not in i] 您可以用所需的任何列

我有两份清单:

PB = ['1','2','3', '10']
LB = ['12', '5', '21']
我想从列表中删除所有带有“1”的内容。我可以通过以下方式一次删除一个项目:

for notAWinner in [PB, LB]:
    notAWinner.remove('1') 
删除不适用于多个参数。如何使用一个命令删除1、10、12和21?

使用以下命令:

no_ones = [i for i in PB + LB if '1' not in i]
您可以用所需的任何列表替换代码的
PB+LB
部分。 它将返回所有没有1的项目

print(no_ones)
这里有一个函数,只需传入您想要的任何列表,它将返回它,而不带1

def remove_ones(l):
    return [i for i in l if '1' not in i]
使用以下命令:

no_ones = [i for i in PB + LB if '1' not in i]
您可以用所需的任何列表替换代码的
PB+LB
部分。 它将返回所有没有1的项目

print(no_ones)
这里有一个函数,只需传入您想要的任何列表,它将返回它,而不带1

def remove_ones(l):
    return [i for i in l if '1' not in i]

您可以
筛选出不需要的项目:

PB = list(filter(lambda x: '1' not in x, PB))
LB = list(filter(lambda x: '1' not in x, LB))

您可以
筛选出不需要的项目:

PB = list(filter(lambda x: '1' not in x, PB))
LB = list(filter(lambda x: '1' not in x, LB))

如果您正在寻找伪代码(y)解决方案

results = []

for i in PB + LB:
  if '1' not in i:
    results.append(i)

print(results)

如果您正在寻找伪代码(y)解决方案

results = []

for i in PB + LB:
  if '1' not in i:
    results.append(i)

print(results)

如果您可以更新您的
PB
LB
以引用新列表,那么使用filter+lambda或列表理解将很容易

PB = list(filter(lambda s: '1' not in s, PB))   #filter

LB = [s for s in LB if '1' not in s]  # List comprehension
但是,如果您需要在现有列表中适当地删除它,这似乎有点棘手。一种方法是使用切片语法替换现有列表的内容:

# similar to what you are doing in your question
for my_list in (PB, LB):
    # my_list  = [s for s in my_list if '1' not in s]  
    # The above will not affect the original PB and LB

    my_list[:] = [s for s in my_list if '1' not in s]
    # The above will replace the content of original PB and LB

如果您可以更新您的
PB
LB
以引用新列表,那么使用filter+lambda或列表理解将很容易

PB = list(filter(lambda s: '1' not in s, PB))   #filter

LB = [s for s in LB if '1' not in s]  # List comprehension
但是,如果您需要在现有列表中适当地删除它,这似乎有点棘手。一种方法是使用切片语法替换现有列表的内容:

# similar to what you are doing in your question
for my_list in (PB, LB):
    # my_list  = [s for s in my_list if '1' not in s]  
    # The above will not affect the original PB and LB

    my_list[:] = [s for s in my_list if '1' not in s]
    # The above will replace the content of original PB and LB

我能做的最好是两行:

PB = ['1','2','3', '10']
LB = ['12', '5', '21']
notAWinner = ['1', '10', '12', '21']

for ls in LB, PB:
    [i if i not in notAWinner else ls.remove(i) for i in ls.copy()]

我能做的最好是两行:

PB = ['1','2','3', '10']
LB = ['12', '5', '21']
notAWinner = ['1', '10', '12', '21']

for ls in LB, PB:
    [i if i not in notAWinner else ls.remove(i) for i in ls.copy()]

notAWinner是我在代码中使用的变量名。
your_list_无_垃圾=[I for I in your_list in[1,10,12,21]]
我会按原样键入吗?这将一次在多个列表上工作?是否有方法搜索项目中是否有1,如果是,则将其删除?是。如果您想循环多个列表,只需扩展该列表即可<代码>[[i for i in your_list if not i in[1,10,12,21]]对于[PB,LB]
中的_list,这将生成一个列表,该列表已全部删除这些数字。notavenuer是我在代码中使用的变量名。
没有垃圾的_list=[i for i in your_list if not i in[1,10,12,21]
我会按原样键入吗?这将一次在多个列表上工作?是否有方法搜索项目中是否有1,如果是,则将其删除?是。如果您想循环多个列表,只需扩展该列表即可<代码>[[i代表您的_列表中的i,如果[1,10,12,21]中没有i,则[i代表您的_列表中的[PB,LB]]
,这将生成一个列表,该列表已全部删除这些数字。我复制并粘贴了它,但它没有删除其中带有1的项目。您必须不打印任何项目。这是一个没有onesI副本的新列表,它按原样粘贴,并且没有删除其中带有1的项目。您必须不打印任何项目。这是一个新的列表,没有