Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #28750

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

Date 2012-09-09 09:26 +1000
From Cameron Simpson <cs@zip.com.au>
Subject Re: AttributeError: 'list' object has no attribute 'lower'
References <roy-838395.13453908092012@news.panix.com>
Newsgroups comp.lang.python
Message-ID <mailman.395.1347146810.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 08Sep2012 13:45, Roy Smith <roy@panix.com> wrote:
| First, I don't understand this code:
| 
| In article <df7ab5f7-c273-4a62-b79a-f364f9c2d3b0@googlegroups.com>,
|  Token Type <typetoken@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@zip.com.au>

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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

AttributeError: 'list' object has no attribute 'lower' Token Type <typetoken@gmail.com> - 2012-09-08 10:13 -0700
  wordnet NLTK Re: AttributeError: 'list' object has no attribute 'lower' Token Type <typetoken@gmail.com> - 2012-09-08 10:32 -0700
  Re: AttributeError: 'list' object has no attribute 'lower' Roy Smith <roy@panix.com> - 2012-09-08 13:45 -0400
    Re: AttributeError: 'list' object has no attribute 'lower' Cameron Simpson <cs@zip.com.au> - 2012-09-09 09:26 +1000
    Re: AttributeError: 'list' object has no attribute 'lower' Token Type <typetoken@gmail.com> - 2012-09-09 06:50 -0700
      Re: AttributeError: 'list' object has no attribute 'lower' Roy Smith <roy@panix.com> - 2012-09-09 10:29 -0400
    Re: AttributeError: 'list' object has no attribute 'lower' Token Type <typetoken@gmail.com> - 2012-09-09 07:00 -0700
      Re: AttributeError: 'list' object has no attribute 'lower' Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-09-10 11:52 +0200
        Re: AttributeError: 'list' object has no attribute 'lower' Token Type <typetoken@gmail.com> - 2012-09-14 08:01 -0700
          Re: AttributeError: 'list' object has no attribute 'lower' Chris Angelico <rosuav@gmail.com> - 2012-09-15 01:19 +1000
        Re: AttributeError: 'list' object has no attribute 'lower' Token Type <typetoken@gmail.com> - 2012-09-14 08:01 -0700
          Re: AttributeError: 'list' object has no attribute 'lower' Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-14 15:18 +0000
    Re: AttributeError: 'list' object has no attribute 'lower' Token Type <typetoken@gmail.com> - 2012-09-09 07:19 -0700
      Re: AttributeError: 'list' object has no attribute 'lower' Roy Smith <roy@panix.com> - 2012-09-09 10:32 -0400

csiph-web