Is a always ordered (as it is in the example)?<div><br></div><div><font class="Apple-style-span" face="'courier new', monospace">>>> from bisect import bisect_left</font></div><div><font class="Apple-style-span" face="'courier new', monospace">>>> a = [['cat',2],['cat',5],['cat',9],['dog',6]]</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">>>> i = bisect_left(a, ['cat'])</font></div><div><font class="Apple-style-span" face="'courier new', monospace">>>> if i < len(a):</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">... print a[i]</font></div><div><font class="Apple-style-span" face="'courier new', monospace">... </font></div><div><font class="Apple-style-span" face="'courier new', monospace">['cat', 2]</font></div>
<div><br></div><div>Do you absolutely want a dictionary as a result?</div><div><br></div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div><font class="Apple-style-span" face="'courier new', monospace">>>> d = {}</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">>>> [d.setdefault(*pair) for pair in a]</font></div><div><font class="Apple-style-span" face="'courier new', monospace">[2, 2, 2, 6]</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">>>> d</font></div><div><font class="Apple-style-span" face="'courier new', monospace">{'dog': 6, 'cat': 2}</font></div>
<div><br></div><div> - Jeremy</div><div><br><div class="gmail_quote">On Mon, Jun 21, 2010 at 7:22 PM, Vikram <span dir="ltr"><<a href="mailto:kpguy@rediffmail.com">kpguy@rediffmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Suppose i have this list:<br>
<br>
>>> a = [['cat',2],['cat',5],['cat',9],['dog',6]]<br>
>>> a<br>
[['cat', 2], ['cat', 5], ['cat', 9], ['dog', 6]]<br>
<br>
Now, there is a nice way to obtain the value 9.<br>
<br>
>>> z = dict(a)<br>
>>> z<br>
{'dog': 6, 'cat': 9}<br>
<br>
Is there any elegant way to extract the value 2? (Value corresponding to the first occurence of 'cat' in the 2-D list).<br>
<br>
Thanks,<br><font color="#888888">
Vikram<br><table border="0" width="644" height="57" cellspacing="0" cellpadding="0" style="font-family:Verdana;font-size:11px;line-height:15px"><tbody><tr><td><a href="http://sigads.rediff.com/RealMedia/ads/click_nx.ads/www.rediffmail.com/signatureline.htm@Middle?" target="_blank"><img></a></td>
</tr></tbody></table></font><br>_______________________________________________<br>
Baypiggies mailing list<br>
<a href="mailto:Baypiggies@python.org">Baypiggies@python.org</a><br>
To change your subscription options or unsubscribe:<br>
<a href="http://mail.python.org/mailman/listinfo/baypiggies" target="_blank">http://mail.python.org/mailman/listinfo/baypiggies</a><br></blockquote></div><br></div>