Re: simple question on dictionary usage

2007-11-04 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit : > On Oct 27, 6:42 am, Karthik Gurusamy <[EMAIL PROTECTED]> wrote: > >>On Oct 26, 9:29 pm, Frank Stutzman <[EMAIL PROTECTED]> wrote: >> >> >> >> >>>My apologies in advance, I'm new to python >> >>>Say, I have a dictionary that looks like this: >> >>>record={'BAT': '14.4'

Re: simple question on dictionary usage

2007-11-03 Thread r . grimm
On Oct 27, 6:42 am, Karthik Gurusamy <[EMAIL PROTECTED]> wrote: > On Oct 26, 9:29 pm, Frank Stutzman <[EMAIL PROTECTED]> wrote: > > > > > My apologies in advance, I'm new to python > > > Say, I have a dictionary that looks like this: > > >record={'BAT': '14.4', 'USD': '24', 'DIF': '45', 'OAT': '16'

Re: simple question on dictionary usage

2007-10-29 Thread Bruno Desthuilliers
Steven D'Aprano a écrit : (snip) > eval("{" + reduce(lambda x,y: y+', '+x, [mo.group(1) for mo in __import__ > ('re').finditer(r"('E.*?'\s*:\s*'.*?'),?", str(record))], "") + "}") Maman ! Steven, you choose the wrong language. You definitively want Perl ! -- http://mail.python.org/mailman/listin

Re: simple question on dictionary usage

2007-10-29 Thread Bruno Desthuilliers
Martin v. Löwis a écrit : >>>egt = {} >>>for key in record: >>> if key.startswith('E'): >>> egt[key] = record[key] >> >>I actually had come up with something like this, but thought it wasn't >>quite as pythonish as it should be. It is certainly much more readable >>to a neophyte to python.

Re: simple question on dictionary usage

2007-10-28 Thread George Sakkis
On Oct 27, 8:58 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > On Sat, 27 Oct 2007 05:23:30 -0700, bearophileHUGS wrote: > > My take too :-) > > > dict(item for item in record.iteritems() if item[0][0] == 'E') > > ``s.startswith('E')`` is a little safer than ``s[0] == 'E'`` as the former

Re: simple question on dictionary usage

2007-10-28 Thread Martin v. Löwis
>> egt = {} >> for key in record: >>if key.startswith('E'): >>egt[key] = record[key] > > I actually had come up with something like this, but thought it wasn't > quite as pythonish as it should be. It is certainly much more readable > to a neophyte to python. My recommendation: forge

Re: simple question on dictionary usage

2007-10-28 Thread bearophileHUGS
Marc 'BlackJack' Rintsch>``s.startswith('E')`` is a little safer than ``s[0] == 'E'`` as the former returns `False` if `s` is empty while the latter raises an `IndexError`.< Thank you. In this problem if there is an empty key in the record dict then maybe it's better to raise an IndexError, becaus

Re: simple question on dictionary usage

2007-10-27 Thread Frank Stutzman
Wow, what a great group! Lots of useful and kind suggestions to what I was sure was a fairly dumb question. A few comments on some selected suggestions (but I appreciate all of them) Edward Kozlowski wrote: > egt = {} > for key in record: >if key.startswith('E'): >egt[key] = record[

Re: simple question on dictionary usage

2007-10-27 Thread Steven D'Aprano
On Sat, 27 Oct 2007 04:29:51 +, Frank Stutzman wrote: > My apologies in advance, I'm new to python > > Say, I have a dictionary that looks like this: > > record={'BAT': '14.4', 'USD': '24', 'DIF': '45', 'OAT': '16', > 'FF': '3.9', 'C3': '343', 'E4': '1157', 'C1': '339', 'E6': >

Re: simple question on dictionary usage

2007-10-27 Thread Marc 'BlackJack' Rintsch
On Sat, 27 Oct 2007 05:23:30 -0700, bearophileHUGS wrote: > My take too :-) > > dict(item for item in record.iteritems() if item[0][0] == 'E') ``s.startswith('E')`` is a little safer than ``s[0] == 'E'`` as the former returns `False` if `s` is empty while the latter raises an `IndexError`. Ciao

Re: simple question on dictionary usage

2007-10-27 Thread bearophileHUGS
My take too :-) dict(item for item in record.iteritems() if item[0][0] == 'E') Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: simple question on dictionary usage

2007-10-27 Thread Bruno Desthuilliers
Frank Stutzman a écrit : > My apologies in advance, I'm new to python > > Say, I have a dictionary that looks like this: > > record={'BAT': '14.4', 'USD': '24', 'DIF': '45', 'OAT': '16', > 'FF': '3.9', 'C3': '343', 'E4': '1157', 'C1': '339', > 'E6': '1182', 'RPM': '996', 'C6': '31

Re: simple question on dictionary usage

2007-10-27 Thread Dustan
On Oct 27, 1:16 am, Frank Millman <[EMAIL PROTECTED]> wrote: > On Oct 27, 8:02 am, Frank Millman <[EMAIL PROTECTED]> wrote: > > > > This should work - > > > > egt = dict([i for i in d.items() if i[0].startswith('E')]) > > > Of course I meant record.items(), not d.items(). Sorry. > > > Frank > > On

Re: simple question on dictionary usage

2007-10-26 Thread Frank Millman
On Oct 27, 8:02 am, Frank Millman <[EMAIL PROTECTED]> wrote: > > This should work - > > > egt = dict([i for i in d.items() if i[0].startswith('E')]) > > Of course I meant record.items(), not d.items(). Sorry. > > Frank On reflection, although my solution is a bit shorter than some others, it may n

Re: simple question on dictionary usage

2007-10-26 Thread Frank Millman
> > This should work - > > egt = dict([i for i in d.items() if i[0].startswith('E')]) > Of course I meant record.items(), not d.items(). Sorry. Frank -- http://mail.python.org/mailman/listinfo/python-list

Re: simple question on dictionary usage

2007-10-26 Thread Frank Millman
Frank Stutzman wrote: > My apologies in advance, I'm new to python > > Say, I have a dictionary that looks like this: > > record={'BAT': '14.4', 'USD': '24', 'DIF': '45', 'OAT': '16', > 'FF': '3.9', 'C3': '343', 'E4': '1157', 'C1': '339', > 'E6': '1182', 'RPM': '996', 'C6': '311',

RE: simple question on dictionary usage

2007-10-26 Thread Ryan Ginstrom
> On Behalf Of Edward Kozlowski > I think this should do the trick. There's probably something > more concise than this, but I can't think of it at the moment. > > egt = {} > for key in record: > if key.startswith('E'): > egt[key] = record[key] Not much more concise, but another way

Re: simple question on dictionary usage

2007-10-26 Thread Karthik Gurusamy
On Oct 26, 9:29 pm, Frank Stutzman <[EMAIL PROTECTED]> wrote: > My apologies in advance, I'm new to python > > Say, I have a dictionary that looks like this: > > record={'BAT': '14.4', 'USD': '24', 'DIF': '45', 'OAT': '16', > 'FF': '3.9', 'C3': '343', 'E4': '1157', 'C1': '339', > 'E

Re: simple question on dictionary usage

2007-10-26 Thread Edward Kozlowski
On Oct 26, 11:29 pm, Frank Stutzman <[EMAIL PROTECTED]> wrote: > My apologies in advance, I'm new to python > > Say, I have a dictionary that looks like this: > > record={'BAT': '14.4', 'USD': '24', 'DIF': '45', 'OAT': '16', > 'FF': '3.9', 'C3': '343', 'E4': '1157', 'C1': '339', > '