Denis
Denis

Reputation: 2314

Creating a set in dictionary

I need to create a set in a dictionary.

dicInvertedIndex = {}
docID = 1

for i in string:
  if condition:
    docID += 1
  dicInvertedIndex[i] = [1, set(docID)]

And i have a error:

dicInvertedIndex[i] = [1, set(docID)]
 TypeError: 'int' object is not iterable

Before I tried this, I created a list in the dictionary and it works.

dicInvertedIndex[i] = [ 1 , [ docID ] ]

And it works. I need to create a dictionary with keys of my documents and values of (int, set())

like dic["awake"] = [5, {2, 30, 99, 234}]

Originally I used a list, but it's slow, and i want to use a set.

Upvotes: 0

Views: 142

Answers (3)

user590028
user590028

Reputation: 11730

I have no idea what you are trying to accomplish, but I see your bug. The set constructor requires an iterable, and you are passing an integer. Here's the fix:

dicInvertedIndex[i] = [1, set([docID])]

Notice that docID is inside square brackets.

Upvotes: 1

Steven Rumbalski
Steven Rumbalski

Reputation: 45542

The set() constructor takes an iterable. docID is an integer and therefore not iterable.

Two workarounds are

{docID} 

or

set([docID])

Upvotes: 2

jonrsharpe
jonrsharpe

Reputation: 122032

The argument to set() is expected to be iterable, just put docID in a list when you pass it:

dicInvertedIndex[i] = [1, set([docID])]

Upvotes: 2

Related Questions