Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
AttributeError: 'list' object has no attribute 'lower'
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  14 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Token Type  
View profile  
 More options Sep 8 2012, 1:13 pm
Newsgroups: comp.lang.python
From: Token Type <typeto...@gmail.com>
Date: Sat, 8 Sep 2012 10:13:21 -0700 (PDT)
Local: Sat, Sep 8 2012 1:13 pm
Subject: AttributeError: 'list' object has no attribute 'lower'
On page 77 of the book natural language processing with Python, we have such an exercise: The polysemy of a word is the number of senses it has. Using WordNet, we can determine that the noun doghas seven senses with len(wn.synsets('dog', 'n')).
Compute the average polysemy of nouns, verbs, adjectives, and adverbs according
to WordNet.http://nltk.googlecode.com/svn/trunk/doc/book/ch02.html

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?

>>> def average_polysemy(pos):

        synset_list = list(wn.all_synsets(pos))
        lemma_list = [synset.lemma_names for synset in synset_list]
        sense_number = 0
        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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "wordnet NLTK Re: AttributeError: 'list' object has no attribute 'lower'" by Token Type
Token Type  
View profile  
 More options Sep 8 2012, 1:33 pm
Newsgroups: comp.lang.python
From: Token Type <typeto...@gmail.com>
Date: Sat, 8 Sep 2012 10:32:59 -0700 (PDT)
Local: Sat, Sep 8 2012 1:32 pm
Subject: wordnet NLTK Re: AttributeError: 'list' object has no attribute 'lower'
I don't know why lemma_list = [synset.lemma_names for synset in synset_list] will lead to such an error.

I have to use extend to solve the problem for lemma_list. The following codes are successful, take all the nouns as an example:

>>> def average_polysemy(pos):

   synset_list = list(wn.all_synsets(pos))
   sense_number = 0
   lemma_list = []
   for synset in synset_list:
     lemma_list.extend(synset.lemma_names)
   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')

3


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "AttributeError: 'list' object has no attribute 'lower'" by Roy Smith
Roy Smith  
View profile  
 More options Sep 8 2012, 1:45 pm
Newsgroups: comp.lang.python
From: Roy Smith <r...@panix.com>
Date: Sat, 08 Sep 2012 13:45:39 -0400
Local: Sat, Sep 8 2012 1:45 pm
Subject: Re: AttributeError: 'list' object has no attribute 'lower'
In article <df7ab5f7-c273-4a62-b79a-f364f9c2d3b0@googlegroups.com>,
 Token Type <typeto...@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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Cameron Simpson  
View profile  
 More options Sep 8 2012, 7:26 pm
Newsgroups: comp.lang.python
From: Cameron Simpson <c...@zip.com.au>
Date: Sun, 9 Sep 2012 09:26:46 +1000
Local: Sat, Sep 8 2012 7:26 pm
Subject: Re: AttributeError: 'list' object has no attribute 'lower'
On 08Sep2012 13:45, Roy Smith <r...@panix.com> wrote:
| First, I don't understand this code:
|
| In article <df7ab5f7-c273-4a62-b79a-f364f9c2d3b0@googlegroups.com>,
|  Token Type <typeto...@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 <c...@zip.com.au>

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Token Type  
View profile  
 More options Sep 9 2012, 9:50 am
Newsgroups: comp.lang.python
From: Token Type <typeto...@gmail.com>
Date: Sun, 9 Sep 2012 06:50:16 -0700 (PDT)
Local: Sun, Sep 9 2012 9:50 am
Subject: Re: AttributeError: 'list' object has no attribute 'lower'
Thanks very much for all of your tips. Take noun as an example. First, I need find all the lemma_names in all the synsets whose pos is 'n'. Second, for each lemma_name, I will check all their sense number.

1)  Surely,we can know the number of synset whose pos is noun by

>>> len([synset for synset in wn.all_synsets('n')])

82115

However, confusingly it is unsuccessful to get a list of lemma names of these synsets by

>>> lemma_list = [synset.lemma_names for synset in wn.all_synsets('n')]
>>> lemma_list[:20]

[['entity'], ['physical_entity'], ['abstraction', 'abstract_entity'], ['thing'], ['object', 'physical_object'], ['whole', 'unit'], ['congener'], ['living_thing', 'animate_thing'], ['organism', 'being'], ['benthos'], ['dwarf'], ['heterotroph'], ['parent'], ['life'], ['biont'], ['cell'], ['causal_agent', 'cause', 'causal_agency'], ['person', 'individual', 'someone', 'somebody', 'mortal', 'soul'], ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna'], ['plant', 'flora', 'plant_life']]
>>> type(lemma_list)

<type 'list'>

Though the lemma_list is a list in the above codes, it contains so many unnecessary [ and ]. How come it is like this? But what we desire and expect is a list without this brackets. Confused, I am really curious to know why.

2)  Then I have to use a loop and extend to get all the lemma_names from synset:

>>> synset_list = list(wn.all_synsets('n'))
>>> lemma_list = []
>>> for synset in synset_list:

        lemma_list.extend(synset.lemma_names)
>>> lemma_list[:20]

['entity', 'physical_entity', 'abstraction', 'abstract_entity', 'thing', 'object', 'physical_object', 'whole', 'unit', 'congener', 'living_thing', 'animate_thing', 'organism', 'being', 'benthos', 'dwarf', 'heterotroph', 'parent', 'life', 'biont']

3) In this case, I have to use loop to get all the lemma_names instead of [synset.lemma_names for synset in wn.all_synsets('n')]. The following is a working solution:

>>> def average_polysemy(pos):

 synset_list = list(wn.all_synsets(pos))
 sense_number = 0
 lemma_list = []
 for synset in synset_list:
  lemma_list.extend(synset.lemma_names)
 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')

3

Thanks again.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Token Type  
View profile  
 More options Sep 9 2012, 10:00 am
Newsgroups: comp.lang.python
From: Token Type <typeto...@gmail.com>
Date: Sun, 9 Sep 2012 07:00:34 -0700 (PDT)
Local: Sun, Sep 9 2012 10:00 am
Subject: Re: AttributeError: 'list' object has no attribute 'lower'

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

Thanks. You guess right. It turns out that lemma_list is a list of list, as I tested in the previous post.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Token Type  
View profile  
 More options Sep 9 2012, 10:19 am
Newsgroups: comp.lang.python
From: Token Type <typeto...@gmail.com>
Date: Sun, 9 Sep 2012 07:19:42 -0700 (PDT)
Local: Sun, Sep 9 2012 10:19 am
Subject: Re: AttributeError: 'list' object has no attribute 'lower'

> structures are simple, just plain print will work, but for more

> complicated structures, pprint.pprint() is a life saver.

I did try . However,

>>> pprint.pprint(lemma_list)

Traceback (most recent call last):
  File "<pyshell#74>", line 1, in <module>
    pprint.pprint(lemma_list)
NameError: name 'pprint' is not defined

>>> pprint.pprint(synset_list)

Traceback (most recent call last):
  File "<pyshell#75>", line 1, in <module>
    pprint.pprint(synset_list)
NameError: name 'pprint' is not defined


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roy Smith  
View profile  
 More options Sep 9 2012, 10:29 am
Newsgroups: comp.lang.python
From: Roy Smith <r...@panix.com>
Date: Sun, 09 Sep 2012 10:29:11 -0400
Local: Sun, Sep 9 2012 10:29 am
Subject: Re: AttributeError: 'list' object has no attribute 'lower'
In article <43a68990-d6cf-4362-8c47-b13ce780b068@googlegroups.com>,
 Token Type <typeto...@gmail.com> wrote:

It looks like synset.lemma_names gets you a list.  And then you're
taking all those lists and forming them into a list of lists:

>>> lemma_list = [synset.lemma_names for synset in wn.all_synsets('n')]

I think what you want to study is the difference between list.append()
and list.extend().  When you use the list builder syntax, you're
essentially writing a loop which does append operations.  The above is
the same as if you wrote:

lemma_list = list()
for synset in wn.all_synsets('n'):
    lemma_list.append(synset.lemma_names)

and I think what you're looking for is:

lemma_list = list()
for synset in wn.all_synsets('n'):
    lemma_list.extend(synset.lemma_names)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roy Smith  
View profile  
 More options Sep 9 2012, 10:32 am
Newsgroups: comp.lang.python
From: Roy Smith <r...@panix.com>
Date: Sun, 09 Sep 2012 10:32:33 -0400
Local: Sun, Sep 9 2012 10:32 am
Subject: Re: AttributeError: 'list' object has no attribute 'lower'
In article <dea2fdd1-ad19-4254-b3bf-4104ce0cb241@googlegroups.com>,
 Token Type <typeto...@gmail.com> wrote:

OK, I can see how this can be confusing.  In "pprint.pprint()", the two
"pprint"s mean different things.  The first one is the name of a module.  
The second one is the name of a function in that module.  In general, I
dislike this style of naming since it just leads to this kind of
confusion.

In any case, you need to do one of two things.

Style 1:

import pprint
pprint.pprint(foo)

Style 2:

from pprint import pprint
pprint(foo)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jean-Michel Pichavant  
View profile  
 More options Sep 10 2012, 5:52 am
Newsgroups: comp.lang.python
From: Jean-Michel Pichavant <jeanmic...@sequans.com>
Date: Mon, 10 Sep 2012 11:52:34 +0200
Local: Mon, Sep 10 2012 5:52 am
Subject: Re: AttributeError: 'list' object has no attribute 'lower'

Token Type wrote:
>> 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.

> Thanks. You guess right. It turns out that lemma_list is a list of list, as I tested in the previous post.

I often noticed people around me that are not that familiar with python
are dismissing the error stack so quickly ; they probably knows the
machine is trying to communicate with them but they don't seem to
understand the message. Error stacks may be difficult to read at first
glance but you can solve a lot of problems just by reading it.

So next time you see 'X' has no attribute 'Y', you'll know that you've
accessed an attribute/method of an object that does not exist, either
you made a typo in the attribute name, or you object is not actually
what you think it is.

Advice : if you have so time, install ipython and execute your scripts
in an ipython shell with the %pdb faeture on. This will automatically
call the debugger upon unhandled exceptions and you'll be able to
inspect your objects live from the prompt.

JM


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Token Type  
View profile  
 More options Sep 14 2012, 11:01 am
Newsgroups: comp.lang.python
From: Token Type <typeto...@gmail.com>
Date: Fri, 14 Sep 2012 08:01:11 -0700 (PDT)
Local: Fri, Sep 14 2012 11:01 am
Subject: Re: AttributeError: 'list' object has no attribute 'lower'
Thanks. By the way, do we have a list of explanations of error message? If so, whenever we come across error message, we can refer to it and solve the problem accordingly.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Token Type  
View profile  
 More options Sep 14 2012, 11:01 am
Newsgroups: comp.lang.python
From: Token Type <typeto...@gmail.com>
Date: Fri, 14 Sep 2012 08:01:11 -0700 (PDT)
Local: Fri, Sep 14 2012 11:01 am
Subject: Re: AttributeError: 'list' object has no attribute 'lower'
Thanks. By the way, do we have a list of explanations of error message? If so, whenever we come across error message, we can refer to it and solve the problem accordingly.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Sep 14 2012, 11:18 am
Newsgroups: comp.lang.python
From: Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>
Date: 14 Sep 2012 15:18:27 GMT
Local: Fri, Sep 14 2012 11:18 am
Subject: Re: AttributeError: 'list' object has no attribute 'lower'

On Fri, 14 Sep 2012 08:01:11 -0700, Token Type wrote:
> Thanks. By the way, do we have a list of explanations of error message?
> If so, whenever we come across error message, we can refer to it and
> solve the problem accordingly.

Forget about a "list of explanations of error message[s]". There is no
such list, and there never will be, because there is no limit to the
number and kind of possible error messages.

Instead, you should actually read the error message you see. Python is
telling you what the problem is. Pay attention to it.

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

This tells you that you tried to access something.lower, but "something"
is a list, and lists don't have an attribute or method "lower".

Normally, Python will show you the line of source code with the error, so
you will even see the name of the variable.

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Sep 14 2012, 11:19 am
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Sat, 15 Sep 2012 01:19:24 +1000
Local: Fri, Sep 14 2012 11:19 am
Subject: Re: AttributeError: 'list' object has no attribute 'lower'

On Sat, Sep 15, 2012 at 1:01 AM, Token Type <typeto...@gmail.com> wrote:
> Thanks. By the way, do we have a list of explanations of error message? If so, whenever we come across error message, we can refer to it and solve the problem accordingly.

Not really, but if you paste the message into Google or DuckDuckGo or
another web search engine, you'll usually find something helpful.
Possibly add a few keywords about what you're doing, if the message
alone is too general.

By the way, you don't need to include both comp.lang.python and
python-list in your addressees; they mirror each other, so sending to
one will make it arrive on the other too.

Have fun!

ChrisA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »