On 15/09/10 15:31, Hs Hs wrote:
Dear Steven,
Thanks for your help.

however I have a question,

using:

for key, value in xdic.items():
    if 1 in value and 2 in value or 3 in value:
        print key


also print keys that have values such as [1,2,3].

In cases where there is [1,2,3] and [1,2] also reported.

How can I extract those keys that have values only [1,2] and [1,3] exclusively.


>>> xdic = {75796988: [1, 2, 3], 75797478: [1, 2, 3], 75797887:[1,2], 75797987:[3,1]}
>>> for key, value in xdic.items():
...     if 1 in value and 2 in value or 3 in value:
...             print key
...
75797987
75796988
75797478
75797887


Here all 4 keys appear. Instead I want to get only 75797887:[1,2] and 75797987:[3,1]
how can I force this.
If you just have a few specific cases you can use "in" to check whether the values you are interested in appear in a specified collection, ie:

>>> xdic = {75796988: [1, 2, 3], 75797478: [1, 2, 3], 75797887:[1,2], 75797987:[3,1]}
>>> for key, value in xdic.items():
...     if value in ([1,2], [2,1], [1,3], [3,1]):
...             print key
...
75797987
75797887

HTH


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to