Re: Using namedtuples field names for column indices in a list of lists

2017-01-12 Thread Michael Torrie
On 01/12/2017 02:26 AM, Deborah Swanson wrote: > It's true, I've only been on this list a few weeks, although I've seen > and been on the receiving end of the kind of "help" that feels more like > being sneered at than help. Not on this list, but on Linux and similar > lists. There does seem to be

Re: Using namedtuples field names for column indices in a list of lists

2017-01-12 Thread Chris Angelico
On Thu, Jan 12, 2017 at 9:27 PM, Marko Rauhamaa wrote: > An instructive anecdote: somebody I know told me he once needed the > definitive list of some websites. He posted a question on the relevant > online forum, but it fell on deaf ears. After some days, he replied to > his

Re: Using namedtuples field names for column indices in a list of lists

2017-01-12 Thread Marko Rauhamaa
"Deborah Swanson" : > I've only been on this list a few weeks, although I've seen and been > on the receiving end of the kind of "help" that feels more like being > sneered at than help. Not on this list, but on Linux and similar > lists. There does seem to be a "tough

RE: Using namedtuples field names for column indices in a list of lists

2017-01-12 Thread Deborah Swanson
Antoon Pardon wrote, on January 12, 2017 12:49 AM > > Op 11-01-17 om 23:57 schreef Deborah Swanson: > > > >> What are we supposed to do when somebody asks a question based on an > >> obvious mistake? Assume that they're a quick learner who has probably > >> already worked out their mistake and

Re: Using namedtuples field names for column indices in a list of lists

2017-01-12 Thread Antoon Pardon
Op 11-01-17 om 23:57 schreef Deborah Swanson: > >> What are we supposed to do when somebody asks a question based on an >> obvious mistake? Assume that they're a quick learner who has probably >> already worked out their mistake and doesn't need an answer? That >> would certainly make our life

RE: Using namedtuples field names for column indices in a list of lists

2017-01-11 Thread Deborah Swanson
Steven D'Aprano wrote, on January 10, 2017 6:19 PM > > On Tuesday 10 January 2017 18:14, Deborah Swanson wrote: > > > I'm guessing that you (and those who see things like you do) might > > not be used to working with quick learners who make mistakes at > > first but catch up with them real

Re: Using namedtuples field names for column indices in a list of lists

2017-01-11 Thread BartC
On 11/01/2017 02:18, Steven D'Aprano wrote: On Tuesday 10 January 2017 18:14, Deborah Swanson wrote: I'm guessing that you (and those who see things like you do) might not be used to working with quick learners who make mistakes at first but catch up with them real fast, or you're very

RE: Using namedtuples field names for column indices in a list of lists

2017-01-10 Thread Steven D'Aprano
On Tuesday 10 January 2017 18:14, Deborah Swanson wrote: > I'm guessing that you (and those who > see things like you do) might not be used to working with quick learners > who make mistakes at first but catch up with them real fast, or you're > very judgemental about people who make mistakes,

Re: Using namedtuples field names for column indices in a list of lists

2017-01-10 Thread Ethan Furman
On 01/09/2017 11:14 PM, Deborah Swanson wrote: So I guess you should just do your thing and I'll do mine. As you say. Takes all kinds, and I think in the end what will count is the quality of my finished work (which has always been excellent), and not the messy process to get there.

RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Ethan Furman wrote, on January 09, 2017 10:06 PM > > On 01/09/2017 08:51 PM, Deborah Swanson wrote: > > Ethan Furman wrote, on January 09, 2017 8:01 PM > > >> As I said earlier, I admire your persistence -- but take some time > >> and learn the basic vocabulary as that will make it much easier

Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Ethan Furman
On 01/09/2017 08:51 PM, Deborah Swanson wrote: Ethan Furman wrote, on January 09, 2017 8:01 PM As I said earlier, I admire your persistence -- but take some time and learn the basic vocabulary as that will make it much easier for you to ask questions, and for us to give you meaningful

RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Erik wrote, on January 09, 2017 8:06 PM > > On 10/01/17 03:02, Deborah Swanson wrote: > > Erik wrote, on January 09, 2017 5:47 PM > >> IIRC, you create it using a list comprehension which creates the > >> records. A list comprehension always creates a list. > > > > Well no. The list is created

RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Ethan Furman wrote, on January 09, 2017 8:01 PM > > On 01/09/2017 07:02 PM, Deborah Swanson wrote: > > Erik wrote, on January 09, 2017 5:47 PM > > >> As people keep saying, the object you have called 'records' is a > >> *list* of namedtuple objects. It is not a namedtuple. > >> > >> IIRC, you

RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
MRAB wrote, on January 09, 2017 7:37 PM > > On 2017-01-10 03:02, Deborah Swanson wrote: > > Erik wrote, on January 09, 2017 5:47 PM > >> As people keep saying, the object you have called 'records' is a > >> *list* of namedtuple objects. It is not a namedtuple. > >> > >> IIRC, you create it using

Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Erik
On 10/01/17 03:02, Deborah Swanson wrote: Erik wrote, on January 09, 2017 5:47 PM IIRC, you create it using a list comprehension which creates the records. A list comprehension always creates a list. Well no. The list is created with: records.extend(Record._make(row) for row in rows) No,

Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Ethan Furman
On 01/09/2017 07:02 PM, Deborah Swanson wrote: Erik wrote, on January 09, 2017 5:47 PM As people keep saying, the object you have called 'records' is a *list* of namedtuple objects. It is not a namedtuple. IIRC, you create it using a list comprehension which creates the records. A list

Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread MRAB
On 2017-01-10 03:02, Deborah Swanson wrote: Erik wrote, on January 09, 2017 5:47 PM As people keep saying, the object you have called 'records' is a *list* of namedtuple objects. It is not a namedtuple. IIRC, you create it using a list comprehension which creates the records. A list

RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Erik wrote, on January 09, 2017 5:47 PM > As people keep saying, the object you have called 'records' > is a *list* > of namedtuple objects. It is not a namedtuple. > > IIRC, you create it using a list comprehension which creates the > records. A list comprehension always creates a list. Well

Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Erik
On 10/01/17 00:54, Deborah Swanson wrote: Since I won't change the order of the records again after the sort, I'm using records.sort(key=operator.attrgetter("Description", "Date")) once, which also works perfectly. So both sorted() and sort() can be used to sort namedtuples. Good to know!

RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Peter Otten wrote, on January 09, 2017 3:27 PM > > While stable sort is nice in this case you can just say > > key=operator.attrgetter("Description", "Date") > > Personally I'd only use sorted() once and then switch to the > sort() method. This works perfectly, thank you. As I read the docs,

Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Peter Otten
breamore...@gmail.com wrote: > On Monday, January 9, 2017 at 5:34:12 PM UTC, Tim Chase wrote: >> On 2017-01-09 08:31, breamoreboy wrote: >> > On Monday, January 9, 2017 at 2:22:19 PM UTC, Tim Chase wrote: >> > > I usually wrap the iterable in something like >> > > >> > > def pairwise(it): >> >

Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread breamoreboy
On Monday, January 9, 2017 at 5:34:12 PM UTC, Tim Chase wrote: > On 2017-01-09 08:31, breamoreboy wrote: > > On Monday, January 9, 2017 at 2:22:19 PM UTC, Tim Chase wrote: > > > I usually wrap the iterable in something like > > > > > > def pairwise(it): > > > prev = next(it) > > > for

Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Peter Otten
Rhodri James wrote: > On 09/01/17 21:40, Deborah Swanson wrote: >> Peter Otten wrote, on January 09, 2017 6:51 AM >>> >>> records = sorted( >>> set(records), >>> key=operator.attrgetter("Description") >>> ) >> >> Good, this is confirmation that 'sorted()' is the way to go. I want a 2 >>

Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Rhodri James
On 09/01/17 21:40, Deborah Swanson wrote: Peter Otten wrote, on January 09, 2017 6:51 AM records = sorted( set(records), key=operator.attrgetter("Description") ) Good, this is confirmation that 'sorted()' is the way to go. I want a 2 key sort, Description and Date, but I think I can

RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
breamore...@gmail.com wrote, on January 09, 2017 8:32 AM > > On Monday, January 9, 2017 at 2:22:19 PM UTC, Tim Chase wrote: > > On 2017-01-08 22:58, Deborah Swanson wrote: > > > 1) I have a section that loops through the sorted data, compares two > > > adjacent rows at a time, and marks one of

RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Tim Chase wrote, on January 09, 2017 6:22 AM > > On 2017-01-08 22:58, Deborah Swanson wrote: > > 1) I have a section that loops through the sorted data, compares two > > adjacent rows at a time, and marks one of them for deletion if the > > rows are identical. > > and my question is whether

RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Peter Otten wrote, on January 09, 2017 6:51 AM > > Deborah Swanson wrote: > > > Even better, to get hold of all the records with the same Description > > as the current row, compare them all, mark all but the different ones > > for deletion, and then resume processing the records after the

Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread breamoreboy
On Monday, January 9, 2017 at 2:22:19 PM UTC, Tim Chase wrote: > On 2017-01-08 22:58, Deborah Swanson wrote: > > 1) I have a section that loops through the sorted data, compares two > > adjacent rows at a time, and marks one of them for deletion if the > > rows are identical. > > > > I'm using >

RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Peter Otten
Deborah Swanson wrote: > Even better, to get hold of all the records with the same Description as > the current row, compare them all, mark all but the different ones for > deletion, and then resume processing the records after the last one? When you look at all fields for deduplication anyway

Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Tim Chase
On 2017-01-08 22:58, Deborah Swanson wrote: > 1) I have a section that loops through the sorted data, compares two > adjacent rows at a time, and marks one of them for deletion if the > rows are identical. > > I'm using > > for i in range(len(records)-1): > r1 = records[i] > r2 =

RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Steve D'Aprano wrote, on January 09, 2017 3:40 AM > > On Mon, 9 Jan 2017 09:57 pm, Deborah Swanson wrote: > > [...] > > I think you are replying to my question about sorting a > namedtuple, in > > this case it's called 'records'. > > > > I think your suggestion works for lists and tuples, and

RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Steve D'Aprano
On Mon, 9 Jan 2017 09:57 pm, Deborah Swanson wrote: [...] > I think you are replying to my question about sorting a namedtuple, in > this case it's called 'records'. > > I think your suggestion works for lists and tuples, and probably > dictionaries. But namedtuples doesn't have a sort function.

RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Antoon Pardon wrote, on January 09, 2017 2:35 AM > If I understand correctly you want something like: > > records.sort(key = lamda rec: rec.xx) > > AKA > > from operator import attrgetter > records.sort(key = attrgetter('xx')) > > or maybe: > > records.sort(key = lambda rec:

RE: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Deborah Swanson
Antoon Pardon wrote, on January 09, 2017 2:14 AM > > 1) I have a section that loops through the sorted data, compares two > > adjacent rows at a time, and marks one of them for deletion if the > > rows are identical. > > > > I'm using > > > > for i in range(len(records)-1): > > r1 =

Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Antoon Pardon
Op 09-01-17 om 07:58 schreef Deborah Swanson: > Peter Otten wrote, on January 08, 2017 5:21 AM >> Deborah Swanson wrote: >> >>> Peter Otten wrote, on January 08, 2017 3:01 AM >> >> Personally I would recommend against mixing data (an actual location) > and >> metadata (the column

Re: Using namedtuples field names for column indices in a list of lists

2017-01-09 Thread Antoon Pardon
Op 09-01-17 om 07:58 schreef Deborah Swanson: > Peter Otten wrote, on January 08, 2017 5:21 AM >> Deborah Swanson wrote: >> >>> Peter Otten wrote, on January 08, 2017 3:01 AM >> >> Personally I would recommend against mixing data (an actual location) > and >> metadata (the column

RE: Using namedtuples field names for column indices in a list of lists

2017-01-08 Thread Deborah Swanson
Steven D'Aprano wrote, on January 08, 2017 7:30 PM > > On Sunday 08 January 2017 20:53, Deborah Swanson wrote: > > > Steven D'Aprano wrote, on January 07, 2017 10:43 PM > > No, I'm pretty sure that's not the case. I don't have access > to your CSV file, > but I can simulate it: > > ls =

RE: Using namedtuples field names for column indices in a list of lists

2017-01-08 Thread Deborah Swanson
Peter Otten wrote, on January 08, 2017 5:21 AM > > Deborah Swanson wrote: > > > Peter Otten wrote, on January 08, 2017 3:01 AM > > Personally I would recommend against mixing data (an actual location) and > metadata (the column name,"Location"), but if you wish my code can be > adapted as

RE: Using namedtuples field names for column indices in a list of lists

2017-01-08 Thread Deborah Swanson
Steven D'Aprano wrote, on January 07, 2017 10:43 PM > > On Sunday 08 January 2017 16:39, Deborah Swanson wrote: > > The recommended way is with the _replace method: > > py> instance._replace(A=999) > Record(A=999, B=20, C=30) > py> instance._replace(A=999, C=888) > Record(A=999, B=20, C=888) >

RE: Using namedtuples field names for column indices in a list of lists

2017-01-08 Thread Steven D'Aprano
On Sunday 08 January 2017 20:53, Deborah Swanson wrote: > Steven D'Aprano wrote, on January 07, 2017 10:43 PM >> >> On Sunday 08 January 2017 16:39, Deborah Swanson wrote: >> >> > What I've done so far: >> > >> > with open('E:\\Coding projects\\Pycharm\\Moving\\Moving >> 2017 in.csv', >> >

RE: Using namedtuples field names for column indices in a list of lists

2017-01-08 Thread Deborah Swanson
Paul Rudin wrote, on January 08, 2017 6:49 AM > > "Deborah Swanson" writes: > > > Peter Otten wrote, on January 08, 2017 3:01 AM > >> > >> columnA = [record.A for record in records] > > > > This is very neat. Something like a list comprehension for named > > tuples?

Re: Using namedtuples field names for column indices in a list of lists

2017-01-08 Thread Paul Rudin
"Deborah Swanson" writes: > Peter Otten wrote, on January 08, 2017 3:01 AM >> >> columnA = [record.A for record in records] > > This is very neat. Something like a list comprehension for named tuples? Not something like - this *is* a list comprehension - it creates a

RE: Using namedtuples field names for column indices in a list of lists

2017-01-08 Thread Deborah Swanson
Peter Otten wrote, on January 08, 2017 5:21 AM > > Deborah Swanson wrote: > > > Peter Otten wrote, on January 08, 2017 3:01 AM > >> > >> Deborah Swanson wrote: > >> > >> > to do that is with .fget(). Believe me, I tried every > possible > >> > way > > to > >> > use instance.A or instance[1]

RE: Using namedtuples field names for column indices in a list of lists

2017-01-08 Thread Peter Otten
Deborah Swanson wrote: > Peter Otten wrote, on January 08, 2017 3:01 AM >> >> Deborah Swanson wrote: >> >> > to do that is with .fget(). Believe me, I tried every > possible way > to >> > use instance.A or instance[1] and no way could I get ls[instance.A]. >> >> Sorry, no. > > I quite agree,

RE: Using namedtuples field names for column indices in a list of lists

2017-01-08 Thread Deborah Swanson
Peter Otten wrote, on January 08, 2017 3:01 AM > > Deborah Swanson wrote: > > > to do that is with .fget(). Believe me, I tried every > possible way to > > use instance.A or instance[1] and no way could I get ls[instance.A]. > > Sorry, no. I quite agree, I was describing the dead end I was in

RE: Using namedtuples field names for column indices in a list of lists

2017-01-08 Thread Peter Otten
Deborah Swanson wrote: > to do that is with .fget(). Believe me, I tried every possible way to > use instance.A or instance[1] and no way could I get ls[instance.A]. Sorry, no. To get a list of namedtuple instances use: rows = csv.reader(infile) Record = namedtuple("Record", next(rows))

RE: Using namedtuples field names for column indices in a list of lists

2017-01-08 Thread Deborah Swanson
Steven D'Aprano wrote, on January 07, 2017 10:43 PM > > On Sunday 08 January 2017 16:39, Deborah Swanson wrote: > > > What I've done so far: > > > > with open('E:\\Coding projects\\Pycharm\\Moving\\Moving > 2017 in.csv', > > 'r') as infile: > > ls = list(csv.reader(infile)) > > lst =

Re: Using namedtuples field names for column indices in a list of lists

2017-01-07 Thread Steven D'Aprano
On Sunday 08 January 2017 16:39, Deborah Swanson wrote: > What I've done so far: > > with open('E:\\Coding projects\\Pycharm\\Moving\\Moving 2017 in.csv', > 'r') as infile: > ls = list(csv.reader(infile)) > lst = namedtuple('lst', ls[0]) > > where 'ls[0]' is the header row of the csv,

Using namedtuples field names for column indices in a list of lists

2017-01-07 Thread Deborah Swanson
What I've done so far: with open('E:\\Coding projects\\Pycharm\\Moving\\Moving 2017 in.csv', 'r') as infile: ls = list(csv.reader(infile)) lst = namedtuple('lst', ls[0]) where 'ls[0]' is the header row of the csv, and it works perfectly well. 'lst' is a namedtuple instance with each of