从列表中删除项目 - Python

时间:2015-06-09 18:44:03

标签: python list

我正在从当地监狱网站上抓取数据。我试图从列表中删除除收费之外的所有元素。我希望所有的法规,债券等都消失了。

以下是我的尝试:

charges = [[], ['13A-12-214.1'], ["ECSO (ETOWAH COUNTY SHERIFF\\'S OFFICE)"], ['SALVIA MISD POSS'], [''], ['M'], ['$1000.00'], [], [], ['13A-10-41'], ["ECSO (ETOWAH COUNTY SHERIFF\\'S OFFICE)"], ['RESISTING ARREST'], [''], ['M'], ['$1000.00'], [], [], ['32.5A.88'], ["ECSO (ETOWAH COUNTY SHERIFF\\'S OFFICE)"], ['IMPROPER LANE USAGE'], [''], ['U'], ['$500.00'], [], [], [''], [''], ['DET FOR COMM CORR'], [''], ['U'], ['$0.00'], [], [], ['<tr>\\r\\n\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t        <td class="SearchHeader" colspan="2">']]

    for string in charges:
        if string == arrestedBy:
            charges.remove(string)
        elif string.isalpha() == False:
            charges.remove(string)
        elif len(string) < 2:
            charges.remove(string)

if charges[-1] == '<tr>\\r\\n\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t\\t        <td class="SearchHeader" colspan="2">':
    charges.remove(charges[-1])

charges = filter(None, charges)

charges = str(charges)

我得到的是:

"ECSO (ETOWAH COUNTY SHERIFF\\S OFFICE)", $1000.00, "ECSO (ETOWAH COUNTY SHERIFF\\S OFFICE)", $1000.00, "ECSO (ETOWAH COUNTY SHERIFF\\S OFFICE)", $500.00, $0.00

我想要的是:

“SALVIA ​​MISD POSS,抵抗逮捕,不正确的路线使用,确定COMM CORR”

1 个答案:

答案 0 :(得分:0)

如果您在抓取时无法限制收费,请考虑使用python list comprehension,而不是迭代列表并删除元素(这是不可取的)。

例如,如果你定义一些函数charges = [i for i in charges if is_charge(i)] ,它包含你定义电荷的逻辑并返回一个布尔值:

`<ul>
    <li class="option">
        <span id="text">111</span>
        <span id="image" class="hide">IMAGE</span>
    </li>
    <li class="option">
        <span id="text">222</span>
        <span id="image" class="hide">IMAGE</span>
    </li>
</ul>`


.option:hover .option > #image
{
display:block;
border-bottom:1px solid black;
}
.option:hover: .option > #text
{
display:none;
}
.hide
{
display:none;
}