Python - In And Not In List Syntax Error
Solution 1:
To generate the values, the min and max and create a custom range generator:
mn = min(price_list)
mx = max(price_list)
defflt_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)
fromdecimal 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 startnotin st:
yield startstart+= 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.
Solution 2:
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]
Solution 3:
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]
Solution 4:
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)
Post a Comment for "Python - In And Not In List Syntax Error"