如果行不从lis中的项目开始

2024-10-01 09:30:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道如何从一个列表中提取那些与另一个列表中的某些项目不同的项目。在

我想做一些类似的东西:

list_results = ['CONisotig124', '214124', '2151235', '235235', 'PLEisotig1235', 'PLEisotig2354', '12512515', 'CONisotig1325', '21352']

identifier_list=['CON','VEN','PLE']


for item in list_results:
  if not item.startswith(     "some ID from the identifier_list"     ):
      print item

那么,我该怎么说:

^{pr2}$

Tags: 项目in列表foritemconresultslist
2条回答

^{}可以使用字符串的元组来测试:

prefix can also be a tuple of prefixes to look for.

将此与列表理解结合使用:

identifier_list = ('CON', 'VEN', 'PLE')  # tuple, not list

[elem for elem in list_results if not elem.startswith(identifier_list)]

演示:

^{2}$

很直截了当,你几乎明白了:

for item in list_results:
  bad_prefix = False
  for id in identifier_list:
    if item.startswith(id):
      bad_prefix = True
      break

  if not bad_prefix:
    print item

相关问题 更多 >