New to python set comprehensions

This is the place for queries that don't fit in any of the other categories.

New to python set comprehensions

Postby abhi1988srivastava » Sat Oct 26, 2013 4:15 am

Hi,

I was trying to learn set comprehensions and came up with a query:

Here r is a set of tuples.
Code: Select all
def e3():
    r3=set()
    return {(r3.add((u,v))(for u,w1 in r if w1==w2) for w2,v in r)}


How can I make this work? It shows error for first forloop. I wanted to demonstrate:

Code: Select all
def e3():
     r3=set()
     for w2,v in r:
           for u,w1 in r:
                    if w1==w2:
                        r3.add((u,v))
    return r3



Please help..

Thanks
Abhinav
abhi1988srivastava
 
Posts: 11
Joined: Mon Sep 09, 2013 5:54 pm

Re: New to python set comprehensions

Postby micseydel » Sat Oct 26, 2013 5:12 am

I believe what you would want is
Code: Select all
r3 = {(u, v) for (u, w1) in r for (w2, v) in r if w1 == w2}

However, your program runs in quadratic time complexity so you may want to seek a different solution if you have large inputs. It can be done in linear time.

Also, the time complexity is still poor, but here's a version that uses the same logic plus short circuting
Code: Select all
r3 = {(u, v) for (u, v) in r if any(u in pair or v in pair for pair in r)}

I believe that this version is slightly more readable as well.

For future reference, when you post a problem like this, it's a good idea to post sample input and outputs as well.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1495
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: New to python set comprehensions

Postby abhi1988srivastava » Sat Oct 26, 2013 7:38 am

Thanks a lot for your query suggestion..can you please explain how is it getting the output as desired?
is there any way or some links from where I can learn the python indepth right from the beginning since I have just started with python..

Thanks for your help again..

Regards

Abhinav
abhi1988srivastava
 
Posts: 11
Joined: Mon Sep 09, 2013 5:54 pm

Re: New to python set comprehensions

Postby micseydel » Sat Oct 26, 2013 7:47 am

You need to master regular loop and conditionals (which you seem to have), and then regular comprehensions, and then once you've done so, you'll need to apply reasonable thought toward whether a nested comprehension is a good idea (it's usually not; I'm leading slightly toward "no" in the example here).

Mostly though, you just need to understand regular comprehensions, which should be easy to find examples of, and then just remember that they're mostly backwards compared to how you'd write the loop regularly, and then with the if part at the end.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1495
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: New to python set comprehensions

Postby abhi1988srivastava » Sat Oct 26, 2013 8:01 am

I have been trying for comprehension but not getting the exact hold of it..I will keep trying :)

Also the query :

r3 = {(u, v) for (u, v) in r if any(u in pair or v in pair for pair in r)}

is actually

r3={(u,v) for (u,v) in r if any(u in (u,w1) in r or (w2,v) in r for <pair> in r)}

can you please confirm my understanding. and apologies for asking such stupid question and <pair> in red?

Thanks again
abhi1988srivastava
 
Posts: 11
Joined: Mon Sep 09, 2013 5:54 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests