如何使列表中的每个对象都浮动

时间:2014-12-20 18:08:41

标签: python multiplying

我正在使用Python 2.当我尝试将列表中的对象相乘时,即使我尝试使用它来解决问题,它也会重复相同的事情两次:

map(float, prices)

我正在使用的代码是:

import urllib
from bs4 import BeautifulSoup

prices = []
htmlfile = urllib.urlopen("http://www.fifacoin.com/default/quick/listwithcategoryid?        category_id=6").read()
soup = BeautifulSoup(htmlfile)
for item in soup.find_all('tr', {'data-price': True}):
    prices.append(item['data-price'])

map(float, prices)
print prices[1] * 2

这段代码只输出价格2的值。我是Python的新手,所以它可能是显而易见的

2 个答案:

答案 0 :(得分:5)

map不会更改原始列表;它只是返回一个新列表。尝试:

prices = map(float, prices)

答案 1 :(得分:2)

您可以尝试列表理解:

answer = [float(i) for i in prices]

输出:

In [253]: prices
Out[253]: ['5', '1', '3', '8']

In [254]: [float(i) for i in prices]
Out[254]: [5.0, 1.0, 3.0, 8.0]

In [255]: prices
Out[255]: ['5', '1', '3', '8']

请注意原始列表保持不变