AttributeError: 'list' object has no attribute 'lower'
Roy Smith
roy at panix.com
Sun Sep 9 10:29:11 EDT 2012
In article <43a68990-d6cf-4362-8c47-b13ce780b068 at googlegroups.com>,
Token Type <typetoken at gmail.com> wrote:
> 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.
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)
More information about the Python-list
mailing list