简体   繁体   English

从Python中的列表中删除字符

[英]Strip characters from list in Python

so I'm trying to print out a list of VMWare templates that are sitting in our lab. 所以我正在尝试打印出我们实验室中的VMWare模板列表。 I want the output to look like this: 我希望输出看起来像这样:

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

Whereas the current output looks more like this: 而目前的输出看起来更像这样:

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

Here's my code: 这是我的代码:

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)

I know this is probably really amateurish code but I'm not really a python guy. 我知道这可能是非常业余的代码,但我不是一个真正的蟒蛇人。 Is there some kind of regex or something I could use to clean this up a bit? 是否有某种正则表达式或我可以用来清理它的东西?

If it is always the same format just split once on whitespace and extract the second element: 如果它始终是相同的格式,只需在空格上拆分一次并提取第二个元素:

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

You can probably also do the split before you put the data in a list but without seeing the actual input it is impossible to say for sure. 您可以在将数据放入列表之前进行拆分,但是如果没有看到实际输入,则无法确定。

A simple regex can do it, gives some flexibility. 一个简单的正则表达式可以做到这一点,提供一些灵活性。
Can either just grab capture group 1 into an array, 可以将捕获组1抓取到阵列中,
or just global find and replace with capture group 1. 或者只是全局查找并替换为捕获组1。
If you don't know all possible characters, just replace 如果您不知道所有可能的字符,只需替换即可
[az\\d.-]+ with \\S+ [az\\d.-]+ with \\S+

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

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

The function you're looking for is map : https://docs.python.org/2/library/functions.html#map 你正在寻找的功能是maphttps//docs.python.org/2/library/functions.html#map

What you'd want to do is call map after filter , like so: 您想要做的是在filter后调用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)

In the snippet above, filter works the same way as what you had before, only I rewrote the call to combine your three functions into one. 在上面的代码片段中, filter工作方式与之前的相同,只是我重写了调用以将三个函数合并为一个。 The map function takes the filtered list and calls clean_template on each element, returning the results as a list. map函数获取已过滤的列表并在每个元素上调用clean_template ,并将结果作为列表返回。

clean_template returns the second half of the string (the part that you're interested in), assuming there is no whitespace in the string other than what you identified. clean_template返回字符串的后半部分(您感兴趣的部分),假设除了您标识的字符串之外,字符串中没有空格。

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

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