使用正则表达式从2个python列表中删除某些元素

使用正则表达式从2个python列表中删除某些元素,python,Python,我需要一些关于python列表的帮助 file_data=["today is good","happiness is very nice", "i am hungry","i need to go to gym"] file_names=["_podcast_podcast_2","article_about_happiness","podcast_podcast_34","article about fitness"] 我需要删除与“播客”相关的元素。如果播客出现在文件名中,则删除该元素,

我需要一些关于python列表的帮助

file_data=["today is good","happiness is very nice", "i am hungry","i need to go to gym"]
file_names=["_podcast_podcast_2","article_about_happiness","podcast_podcast_34","article about fitness"]
我需要删除与“播客”相关的元素。如果播客出现在文件名中,则删除该元素,并删除文件数据的相应元素

所需输出:

file_data=["happiness is very nice", "i need to go to gym"]
file_names=["article_about_happiness","article about fitness"]

使用列表理解的简单单行程序:

file_names, file_data = [[f, file_data[i]] for i, f in enumerate(file_names) if "podcast" not in f]
输出:

['article about fitness', 'i need to go to gym']
['article_about_happiness', 'happiness is very nice']
['happiness is very nice', 'i need to go to gym']
['article_about_happiness', 'article about fitness']
我意识到我可能误读了最初的问题,并得到了错误的输出。以下是您问题中列出的输出结果的版本:

grouped = [[file_names[i], file_data[i]] for i,f in enumerate(file_names) if "podcast" not in file_names[i]]
file_names = [i[0] for i in grouped]
file_data = [i[1] for i in grouped]

print(file_data)
print(file_names)
输出:

['article about fitness', 'i need to go to gym']
['article_about_happiness', 'happiness is very nice']
['happiness is very nice', 'i need to go to gym']
['article_about_happiness', 'article about fitness']

使用列表理解的简单单行程序:

file_names, file_data = [[f, file_data[i]] for i, f in enumerate(file_names) if "podcast" not in f]
输出:

['article about fitness', 'i need to go to gym']
['article_about_happiness', 'happiness is very nice']
['happiness is very nice', 'i need to go to gym']
['article_about_happiness', 'article about fitness']
我意识到我可能误读了最初的问题,并得到了错误的输出。以下是您问题中列出的输出结果的版本:

grouped = [[file_names[i], file_data[i]] for i,f in enumerate(file_names) if "podcast" not in file_names[i]]
file_names = [i[0] for i in grouped]
file_data = [i[1] for i in grouped]

print(file_data)
print(file_names)
输出:

['article about fitness', 'i need to go to gym']
['article_about_happiness', 'happiness is very nice']
['happiness is very nice', 'i need to go to gym']
['article_about_happiness', 'article about fitness']
说明:

  • 我将两个列表转换为一个列表,其中每个元素都是一对(数据、名称)。这就是
    zip
    的工作原理
  • 我使用列表理解只考虑第二个项目中没有“播客”的对。在这里,正则表达式是一种过度使用。如果Python中的“字符串”中没有“子字符串”,则可以执行
    
    
  • 然后,我将其解压缩以获得两个单独的列表(使用
    zip(*list)
    )。我将它们分配给初始变量
  • 说明:

  • 我将两个列表转换为一个列表,其中每个元素都是一对(数据、名称)。这就是
    zip
    的工作原理
  • 我使用列表理解只考虑第二个项目中没有“播客”的对。在这里,正则表达式是一种过度使用。如果Python中的“字符串”中没有“子字符串”,则可以执行
    
    
  • 然后,我将其解压缩以获得两个单独的列表(使用
    zip(*list)
    )。我将它们分配给初始变量

  • 正则表达式是必需的吗?所以不是免费的编码服务。@jp_data_analysis。如果你对这两种方法都不熟悉的话,那么对折纸来说,差不多有一把大锤那么大。正则表达式是必需的吗?所以不是免费的编码服务。@jp\u data\u analysis。如果你对这两种方法都不熟悉的话,大约相当于折纸用的大锤。这很有效!!!。。。我不知道我的名字。我现在就要学了。谢谢@库马尔很乐意帮忙,如果这些答案中有任何一个回答了你的问题,看看@chrisz,你为什么要乞讨,告诉他们昨天他们已经做了两次了?@StefanPochmann根本不想乞讨,我说的是“如果这些答案中有任何一个”,而不仅仅是我的答案。我相信所有人都有优点。这起作用了!!!。。。我不知道我的名字。我现在就要学了。谢谢@库马尔很乐意帮忙,如果这些答案中有任何一个回答了你的问题,看看@chrisz,你为什么要乞讨,告诉他们昨天他们已经做了两次了?@StefanPochmann根本不想乞讨,我说的是“如果这些答案中有任何一个”,而不仅仅是我的答案。我相信大家都有优点。