Python unicode列表的平均值
我有以下清单:Python unicode列表的平均值,python,python-2.7,list,Python,Python 2.7,List,我有以下清单: prices = [u'2.24', u'$2.24', u'$2.24', u'$2.24', u'$2.21'] 我希望获得列表的平均值,并尝试了以下操作: avgPrice = sum(prices) / float(len(prices)) 然而,由于某种原因,我得到了以下错误 TypeError: unsupported operand type(s) for +: 'int' and 'unicode' 我不明白为什么unicode会引起问题。您的价格是字符串,
prices = [u'2.24', u'$2.24', u'$2.24', u'$2.24', u'$2.21']
我希望获得列表的平均值,并尝试了以下操作:
avgPrice = sum(prices) / float(len(prices))
然而,由于某种原因,我得到了以下错误
TypeError: unsupported operand type(s) for +: 'int' and 'unicode'
我不明白为什么unicode会引起问题。您的价格是字符串,而不是数字。您应该首先去掉$signs,然后将它们转换为float。只有这样,你才能把它们加起来,再把总数除以
prices = [u'2.24', u'$2.24', u'$2.24', u'$2.24', u'$2.21']
prices = [float(price.replace('$', '')) for price in prices]
mean = sum(prices)/len(prices)
print(mean)
# 2.2340000000000004
你的价格是字符串,不是数字。您应该首先去掉$signs,然后将它们转换为float。只有这样,你才能把它们加起来,再把总数除以
prices = [u'2.24', u'$2.24', u'$2.24', u'$2.24', u'$2.21']
prices = [float(price.replace('$', '')) for price in prices]
mean = sum(prices)/len(prices)
print(mean)
# 2.2340000000000004
字母“u”代表什么?字母“u”代表什么?