在Python中从列表中删除字符

在Python中从列表中删除字符,python,regex,Python,Regex,因此,我试图打印出我们实验室中的VMWare模板列表。我希望输出结果如下所示: vagrant-ubuntu12.04-small vagrant-centos6.6-small vagrant-ubuntu12.04 vagrant-centos6.6 ['[datastore2] vagrant-ubuntu12.04-small'] ['[datastore2] vagrant-centos6.6-small'] ['[datastore1] vagrant-centos6.6'] ['

因此,我试图打印出我们实验室中的VMWare模板列表。我希望输出结果如下所示:

vagrant-ubuntu12.04-small
vagrant-centos6.6-small
vagrant-ubuntu12.04
vagrant-centos6.6
['[datastore2] vagrant-ubuntu12.04-small']
['[datastore2] vagrant-centos6.6-small']
['[datastore1] vagrant-centos6.6']
['[datastore1] vagrant-ubuntu12.04']
而电流输出看起来更像这样:

vagrant-ubuntu12.04-small
vagrant-centos6.6-small
vagrant-ubuntu12.04
vagrant-centos6.6
['[datastore2] vagrant-ubuntu12.04-small']
['[datastore2] vagrant-centos6.6-small']
['[datastore1] vagrant-centos6.6']
['[datastore1] vagrant-ubuntu12.04']
这是我的密码:

from pysphere import VIServer
from pprint import pprint

VSPHERE = VIServer()

VSPHERE.connect('helike.labs.sdelements.com',
                'xxxxxxxxx',
                'xxxxxxxxx')

VMLIST = VSPHERE.get_registered_vms()


def is_template(string):
    """ Is it a template? """
    if string.find(".vmtx") == -1:
        return False
    else:
        return True


def is_vagrant_template(string):
    """ Is it a Vagrant Template? """
    if string.find("vagrant") == -1:
        return False
    else:
        return True


def is_proper_template(string):
    """ filter out extraneous templates """
    if string.find("sde") == -1:
        return True
    else:
        return False

temp1 = filter(is_template, VMLIST)
temp2 = filter(is_vagrant_template, temp1)
temp3 = filter(is_proper_template, temp2)

for item in temp3:
    relist = item.split('/')[:1]
    pprint(relist)

我知道这可能是非常业余的代码,但我不是一个真正的python爱好者。是否有某种正则表达式或我可以用来稍微整理一下的东西?

如果它总是相同的格式,只需在空白处拆分一次并提取第二个元素:

data = [['[datastore2] vagrant-ubuntu12.04-small'],
['[datastore2] vagrant-centos6.6-small'],
['[datastore1] vagrant-centos6.6'],
['[datastore1] vagrant-ubuntu12.04']]


for sub in data:
    print(sub[0].split(None,1)[1])

vagrant-ubuntu12.04-small
vagrant-centos6.6-small
vagrant-centos6.6
vagrant-ubuntu12.04

您可能也可以在将数据放入列表之前进行拆分,但在没有看到实际输入的情况下,无法确定这一点。

一个简单的正则表达式可以做到这一点,提供了一定的灵活性。
可以将捕获组1捕获到一个数组中,
或者只是全局查找并替换为捕获组1。
如果您不知道所有可能的字符,只需替换
[a-z\d.-]+
\S+

(?mi)^\['\[\\]*\]\h+([a-z\d.-]+)\h*'\]

 (?mi)                         # Modes: Multi-line, No-Case
 ^                             # BOL
 \[' \[ [^\]]* \]
 \h+ 
 ( [a-z\d.-]+ )                # (1)
 \h* 
 '\]

您要查找的函数是
map

您要做的是在
过滤器
之后调用
映射
,如下所示:

def is_proper_vagrant_template(string):
    """ Is it a proper Vagrant template? """
    return ".vmtx" in string and "vagrant" in string and "sde" not in string

def clean_template(string):
    """ Return the second half of the string, assuming whitespace as a separator """
    return string.split()[1]

temp1 = filter(is_proper_vagrant_template, VMLIST)
clean = map(clean_template, temp1)
在上面的代码片段中,
filter
的工作方式与您之前的工作方式相同,只是我重写了该调用以将三个函数合并为一个。
map
函数获取过滤后的列表,并对每个元素调用
clean_template
,将结果作为列表返回


clean_template
返回字符串的后半部分(您感兴趣的部分),假设字符串中除了您标识的内容外没有空格。

您的输入看起来像什么?太棒了,大部分情况下都是这样,但输出中仍然有['']字符,如何删除最后几个字符?如果您确实确定要删除这些字符(并且它们不是某个地方的错误造成的),可以在代码末尾添加以下行:
filter(None,clean)