RE: Namedtuples problem

2017-02-23 Thread Deborah Swanson
Gregory Ewing wrote, on February 23, 2017 9:07 PM > > Deborah Swanson wrote: > > I've run into this kind of problem with namedtuples before, > trying to > > access field values with variable names, like: > > > > label = 'Location' > > records.label > > If you need to access an attribute whose

Re: Namedtuples problem

2017-02-23 Thread Gregory Ewing
Deborah Swanson wrote: I've run into this kind of problem with namedtuples before, trying to access field values with variable names, like: label = 'Location' records.label If you need to access an attribute whose name is computed at run time, you want getattr() and setattr(): value =

RE: Namedtuples problem

2017-02-23 Thread Deborah Swanson
Erik wrote, on February 23, 2017 4:03 PM > > On 23/02/17 23:00, Deborah Swanson wrote: > >> It looks to me like you are indexing into a single-element > list that > >> you are creating using the literal list syntax in the middle of > >> the expression. > > > > Actually, group is essentially a

RE: Namedtuples problem

2017-02-23 Thread Deborah Swanson
Irv Kalb wrote, on February 23, 2017 3:51 PM > > > On Feb 23, 2017, at 3:00 PM, Deborah Swanson > > > wrote: > > > > The weirdness is that > > > > group[0][4] > > > > gets the right answer, but > > > >

RE: Namedtuples problem

2017-02-23 Thread Deborah Swanson
9 AM > >> To: pyt...@deborahswanson.net; python-list@python.org > >> Subject: Re: Namedtuples problem > >> > >> > >> Hi, > >> > >> On 23/02/17 09:38, Deborah Swanson wrote: > >> > group[[idx][records_idx[label]]] > >&g

RE: Namedtuples problem

2017-02-23 Thread Deborah Swanson
To: pyt...@deborahswanson.net; python-list@python.org > >> Subject: Re: Namedtuples problem > >> > >> > >> Hi, > >> > >> On 23/02/17 09:38, Deborah Swanson wrote: > >> > group[[idx][records_idx[label]]] > >> > gets an

Re: Namedtuples problem

2017-02-23 Thread Irv Kalb
> On Feb 23, 2017, at 3:00 PM, Deborah Swanson > wrote: > > The weirdness is that > > group[0][4] > > gets the right answer, but > > group[[idx][records_idx[label]]], > where idx = 0 and records_idx[label]] = 4 If that's the

Re: Namedtuples problem

2017-02-23 Thread Ben Bacarisse
"Deborah Swanson" <pyt...@deborahswanson.net> writes: >> -Original Message- >> From: Erik [mailto:pyt...@lucidity.plus.com] >> Sent: Thursday, February 23, 2017 2:09 AM >> To: pyt...@deborahswanson.net; python-list@python.org >>

Re: Namedtuples problem

2017-02-23 Thread MRAB
On 2017-02-23 23:00, Deborah Swanson wrote: -Original Message- From: Erik [mailto:pyt...@lucidity.plus.com] Sent: Thursday, February 23, 2017 2:09 AM To: pyt...@deborahswanson.net; python-list@python.org Subject: Re: Namedtuples problem Hi, On 23/02/17 09:38, Deborah Swanson wrote

Re: Namedtuples problem

2017-02-23 Thread Erik
On 23/02/17 23:00, Deborah Swanson wrote: It looks to me like you are indexing into a single-element list that you are creating using the literal list syntax in the middle of the expression. Actually, group is essentially a 2-element list. Each group has a list of rows, and each row has a set

Re: Namedtuples problem

2017-02-23 Thread MRAB
On 2017-02-23 22:51, Deborah Swanson wrote: Peter Otten wrote, on February 23, 2017 2:34 AM [snip] #untested def split_into_groups(records, key): groups = defaultdict(list) for record in records: # no need to check if a group already exists # an empty list will

RE: Namedtuples problem

2017-02-23 Thread Deborah Swanson
> -Original Message- > From: Erik [mailto:pyt...@lucidity.plus.com] > Sent: Thursday, February 23, 2017 2:09 AM > To: pyt...@deborahswanson.net; python-list@python.org > Subject: Re: Namedtuples problem > > > Hi, > > On 23/02/17 09:38, Deborah

RE: Namedtuples problem

2017-02-23 Thread Deborah Swanson
Peter Otten wrote, on February 23, 2017 2:34 AM > > Deborah Swanson wrote: > > > > Can anyone see why I'm getting this Index error? and how to fix it? > > I'm not completely sure I can follow you, but you seem to be > mixing two problems > > (1) split a list into groups > (2) convert a list

RE: Namedtuples problem

2017-02-23 Thread Deborah Swanson
Thanks all of you for replying. I'm going to have to study your responses a bit before I can respond. I wrote this code the way I did because I was getting errors that I stopped getting with these fixes and I got the correct results from this code, up until the last line. All of you introduced

Re: Namedtuples problem

2017-02-23 Thread Peter Otten
Pavol Lisy wrote: > If we are talking about less code than necessary then probably we > could use something from python's ecosystem... > import pandas as pd # really useful package Yes, pandas looks like the perfect fit here. There is a learning curve, though, so that it will probably

Re: Namedtuples problem

2017-02-23 Thread Pavol Lisy
On 2/23/17, Peter Otten <__pete...@web.de> wrote: > Peter Otten wrote: > >> Functions to the rescue: > > On second thought this was still more code than necessary. If we are talking about less code than necessary then probably we could use something from python's ecosystem... >>> import pandas

Re: Namedtuples problem

2017-02-23 Thread Steve D'Aprano
On Thu, 23 Feb 2017 08:38 pm, Deborah Swanson wrote: > However, > > group[[idx][records_idx[label]]] > gets an Index Error: list index out of range That's not very helpful: judging from that line alone, there could be as many as THREE places in that line of code that might generate IndexError.

Re: Namedtuples problem

2017-02-23 Thread Peter Otten
Peter Otten wrote: > Functions to the rescue: On second thought this was still more code than necessary. Why not calculate a column on demand? Here's a self-containe example: $ cat swanson_grouped_columns_virtual.py import operator from collections import defaultdict, namedtuple def

Re: Namedtuples problem

2017-02-23 Thread Peter Otten
Deborah Swanson wrote: > This is how the list of namedtuples is originally created from a csv: > > infile = open("E:\\Coding projects\\Pycharm\\Moving\\Moving 2017 in - > test.csv") > rows = csv.reader(infile)fieldnames = next(rows) > Record = namedtuple("Record", fieldnames) > records =

Re: Namedtuples problem

2017-02-23 Thread Erik
Hi, On 23/02/17 09:38, Deborah Swanson wrote: group[[idx][records_idx[label]]] gets an Index Error: list index out of range [snip] Can anyone see why I'm getting this Index error? and how to fix it? It looks to me like you are indexing into a single-element list that you are creating