Re: Not this one the other one, from a dictionary

2009-09-22 Thread Sion Arrowsmith
Vlastimil Brom  vlastimil.b...@gmail.com wrote:
 other_key = (set(data_dict.iterkeys()) - set([not_wanted_key,])).pop()

other_key = set(data_dict.iterkeys()).difference([not_wanted]).pop()
saves you the construction of an unnecessary set instance. At the
cost of a bit more verbosity, you can get rid of a second set:

key_set = set(data_dict.iterkeys())
key_set.difference_update([not_wanted_key])
other_key = key_set.pop()

although the loss of clarity compared to the one liner can't be
worth the miniscule benefit in this case.

-- 
\S

   under construction

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Not this one the other one, from a dictionary

2009-09-22 Thread Jean Daniel
Building on the answers of the others, a simple one liner, no side
effect, not the fastest I guess:

 d={'a': 'bob', 'b': 'stu'}
 set( d.keys() ).difference( [ 'a' ] ).pop()
'b'

Note the square brackets for the parameter of difference(). 'The
string 'a' and the list [ 'a' ] are both iterable but really are
different.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Not this one the other one, from a dictionary

2009-09-22 Thread Vlastimil Brom
Thanks for the elaboration;
in retrospect, given the simple requirement, that there are only two
dict keys, one of which is know and the other to be determined, maybe
just the direct dict methods are appropriate, e.g.

d = {'a': 'bob', 'b': 'stu'}
d_copy = dict(d)
d_copy.pop(a)
'bob'
d_copy.popitem()
('b', 'stu')

vbr
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Not this one the other one, from a dictionary

2009-09-18 Thread Tim Chase
Learning my way around list comprehension a bit.   I wonder if  
someone has a better way to solve this issue.  I have a two element  
dictionary, and I know one of the keys but not the other, and I want  
to look up the other one.


Several ways occur to me.  Of the various solutions I played 
with, this was my favorite (requires Python2.4+ for generator 
expressions):


  d = {'a': 'alice', 'b':'bob'}
  known = 'a'
  other_key, other_value = (
(k,v)
for k,v
in d.iteritems()
if k != known
).next()

If you just want one or the other, you can simplify that a bit:

  other_key = (k for k in d.iterkeys() if k != known).next()
  other_key = (k for k in d if k != known).next()

or

  other_value = (v for k,v in d.iteritems() if k != known).next()

If you're using pre-2.4, you might tweak the above to something like

  other_key, other_value = [
(k,v)
for k,v
in d.iteritems()
if k != known
][0]
  other_key = [k for k in d if k != known)[0]
  other_value = [k for k in d.iteritems if k != known][0]

Hope this helps,

-tkc





--
http://mail.python.org/mailman/listinfo/python-list


Re: Not this one the other one, from a dictionary

2009-09-18 Thread Ishwor
Ross
Hi.

 So I have this dictionary:

 aDict = {'a': 'bob', 'b': 'stu'}

Yes.

 I know that the dictionary contains two keys/value pairs,  but I don't know
 the values nor that the keys will be 'a' and 'b'.   I finally get one of the
 keys passed to me as variable BigOne. e.g.:

 BigOne = a

Right. aDict[BigOne] will give you - 'bob' which may/maynot be what
you want by the looks of it, you want aDict[BigOne].

 aDict.keys()
['a', 'b']
 aDict.values()
['bob', 'stu']

keys() / values() return list so you do not have to worry about
explicitly getting list.

 The other key, call it  littleOne remains unknown.  It might be b but
 could be c, x, etc...   I later need to access both values...

 I have something that works, with list comprehension - but wonder if there's
 a more brief/elegant way to get there:

If you know the dictionary name in this case aDict (which you don't
mention you know or not), you can just traverse it using easily as
such:
for k in aDict:
 print k, aDict[k];

 [[i,a[i]] for i in aDict]
[['a', 'bob'], ['b', 'stu']]
 [[i,a[i]] for i in aDict][0]
['a', 'stu']
 [[i,a[i]] for i in aDict][0][0]
'a'
 [[i,a[i]] for i in aDict][0][1]
'bob'


-- 
Regards,
Ishwor Gurung
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Not this one the other one, from a dictionary

2009-09-18 Thread Ross
Thanks Tim (and Ishwor) for the suggestions, those are structures  
that somewhat new to me - looks good!  I'll play with those.At  
this rate I may soon almost know what I'm doing.


Rgds
Ross.


On 18-Sep-09, at 1:19 PM, Tim Chase wrote:

Learning my way around list comprehension a bit.   I wonder if   
someone has a better way to solve this issue.  I have a two  
element  dictionary, and I know one of the keys but not the other,  
and I want  to look up the other one.


Several ways occur to me.  Of the various solutions I played with,  
this was my favorite (requires Python2.4+ for generator expressions):


  d = {'a': 'alice', 'b':'bob'}
  known = 'a'
  other_key, other_value = (
(k,v)
for k,v
in d.iteritems()
if k != known
).next()

If you just want one or the other, you can simplify that a bit:

  other_key = (k for k in d.iterkeys() if k != known).next()
  other_key = (k for k in d if k != known).next()

or

  other_value = (v for k,v in d.iteritems() if k != known).next()

If you're using pre-2.4, you might tweak the above to something like

  other_key, other_value = [
(k,v)
for k,v
in d.iteritems()
if k != known
][0]
  other_key = [k for k in d if k != known)[0]
  other_value = [k for k in d.iteritems if k != known][0]

Hope this helps,

-tkc







--
http://mail.python.org/mailman/listinfo/python-list


Re: Not this one the other one, from a dictionary

2009-09-18 Thread Vlastimil Brom
2009/9/18 Ross ros...@gmail.com:

 Learning my way around list comprehension a bit.   I wonder if someone has a
 better way to solve this issue.  I have a two element dictionary, and I know
 one of the keys but not the other, and I want to look up the other one.

 So I have this dictionary:

 aDict = {'a': 'bob', 'b': 'stu'}

 I know that the dictionary contains two keys/value pairs,  but I don't know
 the values nor that the keys will be 'a' and 'b'.   I finally get one of the
 keys passed to me as variable BigOne. e.g.:

 BigOne = a

 The other key, call it  littleOne remains unknown.  It might be b but
 could be c, x, etc...   I later need to access both values...

 I have something that works, with list comprehension - but wonder if there's
 a more brief/elegant way to get there:

 BigValu = aDict[BigOne]
 temp =  [ thing for thing in aDict if thing != BigOne ]
 LittleValu = aDict[ temp[0] ]

 Any thoughts?

 - Ross.

 --
 http://mail.python.org/mailman/listinfo/python-list


Hi,
not a list comprehension, but another useful approach to get the
complement of (a) given item(s) might be the set operations.
(As you are working with dict keys, the requirements for set elements
are automatically fulfilled.

 data_dict = {'a': 'bob', 'b': 'stu'}
 not_wanted_key = a
 other_key = (set(data_dict.iterkeys()) - set([not_wanted_key,])).pop()
 other_key
'b'
 data_dict[other_key]
'stu'


vbr
-- 
http://mail.python.org/mailman/listinfo/python-list