[Tutor] Program gets stuck after a creating a list from dictinary items!

Ali Torkamani torkamani at gmail.com
Fri Jul 6 19:21:27 CEST 2012


I'm using Python 2.7.
By stuck I mean, does not pass that specific line (but no errors).

Like I said FD is a list of dictionaries. and sCommonFeatures are a subset
of key's that have value in all of the dictionaries.



On Fri, Jul 6, 2012 at 1:04 PM, Prasad, Ramit <ramit.prasad at jpmorgan.com>wrote:

> > > I could resolve it by defining a small function:
> > >
> > > def getValue(mydict,keys):
> > >     A=[];
> > >     for i in keys:
> > >         A=A+[mydict[i]]
> > >     return A
> > >
> > > and then calling it: D=getValue(prog,sCommonFeatures);
> > > (instead of D=[prog[key1] for key1 in list(sCommonFeatures)];)
> > >
> > > but I'm still surprised why the latter one didn't work!
> >
> > It would be more efficient to do the following
> >
> > def getValue(mydict, keys):
> >     A=[]
> >     for i in keys:
> >         A.append( mydict[i] )
> >     return A
>
> Less efficiently, but maybe more useful if you need to check
> against sOtherFeatures you can do the following.
>
> D = [value for key, value in prog.items() if key in sOtherFeatures
>           or key in sCommonFeatures] # Python 3.x syntax
>
>
> Ramit
>
>
> Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
> 712 Main Street | Houston, TX 77002
> work phone: 713 - 216 - 5423
>
> --
> This email is confidential and subject to important disclaimers and
> conditions including on offers for the purchase or sale of
> securities, accuracy and completeness of information, viruses,
> confidentiality, legal privilege, and legal entity disclaimers,
> available at http://www.jpmorgan.com/pages/disclosures/email.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120706/3e927c8d/attachment-0001.html>


More information about the Tutor mailing list