AttributeError: 'list' object has no attribute 'lower'

Cameron Simpson cs at zip.com.au
Sat Sep 8 19:26:46 EDT 2012


On 08Sep2012 13:45, Roy Smith <roy at panix.com> wrote:
| First, I don't understand this code:
| 
| In article <df7ab5f7-c273-4a62-b79a-f364f9c2d3b0 at googlegroups.com>,
|  Token Type <typetoken at gmail.com> wrote:
| > 	synset_list = list(wn.all_synsets(pos))
| > 	lemma_list = [synset.lemma_names for synset in synset_list]
| 
| It looks like you're taking an iterable, converting it to a list, just 
| so you can iterate over it again.  Why not the simpler:
| 
| > lemma_list = [synset.lemma_names for synset in wn.all_synsets(pos)]

Speaking for myself, when I write something like that it is because I
need to iterate over it twice or more. Often I'll make a tuple instead
of a list in that case, too, to avoid certain types of accidents.

| ?  But, I'm also confused about what lemma_list is supposed to end up 
| being.  The name "lemma_names" is plural, making me think it returns a 
| list of something.  And then you build those up into a list of lists?
| 
| In fact, I'm guessing that's your problem.  I think you're ending up 
| with a list of lists of strings, when you think you're getting a list of 
| strings.

In my case, I have most often had this error (<list>.lower or its
equivalent) when I've accidentally converted a string into a list
of characters; easy to do because strings are themselves iterables,
yielding a sequence of single character strings:-)

It is usually an accident from getting my nesting wrong somewhere.

| My suggestion is to print out all the intermediate data structures 
| (synset_list, lemma_list, etc) and see what they look like.  If the 
| structures are simple, just plain print will work, but for more 
| complicated structures, pprint.pprint() is a life saver.
| 
| Another possibility is to assert that things are what you expect them to 
| be.  Something like:
| 
| assert isinstance(synset_list, list)
| assert isinstance(lemma_list, list)
| assert isinstance(lemma_list[0], str)
| 
| and so on.

+1 to all of this, too.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au>

Too much of a good thing is never enough.       - Luba



More information about the Python-list mailing list