Reputation: 6693
I have a problem.
I have an product.address
like this 'UK some city some road'
And I want to compare this address,if it is start with UK or US or AS or GE
I will save product.dstore_id = 2
,otherwise, save product.dstore_id = 1
My code is like this:
area = [UK,US,AS,GE]
for i in area:
if i in product.address:
product.dstore_id = 2
else:
product.dstore_id = 1
But I found this has a big problem,it will compare all the i
and save the wrong number
So how can I do to reach it . Please guide me a bit Thank you
Upvotes: 1
Views: 106
Reputation: 117856
You could use any
. This will return True
if any of the items in your area
list are present in the product.address
. You then no longer need the for
loop.
if any(address.startswith(i) for i in area):
product.dstore_id = 2
else:
product.dstore_id = 1
For example
area = ['UK','US','AS','GE']
address = 'UK some city some road'
>>> any(addres.startswith(i) for i in area)
True
Upvotes: 4