Python-输入和不输入列表语法错误 [英] Python - In and Not In List Syntax Error

查看:110
本文介绍了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]

预期的发布功能:

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

通过查看现有列表的范围,并从现有列表中的最小值开始,添加不存在于现有列表中的浮点数+ = 5.00,可以得出新列表.我最初的解决方案是:

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

仅供参考:

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

同时,我已经确定了我认为将min_price与price_list中的项目进行比较的问题.我尴尬的解决方法如下.但是,我仍然好奇是否有办法更有效地完成此任务,就像我在最初的猜测中试图解决的那样,或者甚至使用min_price + = 5.00的列表理解甚至更多?

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)))

输出以下内容不是语法错误,而是repr输出:

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]

如果您想要的价值不在您的列表中,则可以使用set存储已经在列表中的价格,但是您将遇到十进制模块:

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')]

您正在问题的第一部分中打印输出,因此您可以看到格式良好的输出,在看到表示已存储的浮点值实际上不等于xx.99的repr后打印列表时,所有您的if x not in list.

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.

这篇关于Python-输入和不输入列表语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆