Alan Gauld

Hi!
We are not quite out of the woods on this last example you gave me. It now seems to be complaining that it doesn't want to append an integer to the list or that this isn't the place to use '.append' -- I am probably interpreting it's complaint wrong:

Python 3.3

If I run this last piece of code that we just added 'enumerate(row)' to:

lens = [0] * len(catalog2[0])
for row in catalog2:
    for col, item in enumerate(row):
        print(col, item, len(item))
        lens[col].append(len(item))
lens = [max(col) for col in lens]

My result is:

0 Drives 6         <<---- my print statement result

Traceback (most recent call last):
File "/home/justme/1a_Computer_Related/Python/scripts/scratch.py", line 43, in <module>
    lens[col].append(len(item))
AttributeError: 'int' object has no attribute 'append'


While messing with the above problem, I found myself thinking (almost always a mistake on my part) that the '.append' extension doesn't need the list pre-formatted any more than a simple lens= [], since .append has the ability to expand the list anyway, which would let the number of columns be more easily expanded later in life by the programmer simply adding a column of data to the table, so I tried-

lens = []
for row in catalog2:
    for col, item in enumerate(row):
        print(col, item, len(item))
        lens[col].append(len(item))
lens = [max(col) for col in lens]

But my result says otherwise:

0 Drives 6

Traceback (most recent call last):
File "/home/justme/1a_Computer_Related/Python/scripts/scratch.py", line 45, in <module>
    lens[col].append(len(item))
IndexError: list index out of range


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to