简体   繁体   中英

Python - In and Not In List Syntax Error

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.

Something like this is probably what you want..

upper_bound = max(int(p) for p in price_list)
lower_bound = min(int(p) for p in price_list)
new_price_list = [(p+0.99) for p in range(lower_bound, upper_bound, 5) 
                         if (p+0.99) not in price_list]

This is an other way to achieve what you need:

price_list = [39.99, 74.99, 24.99, 49.99]

min_price = min(price_list)
max_price = max(price_list)

new_price_list = [min_price + 5.00]
for i in new_price_list:
    if i < max_price - 5.00:
        num = i+5.00
        num = round(num,2)
        new_price_list.append(num)
print new_price_list

Output:

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

Are you really guaranteed that the prices will all end in .99 ? If not, then your solution and the new proposal will both fail on actual data.

I suggest that you first convert the prices to cents:

pennies_list = [round(100*p) for p in price_list]

Now you can go about your usual business of finding the missing prices. Find the min_price and max_price values of pennies list, as polpak showed.

full_list = [p for p in range(min_price, max_price, 500) ]
original_set = set(pennies_list)
full_set = set(full_list)
new_set = full_set - original_set
new_price_list = list(new_set)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM