0

I am trying to see if a company from a list of companies is in a line in a file. If it is I utilize the index of that company to increment a variable in another array. The following is my python code. I keep getting the following error: AttributeError: 'set' object has no attribute 'index'. I cannot figure out what is going wrong and think the error is the line that is surrounded by **.

companies={'white house black market', 'macy','nordstrom','filene','walmart'}
positives=[0 for x in xrange(len(companies))]
negatives=[0 for x in xrange(len(companies))]

for line in f:
    for company in companies:
        if company in line.lower():
            words=tokenize.word_tokenize(line)
            bag=bag_of_words(words)
            classif=classifier.classify(bag)
            if classif=='pos':
                **indice =companies.index(company)**
                positives[indice]+=1
            elif classif=='neg':
                **indice =companies.index(company)**
                negatives[indice]+=1 
2
  • This is no valid Python code Commented Nov 23, 2012 at 4:59
  • it should be companies=['white house black market', 'macy','nordstrom','filene','walmart'] Commented Nov 23, 2012 at 5:00

3 Answers 3

3
companies={'white house black market', 'macy','nordstrom','filene','walmart'}

Is a set. It has unique entries.

companies=['white house black market', 'macy','nordstrom','filene','walmart']

Is a list, and can multiple entries of the same value. It can also be indexed.

Sign up to request clarification or add additional context in comments.

Comments

2
companies={'white house black market', 'macy','nordstrom','filene','walmart'}

The above declaration is a set. And since a set has no ordering, you cannot get index of any elements from it.

>>> d = {2, 3, 4, 5}
>>> d
set([2, 3, 4, 5])  # It is a Set

So, to index an element, it should be declared as a List: -

companies=['white house black market', 'macy','nordstrom','filene','walmart']

3 Comments

... don't {} represent dicts in python?
@johnthexiii.. Yeah they represent, but only when it is declared as key-value pair. Else it is a set. Try printing companies in interpreter. You will get that.
@johnthexiii.. I added example.
1

companies is a set, and set has no order, so it can't use index(). You can change it to list:

companies=['white house black market', 'macy','nordstrom','filene','walmart']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.