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

Roy Smith roy at panix.com
Sat Sep 8 13:45:39 EDT 2012


In article <df7ab5f7-c273-4a62-b79a-f364f9c2d3b0 at googlegroups.com>,
 Token Type <typetoken at gmail.com> wrote:

> I wrote the following function to solve it. However, it pops up 
> "AttributeError: 'list' object has no attribute 'lower'". Quite confused, I 
> supposed [synset.lemma_names for synset in synset_list] has made all the 
> lemma into a list, hasn't it?

I'm not familiar with that library, but here's a few general ideas to 
help you figure out what's going on.

First, I don't understand this code:

> 	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)]

?  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.

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.



> 	for lemma in lemma_list:
> 		sense_number_new = len(wn.synsets(lemma, pos))
> 		sense_number = sense_number + sense_number_new
> 	return sense_number/len(synset_list)
> 
> >>> average_polysemy('n')
> 
> Traceback (most recent call last):
>   File "<pyshell#54>", line 1, in <module>
>     average_polysemy('n')
>   File "<pyshell#53>", line 6, in average_polysemy
>     sense_number_new = len(wn.synsets(lemma, pos))
>   File "C:\Python27\lib\site-packages\nltk\corpus\reader\wordnet.py", line 
>   1191, in synsets
>     lemma = lemma.lower()
> AttributeError: 'list' object has no attribute 'lower'
> 
> Thanks for your tips



More information about the Python-list mailing list