Python 列表附加条件
在这种特定情况下,我如何制作一个适当的if条件,只在列表中添加一个价格值,例如53以下的价格值Python 列表附加条件,python,list,conditional,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:
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)
但是
print a
在for循环之外
显然只打印一个值,因为它只执行了一次搜索,而不是在所有的选项中执行循环。假设您的正则表达式工作正常,这样做可能会:
for price in a:
if int(price)<=53:
price_list.append(price)
offers_list2.append(price)
适用于a中的价格:
如果是int(price),我认为询问者想要<53
而不是价格应该是浮动值,而不是int。对于货币金额浮动
有多安全?参见PEP 327。可能是一份关于靓汤的工作。
for price in a:
if int(price)<=53:
price_list.append(price)
offers_list2.append(price)