列出附加条件

时间:2014-08-05 12:11:42

标签: python list conditional

在这种特定情况下,我如何制作一个合适的if条件,仅将例如低于53的价格值附加到列表中?

offers_list = re.findall("<div class=\"list\"(.*?)</div>", http_response_body, re.DOTALL) # Find list of offers in HTTP Response Body
price_list = []
offers_list2 = []

for i in offers_list:   # loop in the list of offers to look for the specific price values
    a = re.findall("\"price\"=(.*?)\"", i)  # Find specific price value within in each offer
    print a
    price_list.append(a) # Append to list only if the price is lower than X amount
    offers_list2.append(a)

以上代码输出:

[u'47.00']
[u'49.00']
[u'49.00']
[u'50.00']
[u'50.00']
[u'50.00']
[u'50.00']
[u'51.50']
[u'52.50']
[u'53.00']
[...]

然而print a之外的for loop只显示一个值,因为它只通过所有商品只进行了一次搜索而不是循环。

1 个答案:

答案 0 :(得分:2)

假设你的正则表达式正常工作,可能会这样:

for price  in a:
    if int(price)<=53:
        price_list.append(price)
        offers_list2.append(price)

此外,DO NOT PARSE HTML WITH REGEX