comprehension problem

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

comprehension problem

Postby abhi1988srivastava » Mon Sep 09, 2013 6:04 pm

Hi everyone,

I am new to python and trying to use tuple with sets through comprehension

U={'u1','u2','u3','u4'}
R={'r1','r2','r3','r4'}
UR=(('u1','r1'),('u1','r2'),('u2','r3'),('u3','r3'),('u4','r3'),('u4','r4'))

Code: Select all
def assignedRoles(u):
   for i in U:
      for x,y in u:
         if i==x:
            print (x,y)


this gives me user with roles correctly but then i tried to do the same through comprehension like

Code: Select all
def assignedRoles(u):
   m={y for x,y in u if x=={k for k in U}}
   print (m)


Is my use of comprehension wrong since i am getting empty set as output?

I am calling the function as:

assignedRoles(UR)

Please help!!!!

Thanks

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

Re: comprehension problem

Postby Somelauw » Mon Sep 09, 2013 7:43 pm

Try:
Code: Select all
def assignedRoles(u):
    print "\n".join(str((x, y))
            for i in U
            for(x, y) in u if i == x)
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 72
Joined: Tue Feb 12, 2013 8:30 pm


Return to General Coding Help

Who is online

Users browsing this forum: Larz60+, mdovey and 2 guests