python输入语法错误_Python-输入和不输入列表语法错误

I'm trying to construct a new list of floats from another existing list of floats. The expected contents of that first list are easier to identify by example:

price_list = [39.99, 74.99, 24.99, 49.99]

Expected, post function:

print new_price_list

>>[29.99, 34.99, 44.99, 54.99, 59.99, 64.99, 69.99]

The new list is derived by looking at the range of the existing list and, beginning with the minimum value in the existing list, appending the floats += 5.00 that aren't in the existing list. My initial attempt at a solution was:

price_list = [39.99, 74.99, 24.99, 49.99]

min_price = min(price_list)

new_price_list = []

while min_price < max(price_list):

if min_price not in price_list:

new_price_list.append(min_price)

min_price += 5.00

for price in new_price_list:

print price

>>29.99

>>34.99

>>39.99

>>44.99

>>49.99

>>54.99

>>59.99

>>64.99

>>69.99

And just for reference:

print new_price_list

>>[29.99, 34.989999999999995, 39.989999999999995, 44.989999999999995, 49.989999999999995, 54.989999999999995, 59.989999999999995, 64.99, 69.99]

In the meantime, I've identified what I think is an issue with how the min_price is being compared to the items in price_list. My awkward workaround solution is as follows. However, I am still curious if there is anyway to more efficiently accomplish this task, as I was seeking to do in my original guess at a solution, or perhaps even more using a list comprehension even with the min_price += 5.00?

price_list = [39.99, 74.99, 24.99, 49.99]

min_price = min(price_list)

new_price_list = []

while min_price < max(price_list):

if int(min_price) not in [int(price) for price in price_list]:

new_price_list.append(int(min_price))

min_price += 5.00

better_price_list = [price + 0.99 for price in new_price_list]

print better_price_list

[29.99, 34.99, 44.99, 54.99, 59.99, 64.99, 69.99]

Thanks very much for your help! Looking forward to getting to know this community better.

解决方案

To generate the values, the min and max and create a custom range generator:

mn = min(price_list)

mx = max(price_list)

def flt_rnge(start, stop, step):

start += step

while start < stop:

yield start

start += step

print(list(flt_rnge(mn, mx, 5)))

Which outputs the following which is not a syntax error, it is the repr output:

[29.99, 34.989999999999995, 39.989999999999995, 44.989999999999995, 49.989999999999995, 54.989999999999995, 59.989999999999995, 64.99, 69.99]

If you wanted value not already in your list you could use a set to store the prices already in your list but you are going to run into floating-point-arithmetic-issues when comparing the floats, in that case and always when dealing with money you should use the decimal module:

price_list = [39.99, 74.99, 24.99, 49.99]

mn = min(price_list)

mx = max(price_list)

from decimal import Decimal

def flt_rnge(start, stop, step, l):

st = map(str, l)

start,stop,step = Decimal(str(start)),Decimal(str(stop)),Decimal(str(step))

start += step

while start < stop:

if start not in st:

yield start

start += step

print(list(flt_rnge(mn, mx, 5, price_list)))

[Decimal('29.99'), Decimal('34.99'), Decimal('44.99'), Decimal('54.99'), Decimal('59.99'), Decimal('64.99'), Decimal('69.99')]

You are printing the output in your first part of the question so you see the nicely formatted output, when you print the list after you are seeing the repr which shows the float value stored is not actually equal to xx.99 so all your if x not in list.

When dealing with money, you should use the decimal module from the start.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值