简体   繁体   English

Python从列表中删除项目

[英]Python Remove Item from list

I'm really new to Python, only working on it, as another guy in my team left and need to get some work over the line, so apologies if this is a stupid question. 我是Python新手,只是在努力工作,因为团队中的另一个人离开了,需要在线上工作,所以如果这是一个愚蠢的问题,请道歉。

I need to remove items from a list (they'll contain a certain phrase) and have googled and tried a few ways but it doesn't seem to be working. 我需要从列表中删除项目(它们将包含某个短语)并使用Google搜索并尝试了一些方法,但它似乎不起作用。

What I've currently got is, 我目前得到的是,

def get_and_count_card_files(date):
    # Retrieve the DC and SD card files
    dc_files = dc_card_files.get_dc_files(dc_start_location, date)
    sd_files = sd_card_files.get_sd_files(sd_start_location)
    print(dc_files)

if '*NoCover*' in dc_files:
    dc_files.remove('*NoCover*')

Dc_files being the list (bunch of file names) and I need to remove anything that has NoCover in the file name. Dc_files是列表(文件名串),我需要删除文件名中包含NoCover的任何内容。 It does exist as the print function shows me it does. 它确实存在,因为打印功能告诉我它。

Anyone any idea what I'm doing wrong 任何人都知道我做错了什么

在此输入图像描述

With this piece of code, the * won't do a glob or regular expression in these contexts: 使用这段代码, *将不会在这些上下文中执行glob或正则表达式:

if '*NoCover*' in dc_files:
    dc_files.remove('*NoCover*')

so it would be better to say: 所以最好说:

if 'NoCover' in dc_files:

However, we then have to find the right entry to remove in the list, without using wildcards. 但是,我们必须在列表中找到要删除的正确条目,而不使用通配符。

For a small list, you could iterate over it. 对于一个小列表,您可以迭代它。 A list comprehension would do: 列表理解会做:

new_dc_files = [dc for dc in dc_files if 'NoCover' not in dc]

It's a compact way of saying: 这是一种紧凑的说法:

new_dc_files = list()
for dc in dc_files:
    if 'NoCover' in dc:
         continue
    new_dc_files.append(dc)

if '*NoCover*' in dc_files: will look for an element in the list dc_files that matches, explicitly to ' NoCover '. if '*NoCover*' in dc_files:将在列表dc_files中查找匹配的元素,显式为' NoCover '。

What you can do, is 你能做什么,是

# Iterate over the list backwards, this will stop you from skipping elements
# you want to check in the next iteration if you remove something
for index, dc_file in enumerate(dc_files[::-1]):
    if 'NoCover' in dc_file:
        dc_files.pop(index)

Alternatively, you can look at using regular expressions if you have more complex pattern matching requirements. 或者,如果您有更复杂的模式匹配要求,则可以查看使用正则表达式。

You could use a Python list comprehension to filter out the unwanted entries: 您可以使用Python列表解析来过滤掉不需要的条目:

def get_and_count_card_files(date):
    # Retrieve the DC and SD card files
    dc_files = dc_card_files.get_dc_files(dc_start_location, date)
    sd_files = sd_card_files.get_sd_files(sd_start_location)

    print(dc_files)
    dc_files = [file for file in dc_files if 'NoCover' not in file]

You are looking for Python's list comprehension : 您正在寻找Python的列表理解

dc_files = [el for el in dc_files if 'NoCover' not in el]

So what this basically does is filter all the elements from dc_files that do not contain the 'NoCover' string. 那么,这通常做的就是过滤从该元素dc_files不包含'NoCover'字符串。

Note, that if 'NoCover' not in el is executed for each element of the list, and the in operator for a string simply checks exactly what you wanted to do, ie whether 'NoCover' is a substring of el . 注意, if 'NoCover' not in el列表中的每个元素都执行if 'NoCover' not in el ,并且字符串的in运算符只是确切地检查你想要做什么,即'NoCover'是否是el的子串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM