I learned new things today and I thank you all for your responses.
Please consider yourself thanked individually.
Dino
On 1/20/2023 10:29 AM, Dino wrote:
let's say I have this list of nested dicts:
--
https://mail.python.org/mailman/listinfo/python-list
in origListOfDescriptors]
I believe that from Python 3.9 onwards this can be written more
concisely as:
listOfDescriptors = [
{ (L := list(D.items())[0])[1] } | {'value' : L[0] }
for D in origListOfDescriptors] # untested
Best wishes
Rob Cliffe
I actually did i
On Fri, 20 Jan 2023 at 17:30, Dino wrote:
>
> let's say I have this list of nested dicts:
>
> [
>{ "some_key": {'a':1, 'b':2}},
>{ "some_other_key": {'a':3, 'b':4}}
> ]
>
> I need to turn this into:
>
> [
>{ "value": "some_key", 'a':1, 'b':2},
>{ "value": "some_other_key", 'a':3, '
On 2023-01-20, Dino wrote:
>
> let's say I have this list of nested dicts:
>
> [
>{ "some_key": {'a':1, 'b':2}},
>{ "some_other_key": {'a':3, 'b':4}}
> ]
>
> I need to turn this into:
>
> [
>{ "value": "some_key", 'a':1, 'b':2},
>{ "value": "some_other_key", 'a':3, 'b':4}
> ]
[{"v
hopefully you get the spirit.
"value": cn,
"a": cd[cn]["a"],
"b": cd[cn]["b"]
Anyway, the key point (ooops, a pun) is if there's a more elegant way to
do this (i.e. get a reference to the unique key in a dict() when
On 1/20/23 07:29, Dino wrote:
let's say I have this list of nested dicts:
[
{ "some_key": {'a':1, 'b':2}},
{ "some_other_key": {'a':3, 'b':4}}
]
I need to turn this into:
[
{ "value": "some_key", 'a':1, 'b':2},
{ "value": "some_other_key", 'a':3, 'b':4}
]
This doesn't look like
,
{ "value": "some_other_key", 'a':3, 'b':4}
]
I actually did it with:
listOfDescriptors = list()
for cd in origListOfDescriptors:
cn = list(cd.keys())[0] # There must be a better way than this!
listOfDescriptors.append({
"value":
On 2013-04-23 09:30, Tim Chase wrote:
> > But a csv.DictReader might still be more efficient. I never
> > tested. This is the only place I've used this "optimization".
> > It's fast enough. ;)
>
> I believe the csv module does all the work at c-level, rather than
> as pure Python, so it should be
> But a csv.DictReader might still be more efficient.
Depends on what efficiency you care about. The DictReader class is
implemented in Python, and builds a dict for every row. It will never
be more efficient CPU-wise than instantiating the csv.reader type
directly and only doing what you need.
On 2013-04-23 13:36, Neil Cerutti wrote:
> On 2013-04-22, Colin J. Williams wrote:
> > Since I'm only interested in one or two columns, the simpler
> > approach is probably better.
>
> Here's a sketch of how one of my projects handles that situation.
> I think the index variables are invaluable d
On 23 April 2013 14:36, Neil Cerutti wrote:
> On 2013-04-22, Colin J. Williams wrote:
>> Since I'm only interested in one or two columns, the simpler
>> approach is probably better.
>
> Here's a sketch of how one of my projects handles that situation.
> I think the index variables are invaluable
On 2013-04-22, Colin J. Williams wrote:
> Since I'm only interested in one or two columns, the simpler
> approach is probably better.
Here's a sketch of how one of my projects handles that situation.
I think the index variables are invaluable documentation, and
make it a bit more robust. (Python
On 22/04/2013 10:42 AM, Neil Cerutti wrote:
On 2013-04-21, Colin J. Williams wrote:
On 20/04/2013 9:07 PM, Terry Jan Reedy wrote:
On 4/20/2013 8:34 PM, Tim Chase wrote:
In 2.x, the csv.reader() class (and csv.DictReader() class) offered
a .next() method that is absent in 3.x
In Py 3, .next
On 2013-04-21, Colin J. Williams wrote:
> On 20/04/2013 9:07 PM, Terry Jan Reedy wrote:
>> On 4/20/2013 8:34 PM, Tim Chase wrote:
>>> In 2.x, the csv.reader() class (and csv.DictReader() class) offered
>>> a .next() method that is absent in 3.x
>>
>> In Py 3, .next was renamed to .__next__ for *al
On 21 April 2013 14:15, Colin J. Williams wrote:
> In the end, I used:
>
> inData= csv.reader(inFile)
>
> def main():
>
> if ver == '2':
> headerLine= inData.next()
> else:
> headerLine= inData.__next__()
> ...
> for item in inData:
> assert len(dataStore) =
On 21/04/2013 9:43 AM, Peter Otten wrote:
Colin J. Williams wrote:
I was seeking some code that would be acceptable to both Python 2.7 and
3.3.
In the end, I used:
inData= csv.reader(inFile)
def main():
if ver == '2':
headerLine= inData.next()
else:
headerLine
On 21/04/2013 9:39 AM, Jussi Piitulainen wrote:
Colin J. Williams writes:
...
It is not usual to have a name with preceding and following
udserscores,imn user code.
Presumably, there is a rationale for the change from csv.reader.next
to csv.reader.__next__.
...
I think the user code is suppos
Colin J. Williams wrote:
> I was seeking some code that would be acceptable to both Python 2.7 and
> 3.3.
>
> In the end, I used:
>
> inData= csv.reader(inFile)
>
> def main():
> if ver == '2':
> headerLine= inData.next()
> else:
> headerLine= inData.__next__()
>
Colin J. Williams writes:
...
> It is not usual to have a name with preceding and following
> udserscores,imn user code.
>
> Presumably, there is a rationale for the change from csv.reader.next
> to csv.reader.__next__.
...
I think the user code is supposed to be next(csv.reader). For example,
cu
On 20/04/2013 9:07 PM, Terry Jan Reedy wrote:
On 4/20/2013 8:34 PM, Tim Chase wrote:
In 2.x, the csv.reader() class (and csv.DictReader() class) offered
a .next() method that is absent in 3.x
In Py 3, .next was renamed to .__next__ for *all* iterators. The
intention is that one iterate with fo
On 4/20/2013 8:34 PM, Tim Chase wrote:
In 2.x, the csv.reader() class (and csv.DictReader() class) offered
a .next() method that is absent in 3.x
In Py 3, .next was renamed to .__next__ for *all* iterators. The
intention is that one iterate with for item in iterable or use builtin
functions i
On 2013-04-21 00:06, Steven D'Aprano wrote:
> On Sat, 20 Apr 2013 19:46:07 -0400, Colin J. Williams wrote:
>
> > Below is part of a script which shows the changes made to permit
> > the script to run on either Python 2.7 or Python 3.2.
> >
> > I was surprised to see that the CSV next method is no
On Sat, 20 Apr 2013 19:46:07 -0400, Colin J. Williams wrote:
> Below is part of a script which shows the changes made to permit the
> script to run on either Python 2.7 or Python 3.2.
>
> I was surprised to see that the CSV next method is no longer available.
This makes no sense. What's "the CSV
On Sat, Apr 20, 2013 at 4:46 PM, Colin J. Williams wrote:
> Below is part of a script which shows the changes made to permit the script
> to run on either Python 2.7 or Python 3.2.
>
> I was surprised to see that the CSV next method is no longer available.
>
> Suggestions welcome.
> if ver ==
Below is part of a script which shows the changes made to permit the
script to run on either Python 2.7 or Python 3.2.
I was surprised to see that the CSV next method is no longer available.
Suggestions welcome.
Colin W.
def main():
global inData, inFile
if ver == '2':
headerL
25 matches
Mail list logo