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', 'C5': '300',
'C4': '349', 'CLD': '0', 'E5': '1148', 'C2': '329',
'MAP': '15', 'OIL': '167', 'HP': '19', 'E1': '1137',
'MARK': '', 'E3': '1163', 'TIME': '15:43:54',
'E2': '1169'}
From this dictionary I would like to create another dictionary calld
'egt') that has all of the keys that start with the letter 'E'. In
otherwords it should look like this:
egt = {'E6': '1182','E1': '1137','E4': '1157','E5': '1148',
'E2': '1169','E3': '1163'}
This should be pretty easy, but somehow with all my googling I've
not found a hint.
Thanks in advance
--
Frank Stutzman 19 1286
On Oct 26, 11:29 pm, Frank Stutzman <stutz...@skywagon.kjsl.com>
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', 'C5': '300',
'C4': '349', 'CLD': '0', 'E5': '1148', 'C2': '329',
'MAP': '15', 'OIL': '167', 'HP': '19', 'E1': '1137',
'MARK': '', 'E3': '1163', 'TIME': '15:43:54',
'E2': '1169'}
From this dictionary I would like to create another dictionary calld
'egt') that has all of the keys that start with the letter 'E'. In
otherwords it should look like this:
egt = {'E6': '1182','E1': '1137','E4': '1157','E5': '1148',
'E2': '1169','E3': '1163'}
This should be pretty easy, but somehow with all my googling I've
not found a hint.
Thanks in advance
--
Frank Stutzman
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]
print egt
{'E5': '1148', 'E4': '1157', 'E6': '1182', 'E1': '1137', 'E3': '1163',
'E2': '1169'}
-Edward Kozlowski
On Oct 26, 9:29 pm, Frank Stutzman <stutz...@skywagon.kjsl.comwrote:
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', 'C5': '300',
'C4': '349', 'CLD': '0', 'E5': '1148', 'C2': '329',
'MAP': '15', 'OIL': '167', 'HP': '19', 'E1': '1137',
'MARK': '', 'E3': '1163', 'TIME': '15:43:54',
'E2': '1169'}
From this dictionary I would like to create another dictionary calld
'egt') that has all of the keys that start with the letter 'E'. In
otherwords it should look like this:
egt = {'E6': '1182','E1': '1137','E4': '1157','E5': '1148',
'E2': '1169','E3': '1163'}
This should be pretty easy, but somehow with all my googling I've
not found a hint.
One possible solution (read list-comprehension if you not familiar
with it):
>>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', 'C5': '300',
.... 'C4': '349', 'CLD': '0', 'E5': '1148', 'C2': '329',
.... 'MAP': '15', 'OIL': '167', 'HP': '19', 'E1': '1137',
.... 'MARK': '', 'E3': '1163', 'TIME': '15:43:54',
.... 'E2': '1169'}
>>egt = dict([(k, record[k]) for k in record if k.startswith('E')]) egt
{'E5': '1148', 'E4': '1157', 'E6': '1182', 'E1': '1137', 'E3': '1163',
'E2': '1169'}
Karthik
>
Thanks in advance
--
Frank Stutzman
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:
egt = dict((key, record[key])
for key in record
if key.startswith('E'))
Regards,
Ryan Ginstrom
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', 'C5': '300',
'C4': '349', 'CLD': '0', 'E5': '1148', 'C2': '329',
'MAP': '15', 'OIL': '167', 'HP': '19', 'E1': '1137',
'MARK': '', 'E3': '1163', 'TIME': '15:43:54',
'E2': '1169'}
From this dictionary I would like to create another dictionary calld
'egt') that has all of the keys that start with the letter 'E'. In
otherwords it should look like this:
egt = {'E6': '1182','E1': '1137','E4': '1157','E5': '1148',
'E2': '1169','E3': '1163'}
This should work -
egt = dict([i for i in d.items() if i[0].startswith('E')])
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
On Oct 27, 8:02 am, Frank Millman <fr...@chagford.comwrote:
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 not be as efficient, as it retrieves the key and value for
*every* entry in 'record' before testing whether the key begins with
'E'.
If the dictionary is large, this may be a consideration.
Frank
On Oct 27, 1:16 am, Frank Millman <fr...@chagford.comwrote:
On Oct 27, 8:02 am, Frank Millman <fr...@chagford.comwrote:
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 not be as efficient, as it retrieves the key and value for
*every* entry in 'record' before testing whether the key begins with
'E'.
That can be fixed by using record.iteritems() instead of
record.items().
If the dictionary is large, this may be a consideration.
Frank
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': '311', 'C5': '300',
'C4': '349', 'CLD': '0', 'E5': '1148', 'C2': '329',
'MAP': '15', 'OIL': '167', 'HP': '19', 'E1': '1137',
'MARK': '', 'E3': '1163', 'TIME': '15:43:54',
'E2': '1169'}
From this dictionary I would like to create another dictionary calld
'egt') that has all of the keys that start with the letter 'E'. In
otherwords it should look like this:
egt = {'E6': '1182','E1': '1137','E4': '1157','E5': '1148',
'E2': '1169','E3': '1163'}
This should be pretty easy,
Indeed !-)
but somehow with all my googling I've
not found a hint.
egt = dict((k, v) for k, v in record.iteritems() if k.startswith('E'))
My take too :-)
dict(item for item in record.iteritems() if item[0][0] == 'E')
Bye,
bearophile
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,
Marc 'BlackJack' Rintsch
On Sat, 27 Oct 2007 04:29:51 +0000, 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', 'C5': '300', 'C4': '349',
'CLD': '0', 'E5': '1148', 'C2': '329', 'MAP': '15', 'OIL':
'167', 'HP': '19', 'E1': '1137', 'MARK': '', 'E3': '1163',
'TIME': '15:43:54', 'E2': '1169'}
From this dictionary I would like to create another dictionary calld
'egt') that has all of the keys that start with the letter 'E'. In
otherwords it should look like this:
egt = {'E6': '1182','E1': '1137','E4': '1157','E5': '1148',
'E2': '1169','E3': '1163'}
With my tongue firmly in cheek, I present the following obfuscated
solution:
eval("{" + reduce(lambda x,y: y+', '+x, [mo.group(1) for mo in __import__
('re').finditer(r"('E.*?'\s*:\s*'.*?'),?", str(record))], "") + "}")
The above should be a single line.
eval(), reduce(), lambda, string concatenation in the least efficient way
possible, regexes... could it get any worse than this?
--
Steven.
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[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.
Bruno Desthuilliers wrote:
egt = dict((k, v) for k, v in record.iteritems() if k.startswith('E'))
This is what I was looking for. I thought I had seen something simular
to this in one of the tutorials I had read, but couldn't seem to find it.
Steven D'Aprano:
eval("{" + reduce(lambda x,y: y+', '+x, [mo.group(1) for mo in __import__
('re').finditer(r"('E.*?'\s*:\s*'.*?'),?", str(record))], "") + "}")
Ah! Now this is one solution I can get my teeth into. If its not obvious,
I'm a recovering perl programmer.
Thanks to all
--
Frank Stutzman
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, because probably that's not a
normal condition. Sometimes less safe is more safe :-)
With few tests I have seen that (using Psyco) this is probably the
faster solution (and it's quite readable by a newbe too):
record2 = {}
for k in record:
if k[0] == 'E':
record2[k] = record[k]
Misteriously with Psyco it becomes faster than even the equivalent D
language solution (keys starting with "E" are about 5% of the total)
that is statically typed and generally very fast:
string[string] record2;
foreach(k, v; record)
if (k[0] == 'E')
record2[k] = v;
Bye,
bearophile
>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: forget about the comprehension-based ones. It *is*
Pythonic to have the code readable to a neophyte; there is no price to
win for shortness or cuteness.
>egt = dict((k, v) for k, v in record.iteritems() if k.startswith('E'))
This is what I was looking for. I thought I had seen something simular
to this in one of the tutorials I had read, but couldn't seem to find it.
And you consider this readable? I find it way too complex.
Ah! Now this is one solution I can get my teeth into. If its not obvious,
I'm a recovering perl programmer.
Ok, I now see why you can appreciate the comprehension-based one :-)
Again, much of Python's strength comes from it being readable. Simple
is better than complex, and sparse is better than dense, and readability
counts (import this).
Regards,
Martin
On Oct 27, 8:58 am, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
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`.
A string slice is safe and faster though: if s[:1] == 'E'.
George
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.
My recommendation: forget about the comprehension-based ones. It *is*
Pythonic to have the code readable to a neophyte; there is no price to
win for shortness or cuteness.
While I wholefully agree that readability is not something to
undervalue, I would not bash out list-comps (or generator-expressions,
as is the case below) based solutions for such a simple use-case.
Readability is not dumbness neither, and even if list-comps/generator
expression may be something new for most Python newbies, they *are* by
now the canonical, idiomatic Python way in this situation.
>
>>>egt = dict((k, v) for k, v in record.iteritems() if k.startswith('E'))
This is what I was looking for. I thought I had seen something simular to this in one of the tutorials I had read, but couldn't seem to find it.
And you consider this readable? I find it way too complex.
As far as I'm concerned, I do find this solution much more readable than
it's more imperative counterpart.
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 !
On Oct 27, 6:42 am, Karthik Gurusamy <kar1...@gmail.comwrote:
On Oct 26, 9:29 pm, Frank Stutzman <stutz...@skywagon.kjsl.comwrote:
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', 'C5': '300',
'C4': '349', 'CLD': '0', 'E5': '1148', 'C2': '329',
'MAP': '15', 'OIL': '167', 'HP': '19', 'E1': '1137',
'MARK': '', 'E3': '1163', 'TIME': '15:43:54',
'E2': '1169'}
From this dictionary I would like to create another dictionary calld
'egt') that has all of the keys that start with the letter 'E'. In
otherwords it should look like this:
egt = {'E6': '1182','E1': '1137','E4': '1157','E5': '1148',
'E2': '1169','E3': '1163'}
This should be pretty easy, but somehow with all my googling I've
not found a hint.
One possible solution (read list-comprehension if you not familiar
with it):
>>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', 'C5': '300',
... 'C4': '349', 'CLD': '0', 'E5': '1148', 'C2': '329',
... 'MAP': '15', 'OIL': '167', 'HP': '19', 'E1': '1137',
... 'MARK': '', 'E3': '1163', 'TIME': '15:43:54',
... 'E2': '1169'}>>egt =dict([(k,record[k]) for k inrecordif k.startswith('E')])
>egt
{'E5': '1148', 'E4': '1157', 'E6': '1182', 'E1': '1137', 'E3': '1163',
'E2': '1169'}
Karthik
Thanks in advance
--
Frank Stutzman
Hallo,
a functional and concise way.
egt= dict( filter( lambda item: item[0][0] == "E" ,
record.iteritems() ))
Rainer r.*****@science-computing.de a écrit :
On Oct 27, 6:42 am, Karthik Gurusamy <kar1...@gmail.comwrote:
>>On Oct 26, 9:29 pm, Frank Stutzman <stutz...@skywagon.kjsl.comwrote:
>>>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', 'C5': '300', 'C4': '349', 'CLD': '0', 'E5': '1148', 'C2': '329', 'MAP': '15', 'OIL': '167', 'HP': '19', 'E1': '1137', 'MARK': '', 'E3': '1163', 'TIME': '15:43:54', 'E2': '1169'}
>>>From this dictionary I would like to create another dictionary calld 'egt') that has all of the keys that start with the letter 'E'. In otherwords it should look like this:
>>>egt = {'E6': '1182','E1': '1137','E4': '1157','E5': '1148', 'E2': '1169','E3': '1163'}
>>>This should be pretty easy, but somehow with all my googling I've not found a hint.
One possible solution (read list-comprehension if you not familiar with it):
>>>>>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', 'C5': '300', ... 'C4': '349', 'CLD': '0', 'E5': '1148', 'C2': '329', ... 'MAP': '15', 'OIL': '167', 'HP': '19', 'E1': '1137', ... 'MARK': '', 'E3': '1163', 'TIME': '15:43:54', ... 'E2': '1169'}>>egt =dict([(k,record[k]) for k inrecordif k.startswith('E')])
>>>>>egt
{'E5': '1148', 'E4': '1157', 'E6': '1182', 'E1': '1137', 'E3': '1163', 'E2': '1169'}
Karthik
>>>Thanks in advance
>>>-- Frank Stutzman
Hallo,
a functional and concise
and not necessarily efficient
way.
egt= dict( filter( lambda item: item[0][0] == "E" ,
record.iteritems() ))
List comprehensions and generator expressions are just as 'functional'
as lambdas (list comps comes from Haskell FWIW). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Fuzzyman |
last post by:
There have been a couple of config file 'systems' announced recently,
that focus on building more powerful and complex configuration files.
ConfigObj is a module to enable you to much more *simply*...
|
by: Raymond Hettinger |
last post by:
I would like to get everyone's thoughts on two new dictionary methods:
def count(self, value, qty=1):
try:
self += qty
except KeyError:
self = qty
def appendlist(self, key, *values):
try:
|
by: Christoph Zwerschke |
last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist
only since Python 2.4.
Anyway, I was thinking about whether it would be possible and desirable
to change the old behavior in...
|
by: Brian P |
last post by:
I want to expose a property of Dictionary<string, MyAbstractClass>.
I tried to do it this way:
private Dictionary<string, MyChildClass> _dictionary;
public Dictionary<string,...
|
by: Rune B |
last post by:
Hi Group
I was considering using a Generic Dictionary<> as a value container inside
my business objects, for the reason of keeping track of fields changed or
added and so on.
- But how...
|
by: NullQwerty |
last post by:
Hi folks,
I have a Dictionary which contains a string key and an object value. I
want the object value to point to a property in my class and I want it
to be by reference, so that later on I...
|
by: =?utf-8?B?TWFjaWVqIEJsaXppxYRza2k=?= |
last post by:
Hi Pythonistas!
I've got a question about storing tuples in a dictionary. First, a
small test case which creates a list of dictionaries:
import time
list_of_dicts =
keys =
prev_clk =...
|
by: not_a_commie |
last post by:
given a Dictionary<object, objectand an object that exactly
replicates the Equals and GetHashCode of the original object used as
the dictionary key, how do I retrieve the original key that was used?
|
by: Simon Strobl |
last post by:
Hello,
I tried to load a 6.8G large dictionary on a server that has 128G of
memory. I got a memory error. I used Python 2.5.2. How can I load my
data?
SImon
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
| |