Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » List compare is there something better than a double loop? [RSS Feed]

#1 Dec. 3, 2010 03:43:07

S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

List compare is there something better than a double loop?


Hi, I probably have a common problem and maybe i'm not thinking about
this correctly, but here it is.

I'm building a store that has a product_list (class ProductList:
store, name, description) that contains .products (class Product:
name, description, price etc...)

I also have a cart that is built out of (class CartItem: product,
user, qty).

when the store page loads with the products I want to acknowledge that
the item being shown is in the cart.

So when i get the cart i have a result of
and i have a product_list of

So the simpleton in me wants to do a double loop (cart/product_list)
and compare product == cartitem.product if so, do something.

But it seems there should be a better way to do that, and if not, do i
need to make a third list or can i put a transient field on product
like product.qty_in_cart to pass down to my page for testing?

Thanks
j

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 3, 2010 03:53:45

Aaron S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

List compare is there something better than a double loop?


>
>
> I'm building a store that has a product_list (class ProductList:
> store, name, description) that contains .products (class Product:
> name, description, price etc...)
>
>
If you're expecting them to be in the same order, you could do

all(product == cartitem.product for product, cartitem in
itertools.izip(cartitems, products))

This would be True if they're the same and false otherwise. If you want an
order insensitive comparison, you have:

set(products) == set(cartitem.product for cartitem in cartitems)

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 4, 2010 15:48:17

S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

List compare is there something better than a double loop?


very cool thanks, i knew it was a lack of api knowledge on my part.

On Dec 2, 10:53 pm, Aaron Sterling <aaronasterl...@gmail.com> wrote:
> > I'm building a store that has a product_list (class ProductList:
> > store, name, description) that contains .products (class Product:
> > name, description, price etc...)
>
> If you're expecting them to be in the same order, you could do
>
>      all(product == cartitem.product for product, cartitem in
> itertools.izip(cartitems, products))
>
> This would be True if they're the same and false otherwise. If you want an
> order insensitive  comparison, you have:
>
>     set(products) == set(cartitem.product for cartitem in cartitems)

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Dec. 4, 2010 16:10:07

bruno d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

List compare is there something better than a double loop?


On 3 déc, 04:42, Sector7B <joe.greenaw...@gmail.com> wrote:
> Hi, I probably have a common problem and maybe i'm not thinking about
> this correctly, but here it is.
>
> I'm building a store that has a product_list (class ProductList:
> store, name, description) that contains .products (class Product:
> name, description, price etc...)
>
> I also have a cart that is built out of (class CartItem: product,
> user, qty).
>
> when the store page loads with the products I want to acknowledge that
> the item being shown is in the cart.
>
> So when i get the cart i have a result of
> and i have a product_list of
>
> So the simpleton in me wants to do a double loop (cart/product_list)
> and compare product == cartitem.product if so, do something.

Simplest solution that has a chance to scale- set lookups are O(1):

cart_products = set(cartitem.product for cartitem in cart)
for product in product_list:
if product in cart_products:
do_something()


--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » List compare is there something better than a double loop? [RSS Feed]

Board footer

Moderator control

Enjoy the 19th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support